Java database connection-JNDI method-configure the Tomcat6.0 connection pool

Source: Internet
Author: User

6. Configure the Tomcat6.0 connection pool in JNDI Mode
The basic idea of the database connection pool is to create a "buffer pool" for the database connection ". A certain number of connections are put in the buffer pool in advance. When you need to establish a database connection, you only need to extract one from the "buffer pool" and put it back after use. We can set the maximum number of connections in the connection pool to prevent endless connections to the database. More importantly, we can monitor the number and usage of database connections through the connection pool management mechanism to provide a basis for system development, testing, and performance adjustment.
 
Advantages of database connection pool technology:
A) Resource Reuse
Database connections are reused to avoid high performance overhead caused by frequent creation and release of connections. On the basis of reducing system consumption, on the other hand, it also improves the stability of the system Runtime Environment (reducing memory fragments and the number of temporary database processes/Threads ).
 
B) Faster system response speed
During database connection pool initialization, several database connections have been created and placed in the pool for backup. The connection Initialization is complete. For service request processing, the existing available connections are used directly to avoid the time overhead during database connection initialization and release, thus reducing the overall response time of the system.
 
C) new resource allocation methods
For systems with multiple applications sharing the same database, database connection pool technology can be implemented through database connection configuration at the application layer. A few years ago, it may be a new topic. For the current business system, if the application of the connection pool has not been taken into account in the design, then ....... Add this part in the design document. Limit the maximum number of available database connections of an application to prevent an application from occupying all database resources exclusively.
 
D) Unified connection management to avoid database connection leaks
In the more complete implementation of the database connection pool, the occupied connections can be forcibly withdrawn Based on the preset timeout settings. This avoids the possibility of resource leakage during conventional database connection operations.
 
 
General steps:
6.1 configure the JNDI Resource
The configuration of the database connection pool since tomcat 5.5 is different from the previous one. We do not recommend that you configure it in server. xml,
It is better to configure in % atat_home % \ webapps \ yourApp \ META-INF \ context. xml.
Instead of the previous version % atat_home % \ conf \ context. xml file. In this way, the connection pool can be configured separately for different web applications, and Tomcat will automatically reload. Of course, you can also change the context. xml file under % atat_home % \ conf to configure the connection pools of all web applications in a unified manner.
 
Because server. xml is a resource that cannot be dynamically reloaded, once the server is started, you have to restart the server to reload the file. The context. xml file is not, and the tomcat server regularly scans the file. Once the file is changed (the timestamp is changed), the file is automatically reloaded without restarting the server.
 
Note: The following error is reported: Cannot create JDBC driver of class ''for connect URL 'null'
 
Context. xml content:
 
<! -- The contents of this file will be loaded for each web application -->
<Context>
<Resource name = "jdbc/db" auth = "Container"
Type = "javax. SQL. DataSource"
MaxActive = "30" maxIdle = "10" maxwait= "10000"
DriverClassName = "com. mysql. jdbc. Driver"
Url = "jdbc: mysql: // localhost: 3306/mydb"
Username = "root" password = "root"/>
<WatchedResource> WEB-INF/web. xml </WatchedResource>
<WatchedResource> WEB-INF/struts-config.xml </WatchedResource>
</Context>
 
 
Instructions:
The name is the name of the current data source JNDI, which can be set at will;
Auth is the authentication method;
Type resource type;
 
MaxActiv is the maximum number of activated connections in the connection pool. If it is set to 0, there is no limit;
MaxIdle indicates the maximum number of idle connections in the connection pool and the maximum idle time of the database connection. Idle time exceeded,
The database connection is marked as unavailable and then released. If it is set to 0, there is no limit;
MaxWait is the maximum wait time for a connection, in milliseconds. If the wait time is exceeded, an exception is thrown. If it is set to-1, there is no limit .;
 
DriverClassName is the driver reference;
The url is the connection address of the database;
Username is a user name of the database;
Password is the password of username;
 
The <WatchedResource> label defines the resource files that the server monitors during runtime. The server determines whether to modify the files based on the time stamps before and after the changes. If the changes occur, the file is reloaded.
 
6.2 configure web. xml
This step does not seem necessary and can be omitted.
 
Web. xml content:
 
<Resource-ref>
<Description> DB Connection </description>
<Res-ref-name> jdbc/db </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>
 
Note: The content entered in <res-ref-name> must be consistent with the JNDI Name mentioned above, that is, the value of the name attribute of the <Resource> label in context. xml is the same. Although the content of the <res-type> tag is the same as that of the <Resource> tag, it is essential. The <res-auth> tag may be omitted. The default permission is Container.
 
6.3 connect to the database for login verification
 
General steps:
A) obtain the context content loaded by the current server. Context context = new InitialContext ();
Here is the context content mentioned in step 1.
The loading priority is conf/server. in xml, <Resource> tag content-> conf/context. in xml, the <Resource> label content-> conf/Catalina/Host Name (e.g. localhost)/project name. xml
 
Note: conf/Catalina/Host Name (e.g. localhost)/project name. xml is deployed by the project directory/WebRoot/META-INF/context. xml generation (conf/Catalina/Host Name (e.g. localhost)/project name. xml and WebRoot/META-INF/context. the xml content is the same, and the name is set to context. xml becomes the project name. xml ).
 
B) import two class packages: commons-dbcp and commons-pool (from apache)
Javax. SQL. dataSource is an interface. Tomcat uses the org class in the commons-dbcp package. apache. commons. dbcp. is BasicDataBase used as its implementation class, or is it associated with the instance creation class ?? (Not familiar with the process ).
 
Context ctx = new InitialContext ();
DataSource ds = (DataSource) ctx. lookup ("java: comp/env/jdbc/db ");
Or
Context ctx = new InitialContext ();
Context envCtx = (Context) ctx. lookup ("java: comp/env ");
DataSource ds = (DataSource) envCtx. lookup ("jdbc/db ");
 
Key code:
 
Private static Connection conn;
Private static PreparedStatement stmt;

If (conn = null | conn. isClosed ())
{
String name = "java: comp/env/jdbc/db ";

Context context = new InitialContext ();
DataSource data = (DataSource) context. lookup (name );

Conn = data. getConnection ();
Stmt = conn. prepareStatement (SQL );
}

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.