First, why use data sources and connection pooling
The applications that are now being developed are basically data-based and need to be connected to the database frequently. If you connect to the database each time and then close, this performance is bound to be limited. So, we must try to reuse it.
The connection to the database. Therefore, the concept of data source and connection pool is proposed for this case. You can use data sources and connection pooling to achieve the purpose of reusing database connections.
Ii. data sources and connection pooling concepts
In fact, data sources and connection pooling are two different concepts. Some people will confuse them.
The data source is used to connect to the database and obtain the connection object, which is represented in Java using the Javax.sql.DataSource interface. With the data source, we do not need to write another connection data code, directly from the data source to get a connection. And, regardless of the database used, the database connection object is obtained in the same way. So how does the data source object get it? Typically created by a container, we use Jndi in the program to get it. Connection objects created from the data source can be automatically placed in the connection pool for unified management
Connection pooling is used to manage connection objects, connection pooling can be obtained from the data source, the connection pool can have several database connection objects, these connection objects can be reused. When an application needs to connect, it requests the connection pool, and if there is an idle connection in the connection pool, it is assigned to the application, and if not, it may need to wait in the waiting queue. And if you get a connection object from the connection pool and wait until it is finished, you can return the connection to the connection pool by calling the close () method of connection, so that the connection object is programmed to be idle and can wait for the next request. Connection objects in the connection pool remain connected to the database so that frequent connections and shutdowns are avoided. However, these connection objects occupy a certain amount of memory space. Therefore, we want to determine the maximum number of connections in the connection pool based on the concurrency of the database and the hardware conditions of the server.
Third, the data source and connection pool, one is used to create the connection object, one is to manage the connection object
Iv. Configuring data Source 4.1 in Tomcat the steps to use a data source in Tomcat are as follows:
- (1) Adding database driver files to Tomcat
- (2) configuring Tomcat's Conf/context.xml
<Context><Resourcename= "Jdbc/books"Auth= "Container"type= "Javax.sql.DataSource"maxactive= "+"Maxidle= "+"maxwait= "10000"username= "PBDEVJ"Password= "pwd1234"Driverclassname= "Oracle.jdbc.OracleDriver"URL= "Jdbc:oracle:thin: @localhost: 1521:orcl"/></Context>
Property name |
Description |
Name |
Specifies the Jndi name of the resource |
Auth |
Specify Manager for Management resource (Container: Created by container and Management | Application: Created and managed by web apps) |
Type |
Specifies the Java class to which resource belongs |
Maxactive |
Specifies the maximum number of database connections that are active in the connection pool |
Maxidle |
Specifies the maximum number of database connections that are idle in the connection pool |
Maxwait |
Specifies the maximum amount of time that a connection in a connection pool is idle Will throw an exception with a value of-1, indicating that it can wait indefinitely |
- (3) Configure the application's Web. xml file (optional)(note: The new version of Tomcat does not need to be configured as follows)
- (4) using Jndi to get connection objects
Import Javax.naming.Context; Import Javax.naming.InitialContext; Import Javax.sql.DataSource; // ... .. New = (DataSource) ic.lookup ("Java:comp/env/jdbc/books"= Source.getconnection ();
JDBC Five data sources and data pools (Web Foundation learning Note 11)