Tomcat Jndi 数据库连接池配置
在这里以mysql为例,根据自己的摸索。简单介绍一下:
Tomcat 版本:5.0,把mysql的jdbc驱动.jar包放到tomcat的common/lib下去,也可以放到WEB-INF/lib下。
我的web module名字是test,在它的配置文件web.xml中加上:
<resource-ref>
<res-ref-name>jdbc/myJndiName_john</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在C:\jakarta-tomcat-5.0.30\conf\Catalina\localhost中建立一个配置xml文件,
我起名为:test.xml,文件名是任意的。
内容如下:
<Context path="/test" docBase="test"      <!-- 这两个配置元素test为你的web module的名字 (我的名为test)-->         
        debug="5" reloadable="true" crossContext="true">  <Logger className="org.apache.catalina.logger.FileLogger"
             prefix="localhost_DBTest_log." suffix=".txt"
             timestamp="true"/>  <Resource name="jdbc/myJndiName_john"
               auth="Container"
               type="javax.sql.DataSource"/>  <ResourceParams name="jdbc/myJndiName_john">
    <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 -1 for no limit.  See also the DBCP documentation 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
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->
    <parameter>
      <name>maxWait</name>
      <value>10000</value>
    </parameter>    <!-- MySQL dB username and password for dB connections  -->
    <parameter>
     <name>username</name>
     <value>root</value>
    </parameter>
    <parameter>
     <name>password</name>
     <value>john</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>
     -->
    
    <!-- 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/test</value>
    </parameter>
  </ResourceParams>
</Context>在class中用以下代码即可得到数据库连接池中的数据库连接:
private void test(){
try {
Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myJndiName_john"); Connection con = ds.getConnection(); //来连接数据库连接池
System.out.println("test success...");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}官方网站相关详细介绍,http://tomcat.apache.org/tomcat-5.0-doc/jndi-datasource-examples-howto.html以下是tomcat5.5的配置方法:
在Tomcat 5.5\conf\Catalina\localhost\xxxxx.xml(xxxxx.xml为你自己的web的配置xml文件) 中添加resource        <Resource name="MYSQL" 
        type="javax.sql.DataSource" 
        driverClassName="com.mysql.jdbc.Driver" 
        password="********" 
        maxIdle="2" 
        maxWait="5000" 
        username="root" 
        url="jdbc:mysql://localhost:3306/jnestore" 
        maxActive="10"/>
欢迎访问我的blog:
http://blog.sina.com.cn/u/1250048172