jdbc connection pool

Because the creation and destruction of each database connection is time-consuming and labor-intensive. Therefore, the basic strategy is to use the JDBC connection pool to manage all connections.

The general connection pool is just a LinkedList.

For example, we write an implementation class of javax.sql.DataSource, and there is a private variable in the class: LinkedList<Connection> pool = new LinkedList<Connection>();

You can write a static code to initialize the pool variable: For example, create 10 Conncetions.

Java code   Favorite code
  1. private static LinkedList<Connection> pool = new LinkedList<Connection>();  
  2. static{  
  3. //Initialize DriverManager  
  4. //......  
  5. for(int i=0; i<10; i++){  
  6.    Connection conn = DriverManager.getConnection(url, username,password);  
  7.    pool.add(conn);  
  8.  }  
  9. }  

 

When the client calls dataSource.getConnection(), it does not create a new Connection, but takes out a Connection instance from the pool and passes it to the client.

However, when the client calls the connection.close(); function, we should return the secondary connection to the pool instead of actually closing the connection. But the connection.close() function is originally implemented by the Driver (mysql driver, oracle driver, oracle driver. We cannot change the internal implementation of the driver. The solution can only be external to the close() method. rewrite or extend.

solution:

(1). Decoration mode

Overridden class: com.mysql.jdbc.Connection

Write a class CustomedConnection that implements the same interface as com.mysql.jdbc.Connection (java.sql.Conneciton)

Java code   Favorite code
  1. public class CustomedConnection implements java.sql.Connection{  
  2.   private Connection sqlConnection ;  
  3.   private LinkedList<Connection> pool;  
  4.   
  5.   public CustomedConnection(Connection sqlConnection, LinkedList<Connection> pool){  
  6.       this.sqlConnection = sqlConnection;  
  7.       this.pool = pool;  
  8. }  
  9.   
  10.   public void close(){  
  11.     this.pool.add(sqlConnection);  
  12. }  
  13.   
  14.   
  15.  // ......  
  16. //java.sql.Connection 的其他方法的实现都直接调用sqlConnection的实现即可。除了close() 方法。  
  17. //  ......  
  18.   
  19. }  

 DataSource 的实现中:

Java代码   Favorite code
  1. public synchronized Connection getConnection() throws SQLException{  
  2. if(pool.size()>0){  
  3.     Connection conn = pool.remove();//从池中拿出一个connection  
  4.     CustomedConnection customedConnection = new CustomedConnection(conn, pool);  
  5.      return customedConnection;  
  6. }  
  7. else{  
  8.     throw new RuntimeException(“sorry, server busy”);  
  9. }  
  10. }  

 

 (二). 适配器模式

   

Java代码   Favorite code
  1. public class ConnectionAdaptor implements Connection{  
  2.       private Connection conn;  
  3.       public ConnectionAdaptor(Connection conn){  
  4.               this.conn=conn;    
  5.       }  
  6.      //重写所有Connection的方法:都直接调用conn的响应方法即可。  
  7.      //......  
  8. }  
  9.   
  10. //接下来写一个CustomedConnection 继承此适配器:  
  11. public class CustomedConnection extends ConnectionAdaptor {  
  12.     private LinkedList<Connection> pool ;  
  13.     public CustomedConnection(Connection conn, LinkedList<Connection> pool){  
  14.        super(conn);  
  15.        this.pool = pool;  
  16. }  
  17.   
  18. //重写close() 方法即可  
  19. //......  
  20.   
  21. }  

 (三).动态代理

    在实现DataSource的getConnection()方法里,拦截Connection的所有方法,如果方法名为close, 则改变其原本逻辑:

Java代码   Favorite code
  1. public synchronized Connection getConnection(){  
  2.   if(pool.size()>0){  
  3.   final Connection conn = pool.remove();  
  4.   return Proxy.newProxyInstance(conn.getClass().getClassLoader(), conn.getClass().getInterfaces(), new InvocationHandler(){  
  5.            @Override  
  6.            public Object invoke(Object proxy, Method method, Object[] args){  
  7.                     if("close".equals(methode))  
  8.                           return pool.add(conn);  
  9.                     else   
  10.                           return method.invoke(proxy, args);  
  11.              }  
  12.       }  
  13.   );  
  14. }  
  15. }  

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944715&siteId=291194637