This is the same as that of the enhanced Connection class [override the close method] to retrieve connections from the Connection pool.

Source: Internet
Author: User
Tags savepoint
Packagetk. dong. connection. util; importjava. io. IOException; importjava. io. inputStream; importjava. io. printWriter; importjava. SQL. array; importjava. SQL. blob; importjava. SQL. callableStatement; importjava. SQL. clob; importjava. SQL. connection; I

Package tk. dong. connection. util; import java. io. IOException; import java. io. inputStream; import java. io. printWriter; import java. SQL. array; import java. SQL. blob; import java. SQL. callableStatement; import java. SQL. clob; import java. SQL. connection; I

Package tk. dong. connection. util; import java. io. IOException; import java. io. inputStream; import java. io. printWriter; import java. SQL. array; import java. SQL. blob; import java. SQL. callableStatement; import java. SQL. clob; import java. SQL. connection; import java. SQL. databaseMetaData; import java. SQL. driverManager; import java. SQL. NClob; import java. SQL. preparedStatement; import java. SQL. SQLClientInfoException; import java. SQL. SQLException; import java. SQL. SQLFeatureNotSupportedException; import java. SQL. SQLWarning; import java. SQL. SQLXML; import java. SQL. savepoint; import java. SQL. statement; import java. SQL. struct; import java. util. using list; import java. util. map; import java. util. properties; import java. util. concurrent. executor; import java. util. logging. logger; import javax. SQL. dataSource; // This is the same as the enhanced Connection class [override the close method], which removes the Connection from the Connection pool and places the Connection back to the public class JdbcPool implements DataSource {// creates the Connection pool, linkList and private static upload list are used.
 
  
Connections = new external list
  
   
(); // Create a block through static initialization. When the program is initialized, static {// will be triggered to read the configuration file connected to the database into the stream. InputStream inStream = JdbcPool. class. getClassLoader (). getResourceAsStream ("jdbc. properties "); Properties properties = new Properties (); try {properties. load (inStream); // get the driver used to connect to the database (obtain the Driver Based on the attribute in the property file) Class. forName (properties. getProperty ("driverClassName"); for (int I = 0; I <10; I ++) {// obtain the conn Connection object Connection conn = DriverManager. getConnection (properties. getProperty ("url"), properties. getProperty ("user"), properties. getProperty ("pass"); // This is the operation performed when the connection pool is loaded. Rewrite the close in the connection to MyConnection myConnection = new MyConnection (conn, connections ); // Save the processed connection object to the connection pool connections. add (myConnection); System. out. println ("the connection pool has been added:" + connections. size () + ":::::::::::: link object") ;}} 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 () ;}}// these two methods are used to obtain the Connection from the Connection pool @ Overridepublic Connection getConnection () throws SQLException {// The Connection object Connection conn = null; if (connections. size ()> 0) {// retrieve the first object in the linked list and assign the value to the temporary connection object conn = connections. removeFirst (); System. out. println ("another connection object is taken away: Connection Pool and" + connections. size () + "Connection objects");} 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 SQLFeatureNotSupportedException {// 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;} // enhanced Connection pair class // decorator mode // 1. first, let's take a look at the interface or parent class that needs to be inherited by the enhancement object, and write a class to inherit these interfaces or parent classes. Class MyConnection implements Connection {// 2. Define a variable in the class. The variable type is the type of the object to be enhanced. // Used to receive the private answer list of the Connection Pool
     
      
Connections; // used to receive the Connection object private Connection conn; // The constructor is public MyConnection (Connection conn, LinkedList
      
        Connections) {this. conn = conn; this. connections = connections;} // I only use this method, so I only write this method @ Overridepublic void close () throws SQLException {// Add unused connections to the connection pool connections again. add (conn); System. out. println ("A connection object is used up and the connection pool has been returned. Now there is ===" + connections in the connection pool. size ();} // The following method is not used, so no content is written @ 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;} @ Overridepublic Statement createStatement () throws SQLException {// TODO Auto-generated method stubreturn null ;} @ Overridepublic PreparedStatement prepareStatement (String SQL) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic CallableStatement prepareCall (String SQL) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic String nativeSQL (String SQL) throws SQLException {// TODO Auto-generated method stubreturn null ;} @ Overridepublic void setAutoCommit (boolean autoCommit) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic boolean getAutoCommit () throws SQLException {// TODO Auto-generated method stubreturn false ;} @ Overridepublic void commit () throws SQLException {// TODO Auto-generated method stub} @ Overridepublic void rollback () throws SQLException {// TODO Auto-generated method stub} @ Overridepublic boolean isClosed () throws SQLException {// TODO Auto-generated method stubreturn false;} @ Overridepublic DatabaseMetaData getMetaData () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic void setReadOnly (boolean readOnly) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic boolean isReadOnly () throws SQLException {// TODO Auto-generated method stubreturn false;} @ Overridepublic void setCatalog (String catalog) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic String getCatalog (alog () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic void evaluate (int level) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic int getTransactionIsolation () throws SQLException {// TODO Auto-generated method stubreturn 0;} @ Overridepublic SQLWarning handle () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic void clearWarnings () throws SQLException {// TODO Auto-generated method stub} @ Overridepublic Statement createStatement (int resultSetType, int resultSetConcurrency) throws SQLException {// TODO Auto-generated method stubreturn null ;} @ Overridepublic PreparedStatement partition (String SQL, int resultSetType, int partition) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic CallableStatement prepareCall (String SQL, int resultSetType, int resultSetConcurrency) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic Map
         
           > GetTypeMap () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic void setTypeMap (Map
          
            > Map) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic void setHoldability (int holdability) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic int getHoldability () throws SQLException {// TODO Auto-generated method stubreturn 0;} @ Overridepublic Savepoint setSavepoint () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic Savepoint setSavepoint (String name) throws SQLException {// TODO Auto-generated method stubreturn null ;} @ Overridepublic void rollback (Savepoint savepoint) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic void releaseSavepoint (Savepoint savepoint) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic Statement createStatement (int resultSetType, int limit, int resultSetHoldability) throws SQLException {// TODO Auto-generated method stubreturn null ;} @ Overridepublic PreparedStatement construct (String SQL, int resultSetType, int limit, int resultSetHoldability) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic CallableStatement prepareCall (String, int resultSetType, int limit, int resultSetHoldability) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic PreparedStatement prepareStatement (String SQL, int autoGeneratedKeys) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic PreparedStatement prepareStatement (String SQL, int [] columnIndexes) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic PreparedStatement prepareStatement (String SQL, String [] columnNames) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic Clob createClob () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic Blob createBlob () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic NClob createNClob () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic SQLXML release () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic boolean isValid (int timeout) throws SQLException {// TODO Auto-generated method stubreturn false ;} @ Overridepublic void setClientInfo (String name, String value) throws SQLClientInfoException {// TODO Auto-generated method stub} @ Overridepublic void setClientInfo (Properties properties Properties) throws SQLClientInfoException {// TODO Auto-generated method stub} @ Overridepublic String getClientInfo (String name) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic Properties getClientInfo () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic Array createArrayOf (String typeName, Object [] elements) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic Struct createStruct (String typeName, Object [] attributes) throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic void setSchema (String schema) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic String getSchema () throws SQLException {// TODO Auto-generated method stubreturn null;} @ Overridepublic void abort (Executor executor) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic void setNetworkTimeout (Executor executor, int milliseconds) throws SQLException {// TODO Auto-generated method stub} @ Overridepublic int getNetworkTimeout () throws SQLException {// TODO Auto-generated method stubreturn 0 ;}}
          
         
        
       
      
     
    
   
  
 
Test code
Package tk. dong. connectionPool. test; import java. SQL. connection; import java. SQL. SQLException; import org. junit. test; import tk. dong. connection. util. jdbcPool; public class TestJdbcPool {@ Testpublic void jdbcPool () throws SQLException {// create a connection pool object JdbcPool jdbcPool = new JdbcPool (); // obtain the connection object jdbcPool from the connection pool. getConnection (); // obtain the connection object jdbcPool multiple times. getConnection (); jdbcPool. getConnection (); Connection conn = jdbcPool. getConnection (); // return the used connection object conn. close (); // obtain the connection object jdbcPool again. getConnection (); jdbcPool. getConnection (); jdbcPool. getConnection (); jdbcPool. getConnection ();}}
Test output result
The connection pool has already been added: 1 ::::::::: 2: :::::::::::: link object connection pool has been added: :::::::: 3 :::::::::: connection object connection pool has been added: 4 :::::::::::: the connection object connection pool has been added :::: :::::: 5 ::::::::::::: link object connection pool already added: ::::::: 6 ::::: ::::: the link object connection pool has been added: :::::::: 7 :::::::::: link object connection pool has been added: :::::::::: 8 ::::: ::::: 9 ::::::::::::: link object connection pool already added :::::: :::: a link object and a connection object are taken away: ::::: the connection pool has nine connection objects and another connection object is taken away ::::::: the connection pool also has eight connection objects and one connection object is taken away: the connection pool has seven connection objects and another connection object is taken away ::::::: there are also 6 connection objects in the connection pool. One connection object is used up and has been returned to the connection pool. Now, another connection object in the connection pool is = 7 and has been taken away ::::::: the connection pool also has 6 connection objects and one connection object is taken away: the connection pool has 5 connection objects and another connection object is taken away ::::::: there are four connection objects in the connection pool, and one connection object is taken away: :::: the connection pool has three connection objects.

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.