Custom simple JDBC Connection pooling and testing

Source: Internet
Author: User
Tags connection pooling

 PackageCom.demo.myDataSource;Importjava.sql.Connection;Importjava.util.LinkedList;ImportJavax.sql.DataSource;Importcom.demo.jdbc.jdbcUtils;/** * @authorAdministrator * Custom connection pooling*/ Public classmyDataSource {//Create a collection as a pool for storing connection objects    StaticLinkedlist<connection> pool =NewLinkedlist<connection>(); Static{        //Initialize connection pool (default initialization 3)         for(inti = 0;i<3;i++) {Connection conn; Try{conn=jdbcutils.getconnection ();            POOL.ADDLAST (conn); } Catch(Exception e) {e.printstacktrace (); }                }            }    //Get Connections     Public StaticConnection getconnection () {//take a connection connection object from the Pool collection        if(Pool.isempty ()) {//If the pool is empty, add 2 additional connection objects             for(inti = 0;i<2;i++) {Connection conn; Try{conn=jdbcutils.getconnection ();                POOL.ADDLAST (conn); } Catch(Exception e) {e.printstacktrace (); }}} System.out.println ("Connection objects obtained from the connection pool"); //is not empty, the first connection object is returned        returnPool.removefirst (); }    //Release connection (return connection)     Public Static voidreleaseconnection (Connection conn) {pool.addlast (conn); System.out.println ("Connection has been returned"); }    }
 PackageCom.demo.myDataSource;Importjava.sql.Connection;Importjava.util.LinkedList;ImportJavax.sql.DataSource;Importcom.demo.jdbc.jdbcUtils;/** * @authorAdministrator * Custom connection pooling*/ Public classmyDataSource {//Create a collection as a pool for storing connection objects    StaticLinkedlist<connection> pool =NewLinkedlist<connection>(); Static{        //Initialize connection pool (default initialization 3)         for(inti = 0;i<3;i++) {Connection conn; Try{conn=jdbcutils.getconnection ();            POOL.ADDLAST (conn); } Catch(Exception e) {e.printstacktrace (); }                }            }    //Get Connections     Public StaticConnection getconnection () {//take a connection connection object from the Pool collection        if(Pool.isempty ()) {//If the pool is empty, add 2 additional connection objects             for(inti = 0;i<2;i++) {Connection conn; Try{conn=jdbcutils.getconnection ();                POOL.ADDLAST (conn); } Catch(Exception e) {e.printstacktrace (); }}} System.out.println ("Connection objects obtained from the connection pool"); //is not empty, the first connection object is returned        returnPool.removefirst (); }    //Release connection (return connection)     Public Static voidreleaseconnection (Connection conn) {pool.addlast (conn); System.out.println ("Connection has been returned"); }    }

Custom simple JDBC Connection pooling and testing

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.