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