Java Web----Database connection pool

Source: Internet
Author: User

Pool parameters (all pool parameters have default values):

Initial Size: 10 x

Minimum number of idle connections: 3

Increment: Smallest unit created at a time (5)

Maximum number of idle connections: 12

Maximum number of connections: 20

Maximum wait time: 1000 milliseconds

Four connection parameters

Connection pooling also uses four connection parameters to complete the creation of connection objects!

The implemented interface

Connection pooling must be implemented: Javax.sql.DataSource Interface!

The connection object returned by the connection pool, its close () method is different! The close () call it is not closed, but the connection is returned to the pool!

1 Concepts of database connection pooling

Use pools to manage connection, which can be reused with connection. With the pool, we don't have to create connection ourselves, but instead we get the connection object through the pool. When connection is finished, calling connection's close () method does not actually close the connection, but instead connection "returns" to the pool. The pool will be able to use this connection object again.



2 JDBC Database connection pool interface (DataSource)

Java provides a common interface for database connection pooling: Javax.sql.DataSource, each vendor can have its own connection pool to implement this interface. So the application can easily switch the connection pool of different vendors!

3 Custom connection pooling (Itcastpool)

Analysis: Itcastpool needs to have a list to hold the connection object. Create 5 connection objects in the Itcastpool constructor in the list! When the employing person calls the Itcastpool getconnection (), then a return is taken from the list. Throws an exception when no connection is available in the list.

We need to enhance the close () method of the connection, so we need to customize the Itcastconnection class to decorate the connection! The close () method is enhanced. Because the connection needs to be "returned" to the pool when the close () method is called, the Itcastconnection class needs to have a reference to the pool object, and the pool class also provides a "return" method.


public class  Itcastpool implements DataSource {private static Properties props = new Properties ();p rivate list<connection> List = New arraylist<connection> (); static {InputStream in = ItcastPool.class.getClassLoader (). getResourceAsStream (" Dbconfig.properties "); try {props.load (in); Class.forName (Props.getproperty ("Driverclassname"));} catch (Exception e) {throw new RuntimeException (e);}} Public Itcastpool () throws SQLException {for (int i = 0; i < 5; i++) {Connection con = drivermanager.getconnection (prop S.getproperty ("url"), Props.getproperty ("username"), Props.getproperty ("password")); Itcastconnection Conwapper = New Itcastconnection (con, this); List.add (Conwapper);}} public void Add (Connection con) {list.add (con);} Public Connection getconnection () throws SQLException {if (list.size () > 0) {return list.remove (0);}    throw new SQLException ("Not Connected");} ......}

public class Itcastconnection extends Connectionwrapper {private Itcastpool pool;public itcastconnection (Connection con , Itcastpool Pool) {super (con); this.pool = pool;} @Overridepublic void Close () throws SQLException {Pool.add (this);}}



Java Web----Database connection pool

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.