Tomcat configuration SQL Server Connection pool

Source: Internet
Author: User
Tags auth connect mysql resource stmt tomcat tomcat server
Server|sqlserver 1. Enter the Conf folder directory in the Tomcat directory, find Server.xml in tomcat server.xml file in </Context> tags and </Host> tags, the format of the connection statement, as follows

<!-path the directory of your application,/dbtest says in the Tomcat WebApps directory à

<!-docbase= "Dbtest" indicates the path name when you perform the stop procedure, for example, the following execution path is http://localhost:8080/dbtest--> <context path= "/dbtest" Docbase= "Dbtest" debug= "5" reloadable= "true" crosscontext= "true" >

<!--maxactive: Maximum number of connection pools, make sure there are enough connections-->

<!--Maxidle: Maximum number of idle connections, set to-1 means unrestricted-->

<!--maxwait: The maximum wait connection time (the time at which the largest waiting connection pool is available), in nanoseconds, set to 10000 equal to 10 seconds, and if set to-1 indicates uncertainty-->

<!--username and password: the account number and password used to connect to the database-->

<!--Driverclassname: A driver that connects to a database, such as SQL Server

Com.microsoft.jdbc.sqlserver.sqlserverdriver.-->

<!--URL: Connect the database path, such as

Jdbc:microsoft:sqlserver://localhost:1433;databasename=kb_rate-->

<resource name= "Default_jdbc" auth= "Container" type= "Javax.sql.DataSource" maxactive= "30" maxidle= " Maxwait= "10000" username= "Javauser" password= "Javadude" driverclassname= "Com.microsoft.jdbc.sqlserver.SQLS" Erverdriver "url=" jdbc:microsoft:sqlserver://localhost:1433;databasename=kb_rate ""/>

</Context>

After the     configuration is complete, add a test file under the Dbtest directory, as follows: <%@ page contenttype= "text/html;charset=gb2312"%><%@ Page import= "java.sql.*"%><%@ page import= "javax.sql.*"%><%@ page import= "javax.naming.*"%><%   Connection conn = null;  Context Initctx = new InitialContext ();  if (initctx = null)    & nbsp throw new Exception ("Cannot get context!");       Context CTX = (context) initctx.lookup ("java:comp/env");       Object obj = (object) ctx.lookup ("/default_jdbc");//Get Connection Pool objects        Javax.sql.DataSource ds = (javax.sql.DataSource) obj; Type conversions       conn = ds.getconnection ();  Statement stmt = conn.createstatement ();  PreparedStatement ps=conn.preparestatement ("SELECT * from Finalorderdata");  ResultSet rs=ps.executequery ();   while (Rs.next ()) {      out.println (rs.getstring (1) + "<BR>");        i++; }rs.close (); Stmt.close (); Conn.close (); OUT.PRINTLN ("Connection pool test Successful" +i);

    to the TOMCAT5.0 version of the settings are similar, but in the Server.xml add a connection pool settings on the statement is not, you can refer to http://jakarta.apache.org/tomcat/ Tomcat-5.0-doc/jndi-datasource-examples-howto.html Other operations are similar, <context path= "/dbtest" docbase= "DBTest"          debug= "5" reloadable= "true" crosscontext= "true" ><context path= "/dbtest" Docbase= "dbtest"         debug= "5" reloadable= "true" crosscontext= "true" >   <logger classname= "Org.apache.catalina.logger.FileLogger"               prefix= "Localhost_dbtest_log." suffix= ". txt"               timestamp= "true"/>  <resource name= "Jdbc/testdb"                 auth= "Container"                 type= "Javax.sql.DataSource"/>  < Resourceparams NAme= "Jdbc/testdb" >    <parameter>      <name>factory</ name>      <value>org.apache.commons.dbcp.basicdatasourcefactory</value >    </parameter>    <!--Maximum number of DB connections in pool. Make sure you         Configure your mysqld max_connections large to handle         all of your db connections. Set to 0 for no limit.        -->    <parameter>       <name>maxActive</name>      <value>100 </value>    </parameter>    <!--Maximum number of idle DB connections to Retain in pool.         Set to-1 for no limit.  the also the DBCP Documentat Ion On this         and the Minevictableidletimemillis configuration parameter.         -->    <parameter>       <name>maxIdle</name>      <value>30</value>     </parameter>    <!--Maximum time to wait for a DB connection to become Available&nbs p;        in MS, example seconds. An Exception are thrown if         this timeout be exceeded.  Set to-1 to Wai T indefinitely.        -->    <parameter>       <name>maxWait</name>      <value>10000</ value>    </parameter>    <!--MySQL db username and password for DB connections -->    <parameter>     <name>username</name>      <value>javauser</value>    </parameter>     <parameter>     <name>password</name>     < value>javadude</value>    </parameter>    <!--Class name for the old Mm.mysql JDBC driver-uncomment This entry and comment next         if you want t o Use this driver-we recommend using connector/j though    <parameter>    & nbsp;  <name>driverClassName</name>       <value> org.gjt.mm.mysql.driver</value>    </parameter>    -->     <!--Class name for the official MySQL connector/j driver-->   <parameter>       <name>driverClassName</name>        <value>com.mysql.jdbc.Driver</value>    </parameter>     <!--the JDBC connection URL for connecting to your MySQL DB.&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N bsp;  the autoreconnect=true argument to the URL makes sure that the          Mm.mysql JDBC Driver'll automatically reconnect if mysqld closed the       & nbsp connection.  mysqld By default closes idle connections after 8 hours.       & nbsp -->    <parameter>      <name>url</name>       <value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>     </parameter>  </ResourceParams></Context> have noFoot of the place, please make corrections!

And, is to please the shrimp door can tell the younger brother, how to set the method to make the connection pool to achieve the highest efficiency, because I tried to use Tomcat connection pool, found relatively slow, more than a long time, I do not know set the wrong there,,!! My settings are as follows: <context path= "/kb_rate" docbase= "/kb_rate" debug= "5" reloadable= "true" crosscontext= "true" > <r Esource name= "Default_jdbc" auth= "Container" type= "Javax.sql.DataSource" maxactive= "" maxidle= "Maxwai"              T= "10000" username= "sa" password= "1234" driverclassname= "Com.microsoft.jdbc.sqlserver.SQLServerDriver" Url= "Jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=kb_rate"/></context>



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.