JDBC connection pool technology, jdbc connection
I. Connection Pool technology (1) the basic process of accessing the database through JDBC in Java is as follows:
1. Load the database driver;
2. Establish a database connection through JDBC;
3. Access the database and execute SQL statements;
4. Disconnect the database.
(2) In Web application development, there are many problems when using this mode to access the database. To solve these problems, you can use the database connection pool technology. The connection pool stores a certain number of database connection objects in a collection object. When a program needs to use a database connection, it requests to get an idle connection from the pool. After the program is used, it will put the connection back into the pool for reuse. By reusing the connection method, the connection pool reduces the overhead of creating a connection and significantly improves the database access efficiency of the system.
(3) The connection pool technology can be used to solve the above problems. The connection pool is actually a set object that stores a certain number of database connection objects.
(4) In JSP technology, the connection pool is generally used by the data source. A data object is registered as a JNDI resource of the web server. The program should be used to obtain the data source object through JDNI, then, the database connection is obtained through the data source object, and the connection pool provides a physical connection for the data source.
2. Use method (1) Open the c: \ tomcat \ conf \ context. xml file in a text editor, and add the following data source configuration code in the <Context> </Context> element:
(2) The basic process for an application to connect to a database through the data source is to use the context object to obtain a data source object based on the JNDI name and get a connection through the data source object.
(3) The full name of JNDI is Java naming and directory interface (java naming and directory interface), which is a java naming and directory access interface provided by sun.
The main method is as follows: 1. javax. naming. the lookup method provided by the Context method can search for a named object in the JNDI Context and return an object 2. javax. naming. initialContext3.javax. SQL. the DtaSource data source is a Factory-class Factory Visteon Datasource object associated with a physical connection chicken wings. You must specify a connection pool for the DtaSource data source. The Connection Pool provides a physical connection for the JNDI data source;
Iii. instance analysis
(1) write two JSP pages, one from the connection pool to obtain the titles table for querying the pubs database, and the other from the JDBC driver to obtain the titles table for querying the pubs database, each JSP page executes a query statement 500 times and records the related connection information through the query analyzer to compare the database access efficiency of the two database connection methods.
The procedure is as follows:
Step 2: Define the connection pool and data source. Open the c: \ tomcat \ conf \ context. xml file in a text editor, and add the configuration code in the <Context> </Context> element (the content is not described above)
Step 2: Create a JSP page and access the pubs/titles table 2nd times using the connection pool.
<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Step 2: Create a JSP page named exam623.jsp and use the JDBC driver to directly access the pubs/titles table for 3rd Times.
<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
(2) This does not need to be the majority, and the effect is visible!
Note: We hope you can indicate this when reprinting!
What is the JDBC connection pool?
Database connection is a key, limited, and expensive resource, which is particularly prominent in the Web applications of multiple users. Database connection management can significantly affect the scalability and robustness of the entire application, and affect the performance indicators of the program. The database connection pool is designed to address this issue. The database connection pool is responsible for allocating, managing, and releasing database connections. It allows applications to repeat an existing database connection instead of reestablishing one; release the database connection with idle time exceeding the maximum idle time to avoid database connection omissions caused by the absence of the database connection. This technology can significantly improve the performance of database operations.
Open-source database connection pools in Java include the following:
1. C3P0 C3P0 is an open-source JDBC Connection pool. It is released together with Hibernate in the lib directory, including the DataSources object of the Connection and Statement pool that implements the jdbc3 and jdbc2 extension specifications.
2. Proxool this is a Java SQL Driver and provides connection pool encapsulation for other types of drivers you choose. It can be easily transplanted to the existing code. Completely configurable. Fast, mature, and robust. You can transparently Add the connection pool function to your existing JDBC driver.
3. Jakarta DBCP is a database connection pool dependent on the Jakarta commons-pool Object pool mechanism. DBCP can be used directly in applications.
4. DDConnectionBroker is a simple and lightweight database connection pool.
5. DBPool is an efficient and easy-to-configure database connection pool. In addition to supporting the functions of the connection pool, it also includes an object pool that allows you to develop a database connection pool that meets your own needs.
6. XAPool is an XA database connection pool. It implements javax. SQL. XADataSource and provides a connection pool tool.
7. Primrose is a Java-developed database connection pool. Currently, the supported containers include Tomcat 4 & 5, Resin3 and JBoss3. it also has an independent version that can be used in the application without running in the container. Primrose uses a web interface to control SQL processing tracking, configuration, and dynamic pool management. In case of heavy load, You can process the Connection Request queue.
8. SmartPool is a connection pool component that imitates the features of the Application Server Object pool. SmartPool can solve some critical problems such as connection leaks, connection blocking, and opened JDBC objects such as Statements and PreparedStatements. smartPool supports multiple pools, automatically closes associated JDBC objects, detects connection leaks after the set time-outs, and tracks connection usage, forcibly enable connections that are least recently used, and pack SmartPool into an existing pool.
9. MiniConnectionPoolManager is a lightweight JDBC database connection pool. It only requires Java1.5 (or higher) and does not depend on third-party packages.
10. BoneCP is a fast and open-source database connection pool. Help you manage data connections so that your applications can access the database more quickly. It is 25 times faster than C3P0/DBCP connection pool.
Java web development uses Jdbc database connection pool technology
Without using SSH, developers focus on writing JAVA code and logic.