当前位置:网站首页>Quick start to connection pooling
Quick start to connection pooling
2022-07-26 20:32:00 【Soup key】
Catalog
Background of database connection
Custom database connection pool
Background of database connection
- Database connection is a key 、 Limited 、 Expensive resources
- This is especially prominent in multi-user web applications
- The management of database connections can significantly affect the performance of the entire application Performance indicators , Database connection pool is proposed to solve this problem
- Such as : When users visit the website, they constantly create connections and constantly destroy connections , Then the efficiency is not very high
Database connection pool
- Database connection pool To be responsible for the distribution of , Manage and release database connections
- It allows applications to Reuse An existing database connection , Instead of building a new
- This technology can obviously improve the performance of database operation
- Brief process :
- Is to prepare a container in advance , This container is the connection pool of the database , In the container , Prepare some database connections in advance
- Then came the request , Take a connection object from the connection pool and use it
- Don't turn it off after use , Instead, it is returned to the connection pool , Achieve the effect of reuse
- The so-called pool , Is to make a container , Many database connection objects are stored in this container
DataSource
- DataSource Interface Overview
- javax.sql.DataSourse Interface : data source ( Database connection pool )
- Java Official database connection pool specification ( Interface )
- If you want to complete the database connection pool technology , It has to happen DataSource Interface
- Core functions :
- Get database connection object :
- Connection getConnection();
Custom database connection pool
- 1. Define a class , Realization DataSource Interface
- 2. Define a container , Used to save multiple Connection Connection object
- 3. Define static code blocks , adopt JDBC Utility class acquisition 10 Save the connection to the container
- 4. rewrite getConnection Method , Get a connection from the container and return
- 5. Definition getSize Method , Used to get the size of the container and return
demonstration

package demo02.myDataSourse; import demo02.utils.JDBCUtils; import javax.sql.DataSource; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.logging.Logger; // Custom database connection pool public class demoDataSourse implements DataSource{ //1. Preparation container , Used to save multiple connection objects private static List<Connection> pool = Collections.synchronizedList(new ArrayList<>()); //2. Define static code blocks , Get... Through the tool class 10 A connection object static{ for(int i=1;i<=10;i++){ Connection con = JDBCUtils.getConnection(); pool.add(con); } } //3. rewrite getConnection(), Used to get a connection object @Override public Connection getConnection() throws SQLException { if(pool.size()>0){ Connection con = pool.remove(0); return con; }else{ throw new RuntimeException(" The number of connections has been exhausted "); } } //4. Definition getSize Method , Gets the size of the connection pool container public int getSize(){ return pool.size(); } @Override public Connection getConnection(String username, String password) throws SQLException { return null; } @Override public <T> T unwrap(Class<T> iface) throws SQLException { return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; } @Override public PrintWriter getLogWriter() throws SQLException { return null; } @Override public void setLogWriter(PrintWriter out) throws SQLException { } @Override public void setLoginTimeout(int seconds) throws SQLException { } @Override public int getLoginTimeout() throws SQLException { return 0; } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { return null; } }test
- The test here does not return the connected object , It's a direct shut down
- There are effects in the presentation , How to return it will be introduced later


package demo02.myDataSourse; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class demoDataSourseTest { public static void main(String[] args) throws Exception { //1. Create connection pool object demoDataSourse dataSourse = new demoDataSourse(); System.out.println(" Quantity before use " + dataSourse.getSize()); //2. Get the connection object through the connection pool object Connection con = dataSourse.getConnection(); //3. Query all the information in the student table String sql = "SELECT * FROM student"; PreparedStatement pst = con.prepareStatement(sql); //4. perform sql sentence , Receive the result set ResultSet rs = pst.executeQuery(); //5. Processing result set while(rs.next()){ System.out.println(rs.getInt("sid")+"\t"+rs.getString("name")+"\t"+rs.getInt("age")+"\t"+rs.getDate("birthday")); } //6. Release resources rs.close(); pst.close(); con.close();// After use , Not returned to close the connection ; Return later System.out.println(" Quantity after use " + dataSourse.getSize()); } }
边栏推荐
- SwiftUI 4 新功能之实时获取点击位置 .onTapGesture { location in} (教程含源码)
- Strengthen supervision on secret room escape and script killing, and focus on strengthening fire safety and juvenile protection
- Arpspoof installation and use
- Software testing - development test content specification (project test template)
- Task 2 kaggle diabetes detection
- How to implement an asynchronous task queue system that can handle massive data (supreme Collection Edition)
- 为什么 ThreadLocal 可以做到线程隔离?
- Cookies and sessions
- 「企业管理」精诚CRM+——一体化管理企业业务流程
- This point - super classic interview questions
猜你喜欢

Silent desktop fan chip dltap703sd Jericho

Cookies and sessions

regular expression

BUU刷题记4

What are the key technologies of digital factory

EtherCAT 同步模式

Array operations add, delete, modify, and query

第二章:遇到阻难!绕过WAF过滤!【SQL注入攻击】

YGG cooperates with my pet hooligan, AMGI's flagship NFT project, to enter the rabbit hole

本机号码一键登录原理与应用(荣耀典藏版)
随机推荐
BGP的路由黑洞和防环
A super simple neural network code with 5 coordinates for one layer node training
软件测试-开发提测内容规范(项目提测模板)
tkinter使用wpf控件
Read the four service types of kubernetes!
Shell script basic programming commands
Ue5 editor slate quick start [opening]
How to praise others gracefully? Try these methods
第二章:遇到阻难!绕过WAF过滤!【SQL注入攻击】
After being fined "paid leave" for one month, Google fired him from AI on "love"
this指向,最简单的规则记住它
Cookies and sessions
剑指offer46把数字翻译成字符串
Dio problem summary
数据块的存储系统中缓存的作用是什么?
小场景带来大提升!百度飞桨EasyDL助力制造业流水线AI升级
Nmap installation and use
本机号码一键登录原理与应用(荣耀典藏版)
Gbase learning - install gbase 8A MPP cluster v95
【Delphi】FMX Form的BorderStyles不同平台说明


