Learn about database connection pooling

Source: Internet
Author: User
Tags connection pooling

The database connection pool is the factory of the Connection object:

Because a physical connection is opened when a database connection is created, performance is degraded when it is closed when it is finished. So we will use the database connection pool, in the database connection pool we will create a certain number of connections, when we

To connect to the database, remove the connection from the pool and put it back in the pool when it is exhausted. If the connection in the pool is used. Will wait, and if the wait time is exceeded, the exception will be reported.

For database connection pooling. We can use Apache's Basicdatasource, a class that implements the DataSource interface primarily. For the use of this class we want to put: Commons.pool.jar,commons.dbcp.jar,commons.collections.jar.

1  PackageCom.devil.util;2 3 Importjava.sql.Connection;4 Importjava.sql.SQLException;5 6 ImportOrg.apache.commons.dbcp.BasicDataSource;7 8  Public classDatabasepool {9     Ten     Private Static FinalDatabasepool instance =NewDatabasepool (); OneBasicdatasource ds =NewBasicdatasource (); A     PrivateDatabasepool () { - Ds.setpassword (Xmldateparse.getpassword ()); - Ds.seturl (Xmldateparse.geturl ()); the Ds.setusername (Xmldateparse.getusername ()); - Ds.setdriverclassname (Xmldateparse.getdriver ()); -          -         //set the Max data connection pool connection active Count +Ds.setmaxactive (10); -         //set the initial connection size +Ds.setinitialsize (2); A         //set the min idle size atDs.setminidle (2); -         //set the max wait mins -Ds.setmaxwait (2000); -          -     } -      PublicConnection getconnection () { in          -Connection conn =NULL; to         Try { +conn =ds.getconnection (); -}Catch(SQLException e) { the e.printstacktrace (); *         } $         Panax Notoginseng         returnConn; -     } the      +      Public StaticDatabasepool getinstance () { A         returninstance; the     } +  -      Public voidClose () { $             Try { $                 if(ds! =NULL){ - ds.close (); -                 } the}Catch(SQLException e) { -             }Wuyi     } the}

Learn about database connection pooling

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.