当前位置:网站首页>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()); } }
边栏推荐
- Bean注入和生命周期
- [record of question brushing] 22. bracket generation
- numpy.zeros_ like
- 如何优雅地赞美他人?不妨尝试下这几种方式
- 解决AttributeError: module ‘win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9‘ has no attribu
- What are the key technologies of digital factory
- Solve attributeerror: module 'win32com.gen_ py. 00020813-0000-0000-C000-000000000046x0x1x9‘ has no attribu
- The Sandbox 和艺术家 Alec Monopoly 达成合作
- nmap安装和使用
- 数据块的存储系统中缓存的作用是什么?
猜你喜欢
随机推荐
What are the key technologies of digital factory
LCP 11. 期望个数统计
This points to the simplest rule remember it
剑指offer46把数字翻译成字符串
Parallel execution (II). Multiprocessing
C # convert PDF files into pictures
任务二 kaggle糖尿病检测
医疗直播平台需要什么功能
QT驾校科目考试系统——从实现到发布
【刷题记录】22. 括号生成
How to build a super interface collaboration platform: count the six weapons of apifox
5.20晚上单身狗都在哪里?
YGG 与 AMGI 的旗舰 NFT 项目 My Pet Hooligan 合作进入 The Rabbit Hole
nmap安装和使用
MySQL InnoDB engine (V)
【Delphi】FMX Form的BorderStyles不同平台说明
数字化工厂的优势有哪些
this指向-超经典面试题
three. JS tag and pop-up the earth
ES6新特性












