Database connection Buffer Pool

Source: Internet
Author: User
Tags commit connection pooling interface
Data | database | Database connection A database buffer pool refers to a physical connection to a database that is cached in memory space, and these database connections can be reused. The database buffer pool is important for improving the performance of Java database applications, especially when the Java database application is running in a middle-tier server environment.

The data buffer pool exists in the middle-tier server environment and can be invoked by different Java applications. The Javax.sql.RowSet package adds support for buffered data sources, which means that the buffer pool buffered database connection can be viewed as a real data source service to use. The RowSet package provides several interfaces for handling database buffer pools, with the main interfaces:
1. DataSource interface: the instance object of the DataSource interface represents the buffered data source service that exists in the middle-tier server. Use it to return the existing database connection in the database buffer pool, the instance object of the DataSource interface is actually a provider of a Jndi service, and before it is used, the Jndi service object must be registered in the middle-tier server environment and bound to a service name. Then it can be invoked by another Java application.
2. ConnectionPoolDataSource interface: This interface can be used to create a physical connection to a database that is buffered from the buffer pool, and it may be invoked by an instance object of the DataSource interface.
3. Pooledconnection interface: This interface represents a buffered database connection, and it defines a getconnection () method that can be used to return an instance object of the Java.sql.Connection interface.
How to manipulate the database buffer pool using classes and interfaces defined by the rowset package, the following JSP code snippet provides a simple, incomplete example

Cases

<%

Context ctx = new InitialContext ();

DataSource ds = (DataSource) ctx.lookup ("Jdbc/employeedb");

A Connection. Connection pooling are done

Internally by the DataSource object.

Connection con = ds.getconnection (jdbc/webdatabase "sa" "");

Do all the work as a single transaction (optional).

Con.setautocommit (FALSE);

The actual work (queries and updates) would go.

Work is do using standard JDBC code as defined in the

Rest of the JDBC API.

Commit the transaction.

Con.commit ();

Close the connection. This returns the underlying physical

Database connection to the pool.

Con.close ();

%>

The above JSP code section of the operating mechanism is as follows

???? 1. First, the program code obtains the initialized Jndi environment and calls the Context.lookup () method to obtain a DataSource object from the Jndi service provider

???? 2. The middle-tier Jndi service provider returns a DataSource object to the current Java application This DataSource object represents the existing buffered data source on the middle-tier service

???? 3, the application calls the DataSource Object Getconnection () method

???? 4, when the DataSource object's Getconnection () method is called, the middle-tier server will query the database connection buffer pool There is no pooledconnection Interface instance Object this Pooledconnection object will be used to establish a physical database connection to the database

???? 5. If a Pooledcoonection object is hit in the buffer pool, the connection buffer pool simply updates the internal buffer connection queue and returns the Pooledconnection object if no ready-made pooledconnection is found within the buffer pool Object then the ConnectionPoolDataSource interface will be used to generate a new Pooledconnection object and return it for use by the application

???? 6\ the middle-tier server invokes the Getconnection () method of the Pooledconnection object to return a Java.sql.Connection object to the current Java application

???? 7. When the middle-tier server invokes the Getconnection () method of the Pooledconnection object, the JDBC database driver creates a connection object and returns it to the middle-tier server

???? 8. The middle-tier server returns the Connection object to the application Java application can assume that the Connection object is an ordinary JDBC Connection object that can interact with the database engine by establishing a de facto connection with the database

???? 9. The close () method of the connection interface can be invoked when the application does not need to use the connection object. Note that in this case the close () method does not turn off the de facto database connection, just frees up the database connection that is occupied by the application, and returns it to the database connection buffer pool, which automatically gives the database connection to the next application in the request queue.



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.