The setup and application of Tomcat database connection pool

Source: Internet
Author: User
Tags auth join sql mysql resource mysql database tomcat
Data | database | database connection

1. Place the database driver jar file in the common/lib of Tomcat;

2. Set up the data source in Server.xml, take the MySQL database for example, as follows:
Join in 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 names, usually "jdbc/xxx" format;
Type, "Javax.sql.DataSource";
Password, database user password;
Driveclassname, database driven;
Maxidle, maximum idle number, maximum idle time of database connection. More than idle time, the database company
The connection is marked as unavailable and then released. A set of 0 means no limit.
Maxactive, the maximum number of database connections for the connection pool. A set of 0 means no limit.
Maxwait, the maximum connection waiting time is established. If you exceed this time, you will receive an exception. Set to-1 indicates
Unlimited.

3. Set the data source reference in the Web.xml of your Web application, as follows:
Join in 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>Shareable</res-sharing-scope>
</resource-ref>
Sub-node Description: description, descriptive information;
Res-ref-name, refer to the name of the data source, the attribute name of the previous step;
Res-type, resource type, "Javax.sql.DataSource";
Res-auth, "Container";
Res-sharing-scope, "shareable";

4. Set the data source link in the context.xml of the Web application as follows:
Join in the <Context></Context> node,
<resourcelink
Name= "Jdbc/dbpool"
Type= "Javax.sql.DataSource"
Global= "Jdbc/dbpool"/>
Attribute Description: Name, the property name value with steps 2nd and 3rd, and the Res-ref-name value of the child node;
Type, also take "Javax.sql.DataSource";
Global, with the name value.

At this point, the settings are complete, and the following is how to use the database connection pool.
1. Set up a connection pool class, Dbpool.java, to create a connection pool with the following code:
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 a unknown DataSource");
catch (Namingexception ne) {
Ne.printstacktrace ();
}
}
public static DataSource Getpool () {
return pool;
}
}

2. In the class or JSP page where you want to use the database operation, you can use Dbpool.getpool (). getconnection () to get a connection object and then do the database operation, and finally don't forget to call the Close () method on the Connection object , note: Instead of closing the connection, this 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.