Tomcat database connection pool settings and Applications

Source: Internet
Author: User
1. Drive the database Program In the common/lib directory of Tomcat;

2. Set the data source in server. xml. Take MySQL database as an example, as follows:
Add the following content to the <globalnamingresources> </globalnamingresources> node,
<Resource
Name = "JDBC/dbpool"
Type = "javax. SQL. datasource"
Password = "root"
Driverclassname = "com. MySQL. JDBC. Driver"
Maxidle = "2"
Maxwait = "5000"
Username = "root"
Url = "JDBC: mysql: // 127.0.0.1: 3306/test"
Maxactive = "4"/>
Attribute description: name, data source name, usually in the format of "JDBC/XXX;
Type, "javax. SQL. datasource ";
Password: the password of the database user;
Driveclassname, database driver;
Maxidle, maximum number of idle instances, and maximum idle time for database connection. Database Connection exceeds idle time
The connection is marked as unavailable and then released. If it is set to 0, there is no limit.
Maxactive: the maximum number of database connections in the connection pool. If it is set to 0, there is no limit.
Maxwait, maximum connection wait time. If this time is exceeded, an exception occurs. Set to-1
Unlimited.

3. Configure the data source reference in the web. xml file of your web application, as shown in the following code:
Add the following content to the <web-app> </Web-app> node,
<Resource-ref>
<Description> MySQL db connection pool </description>
<Res-ref-Name> JDBC/dbpool </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
<Res-sharing-scope> retriable </RES-sharing-scope>
</Resource-ref>
Sub-node Description: Description, description information;
Res-ref-name. For details, refer to the data source name. The attribute name in the preceding step;
Res-type, resource type, "javax. SQL. datasource ";
Res-auth, "container ";
Res-sharing-scope, "retriable ";

4. Set the data source link in context. xml of the Web application as follows:
Add it to the <context> </context> node,
<Resourcelink
Name = "JDBC/dbpool"
Type = "javax. SQL. datasource"
Global = "JDBC/dbpool"/>
Attribute description: name, which is the same as the attribute name value of step 2 and step 2, and the res-ref-name value of the subnode;
Type, also take "javax. SQL. datasource ";
Global, with the same name value.
 
Now, the settings are complete. The following describes how to use the database connection pool.
1. Create a connection pool class, dbpool. Java, used to create a connection pool,CodeAs follows:
Import javax. Naming. context;
Import javax. Naming. initialcontext;
Import javax. Naming. namingexception;
Import javax. SQL. datasource;

Public class dbpool {
Private Static datasource pool;
Static {
Context Env = NULL;
Try {
ENV = (context) New initialcontext (). Lookup ("Java: COMP/ENV ");
Pool = (datasource) ENV. Lookup ("JDBC/dbpool ");
If (pool = NULL)
System. Err. println ("'dbpool 'is an unknown datasource ");
} Catch (namingexception ne ){
Ne. printstacktrace ();
}
}
Public static datasource getpool (){
Return pool;
}
}

2. dbpool is used in the class or JSP page for database operations. getpool (). getconnection (), get a connection object, you can perform database operations, and finally don't forget to call the close () method for the connection object. Note: This connection will not be closed here, instead, the connection is put back into the 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.