Create a connection pool for a database using a dynamic Proxy and return the connection

Source: Internet
Author: User
Packagetk. dong. connection. util; importjava. io. IOException; importjava. io. inputStream; importjava. io. printWriter; importjava. lang. reflect. invocationHandler; importjava. lang. reflect. method; importjava. lang. reflect. proxy; importjava. SQL. conn

Package tk. dong. connection. util; import java. io. IOException; import java. io. inputStream; import java. io. printWriter; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; import java. SQL. conn

Package tk. dong. connection. util; import java. io. IOException; import java. io. inputStream; import java. io. printWriter; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. SQL. SQLFeatureNotSupportedException; import java. util. using list; import java. util. properties; import java. util. logging. logger; import javax. SQL. dataSource; // This is the public class JdbcPoolProxy implements DataSource operation for creating a connection pool and returning a link through a dynamic Proxy (Proxy ).
 
  
Connections = new external list
  
   
(); Static {// get the configuration file of properties and store InputStream inputStream = JdbcPoolProxy as a stream. class. getClassLoader (). getResourceAsStream ("jdbc. properties "); // property processing object for creating properties Properties properties = new Properties (); try {// load the property file to properties. load (inputStream); // gets the connected driver File Class. forName (properties. getProperty ("driverClassName"); // create a connection in a loop and put it in the connection pool for (int I = 0; I <10; I ++) {// create the Connection object final Connection conn = Drive RManager. getConnection (properties. getProperty ("url"), properties. getProperty ("user"), properties. getProperty ("pass"); // Add the created sub-connection object to the connection pool // use the dynamic proxy to process the close operation to obtain the connection object and return the effect of the Connection Pool connections. add (Connection) Proxy. newProxyInstance (JdbcPoolProxy. class. getClassLoader (), new Class [] {Connection. class}, new InvocationHandler () {@ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Thro Wable {// if (! Method. getName (). equals ("close") {// execute the target method return method. invoke (conn, args);} // if it is the close method // Add the connection object connections to the connection pool. add (conn); System. out. println ("when another connection is used up, a connection pool is returned. The current connection pool has" + connections. size () + "connection objects"); return null ;}}); System. out. println ("adds a connection object to the connection pool. The current connection pool has ======" + connections. size () + "==== connection objects") ;}} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}@ Overridepublic Connection getConnection () throws SQLException {// declare the Connection object Connection conn = null; // determine whether there is a Connection object in the Connection pool if (connections. size ()> 0) {// retrieve the connection object conn = connections from the connection pool. removeFirst (); System. out. println ("A connection object is occupied, and the connection pool also has ========" + connections. size () + "Connections");} return conn ;}@ Overridepublic Connection getConnection (String username, String password) throws SQLException {// TODO Auto-generated method stubreturn null ;} @ Overridepublic PrintWriter getLogWriter () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic void setLogWriter (PrintWriter out) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic void setLoginTimeout (int seconds) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic int getLoginTimeout () throws SQLException {// TODO Auto-generated method stubreturn 0;} @ Overridepublic Logger getParentLogger () throws writable {// TODO Auto-generated method stubreturn null;} @ Overridepublic
   
    
T unwrap (Class
    
     
Iface) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic boolean isWrapperFor (Class
     Iface) throws SQLException {// TODO Auto-generated method stubreturn false ;}}
    
   
  
 
The following is the test code.
Package tk. dong. connectionPool. test; import java. SQL. connection; import java. SQL. SQLException; import org. junit. test; import tk. dong. connection. util. jdbcPoolProxy; public class implements {@ Testpublic void test () throws SQLException {// create a connection pool object JdbcPoolProxy jdbcPoolProxy = new JdbcPoolProxy (); // retrieve the connection jdbcPoolProxy from the connection pool. getConnection (); jdbcPoolProxy. getConnection (); jdbcPoolProxy. getConnection (); jdbcPoolProxy. getConnection (); Connection conn = jdbcPoolProxy. getConnection (); conn. close (); jdbcPoolProxy. getConnection (); jdbcPoolProxy. getConnection ();}}

The running result is as follows:

A connection object is added to the connection pool. The current connection pool has a connection object ====== connection object, the current connection pool has a connection object. A connection object is added to the connection pool, the current connection pool has a connection object. A connection object is added to the connection pool, the current connection pool has a connection object. A connection object is added to the connection pool, the current connection pool has =====5 ====== connection object. A connection object is added to the connection pool, the current connection pool has a connection object. A connection object is added to the connection pool, the current connection pool has a connection object. A connection object is added to the connection pool, the current connection pool has ==== 8 ==== connection object. A connection object is added to the connection pool, the current connection pool has a connection object. A connection object is added to the connection pool, the current connection pool has =====10 ==== connection objects, and a connection object is occupied, there is also a connection pool ========= 9 connections where a connection object is occupied, there is also a connection pool ========= eight connections with a connection object being occupied, there is also a connection pool ========= 7 connections where a connection object is occupied, there is also a connection pool ========= 6 connections, and a connection object is occupied, the connection pool is also ========== five connections and one connection is used up, and a connection pool is returned. There are 6 connection objects in the current connection pool, and one connection object is occupied, the connection pool also has five connections, one of which is occupied, and the connection pool also has four connections
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.