tomcat数据库连接池配置

来源:互联网 发布:赌场掷骰子庄家算法 编辑:程序博客网 时间:2024/04/29 20:13

一、context.xml中的配置

 

<Context path="/DBTest" docBase="DBTest"  

       debug="5" reloadable="true" crossContext="true">  

<!—注意到这里的DBTest了吧,这就是要求大家建立DBTest目录的原因。-->    

 

 <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 enough 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 0 for no limit.   

        -->  

   <parameter>  

     <name>maxIdle</name>  

     <value>30</value>  

   </parameter>    

 

   <!-- Maximum time to wait for a dB connection to become available   

  1.         in ms, in this example 10 seconds. An Exception is thrown if   
  2.         this timeout is exceeded.  Set to -1 to wait indefinitely.   
  3.         -->  

   <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 to use this driver - we recommend using Connector/J though   

   <parameter>  

      <name>driverClassName</name>  

      <value>org.gjt.mm.mysql.Driver</value>  

</parameter>  

这里面是被注释的,因为以前连接mysql是这样连接的,现在建议用下面的方法,自己可以看到。   

    -->  

 

   <!-- 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.   

        The autoReconnect=true argument to the url makes sure that the   

        mm.mysql JDBC Driver will automatically reconnect if mysqld closed the   

        connection.  mysqld by default closes idle connections after 8 hours.   

        -->  

   <parameter>  

     <name>url</name>  

     <value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>  

<!--这是数据库的地址,也可以不要这么繁琐,改为   

jdbc:mysql://localhost/javatest即可,javatest为数据库名-->  

   </parameter>  

 </ResourceParams>  

</Context>  

二、web.xml 中的配置

<?xml version="1.0" encoding="ISO-8859-1"?>  

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"  

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  version="2.4">  

 <description>MySQL Test App</description>  

 <resource-ref>  

     <description>DB Connection</description>  

     <res-ref-name>jdbc/TestDB</res-ref-name>  

     <res-type>javax.sql.DataSource</res-type>  

     <res-auth>Container</res-auth>  

 </resource-ref>  

</web-app> 

三、 java中使用的代码

try  

{   

Context initCtx=new InitialContext();   

DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/TestDB");   

Connection conn=ds.getConnection();   

out.println("data from database:<br>");   

Statement stmt=conn.createStatement();   

ResultSet rs =stmt.executeQuery("select id, foo, bar from testdata");   

while(rs.next())   

{   

out.println(rs.getInt("id"));   

out.println(rs.getString("foo"));   

out.println(rs.getString("bar"));   

}   

rs.close();   

stmt.close();   

}   

catch(Exception e)   

{   

e.printStackTrace();   

}