JDBC Database connection Pooling Technology

Source: Internet
Author: User
Tags connection pooling manage connection time in milliseconds

In JDBC, getting a connection or freeing a resource is a two process that consumes system resources very much, and in order to solve such performance problems, the connection pooling technique is often used to share the connection. This way we don't need to create a connection, release the connection each time, and these operations are given to the connection pool.

The concept of pooling is used to manage connection so that connection can be reused. Once you have a connection pool, you do not have to create the connection yourself, but instead get the connection object by connecting pools. When connection is finished, calling connection's close () method does not really close the connection, but instead returns the connection to the connection pool. The connection pool can continue to retain this connection for other users to use.

Java provides a common interface for database connection pooling: Javax.sql.DataSource, each vendor needs to have its own connection pool to implement this interface, so that the application can easily switch the connection pool of different vendors. There are three common connection pools: DBCP, c3p0, and Druid

DBCP Connection Pool

DBCP is an open source connection pool, one of the Apache common members, also more common in enterprise development, Tomcat's built-in connection pool.

Before using DBCP, 2 packages are introduced: Commons-dbcp.jar and Commons-pool.jar.

1  PackageCom.dbcp.demo;2 ImportJava.sql.Connection;3 ImportJava.sql.PreparedStatement;4 ImportJava.sql.SQLException;5 ImportJavax.sql.DataSource;6 ImportOrg.apache.commons.dbcp.BasicDataSource;7 8  Public classDEMODBCP {9     //parameters of the connection databaseTen      Public Static FinalString DRIVER = "Com.mysql.jdbc.Driver"; One      Public Static FinalString URL = "Jdbc:mysql://localhost:3306/mybatis"; A      Public Static FinalString USERNAME = "Root"; -      Public Static FinalString PASSWORD = "123456"; -  the     //Create Connection Pool -      Public StaticBasicdatasource DataSource =NewBasicdatasource (); -  -     //Static code block +     Static{ -         //Basic configuration of Connection pool objects +Datasource.setdriverclassname (DRIVER);//driver for the database to be connected ADatasource.seturl (URL);//Specify the address of the database to connect to atDatasource.setusername (USERNAME);//Specify the user name to connect to -Datasource.setpassword (PASSWORD);//Specify the password to connect to the database -  -         //There should also be a statement to set the number of connections -} -  in     //Return connection pool object -      Public StaticDataSource Getdatasource () { to         returnDataSource; +} -  the     //test Connection Pool *      Public Static voidMain (string[] args)throwsSQLException { $         //Get Connection poolPanax NotoginsengDataSource DataSource = Demodbcp.getdatasource (); -         //Get connections from the connection pool theConnection conn = Datasource.getconnection (); +         //Prepare SQL statements AString sql = "INSERT into person (LastName, FirstName, Address, age) Values" + the" (?, ? ,?, ?)"; +         //Get Presparedstatement object -PreparedStatement pstmt = conn.preparestatement (sql); $         //Set parameters $Pstmt.setstring (1, "Zhang"); -Pstmt.setstring (2, "three"); -Pstmt.setstring (3, "Ethiopia"); thePstmt.setint (4, 24); -         //Perform CRUD operationsWuyiPstmt.execute (); the         //Close connection -Conn.close (); Wu} -}

DBCP Common configuration options

Required Configuration Items
Driverclassname Database driver name
Address of the URL database
Username User Name
Password Password
Basic items
Maxactive Maximum number of connections
InitialSize How many connection connection objects are initialized in a connection pool
Extension items
Maxwait timeout wait time in milliseconds

JDBC Database connection Pooling Technology

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.