Connection pool configuration and usage for Tomcat under MySQL

Source: Internet
Author: User
Tags try catch

Recently maintained a project has a problem, the final analysis is stuck in the database connection pool, and then did some learning.

First write my own method, and then the other online no success of the method.

1, first of all, of course, the MySQL jar package is placed in the Lib directory, toncat or their own projects under the Lib can.

2, in the Tomcat conf directory server.xml Add the following content, to be added between <Host></Host>

<context path= "/myexample" docbase= "Myexample" crosscontext= "true" >
<resource name= "Jdbc/mysql" auth= "Container" type= "Javax.sql.DataSource"
Username= "Root"
password= "[Email protected]"
Maxactive= "850"
maxidle= "80"
Removeabandoned= "true"
removeabandonedtimeout= "5"
Driverclassname= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://localhost:3306/shangman?autoreconnect=true"
minevictableidletimemillis= "4000"
Timebetweenevictionrunsmillis= "/>"
</Context>

Or, under Tomcat's Conf\catalina\localhost, create a new project name. xml file (recommended this way)

Inside the content is

<?xml version= "1.0" encoding= "UTF-8"?>
<context path= "/myexample" docbase= "Myexample" crosscontext= "true" >
<resource name= "Jdbc/mysql" auth= "Container" type= "Javax.sql.DataSource"
Username= "Root"
password= "[Email protected]"
Maxactive= "850"
maxidle= "80"
Removeabandoned= "true"
removeabandonedtimeout= "5"
Driverclassname= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://localhost:3306/shangman?autoreconnect=true"
minevictableidletimemillis= "4000"
Timebetweenevictionrunsmillis= "/>"
</Context>

Here are some of the more important parameters to explain:
Name: Represents your connection pool, which is the address you want to access the connection pool
Auth: Is the connection pool management right attribute,
Container represents container Management
Type: Is the kind of object
Driverclassname: Is the database-driven name

URL: Is the address of the database
Username: is the user name of the login database
Password: is the password of the login database
Maxidle, maximum idle number, maximum idle time for database connections. When the idle time is exceeded, the database connection is marked as unavailable and then released. Set to 0 to indicate no limit.
Maxactive, the maximum number of database connections for the connection pool. Set to 0 to indicate no limit.
Maxwait, the maximum connection wait time is established. If this time is exceeded, the exception will be received. Set to-1 to indicate no limit.
Maxactive: Maximum connection database connection, set 0 for No Limit
Maxidle: Maximum number of waiting connections, set 0 for No Limit
Maxwait: Maximum wait milliseconds, in MS, error message over time
The maxactive is usually set to the possible concurrency.

Then the configuration is complete and the following is used.

try {
Initializing the Lookup namespace
Context initcontext = new InitialContext ();
Context Envcontext = (context) initcontext.lookup ("java:/comp/env");
Find DataSource
DataSource ds = (DataSource) envcontext.lookup ("Jdbc/mysql");
Connection Connection = Ds.getconnection ();

...... After that, it is the same as using normal database connections.

}catch (Exception ex) {

Out.println (Ex.tostring ());

}

It is best to wrap it with a try catch.

The following said on the net to see the other did not succeed, and some say in the project directory of the Meta-inf directory to build a content.xml put on the above content, I tried, did not succeed.

There is also the need to add between the applications under the Web. XML:

<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/sample</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

I tried, plus this has no effect.

Unsuccessful or not affected by the possibility and version, I did not try them to say the version of the 6.0, I used the 8.* version.

Connection pool configuration and usage for Tomcat under MySQL

Related Article

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.