Connect to database using Configuration connection pool in Tomcat

Source: Internet
Author: User

A: First configure the data source in the Contenx tag in the Conf/context.xml file under Tomcat:

<resource name= "Jdbc/zzz"

Auth= "Container" type= "Javax.sql.DataSource"

Maxactive= "maxidle=" maxwait= "10000"

Username= "sa" password= "sa"

Driverclassname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"

Url= "Jdbc:sqlserver://127.0.0.1:1433;databasename=testdb"/>

Name: Specifies the Jndi name of the resource

Auth: Specifies the manager that manages resource, which has two optional values: Container, Application

Container is a container created and managed application is created and managed by a Web application

Type: Specifies the Java class name to which resource belongs

Maxactive: Specifies the maximum number of connections that are active in the database connection pool, which means unrestricted

Maxidle: Specifies the maximum number of connections that are idle in the database connection pool, which means unrestricted

Second: Then remember, sqljdbc.jar bag not only to put in the web_inf/lib and put in Apache-tomcat-6.0.41\lib.

The final Test code:

<%

Long begin = System.currenttimemillis ();
for (int i = 0; i <; i++) {
Context context = new InitialContext ();
DataSource DataSource = (DataSource) context.lookup ("java:comp/env/jdbc/zzz");
Connection Connection = Datasource.getconnection ();
Connection.close ();
}
Out.print (System.currenttimemillis ()-begin);
%>

This configuration will be more than the general open close connection time many many, test data loop 1000 times: The general time is more than 10 seconds, and the configuration database connection pool only more than 60 milliseconds!

Connect to database using Configuration connection pool in Tomcat

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.