服务器 频道

Tomcat下配置Mysql的连接池

【IT168 服务器学院】1.在$CATALINA_HOME/conf/server.xml中添加配置信息,声明连接池的具体信息,添加内容如下:
  
  


  
  
  
  
  
  
  
  
  
  factory
  
  org.apache.commons.dbcp.BasicDataSourceFactory
  
  

  
  
  
  maxWait
  
  5000
  
  

  
  
  
  maxActive
  
  20
  
  

  
  
  
  username
  
  shopadm
  
  

  
  
  
  password
  
  123
  
  

  
  
  
  url
  
  jdbc:mysql://localhost/shopdb?useUnicode=true&charact-erEncoding=gb2312
  
  

  
  
  
  driverClassName
  
  com.mysql.jdbc.Driver
  
  

  
  
  
  maxIdle
  
  10
  
  

  

2. 在$CATALINA_HOME/conf/web.xml的前添加如下信息:
  
  


  
  DB Connection
  
  jdbc/mysql
  
  javax.sql.DataSource
  
  Container
  
  
  
  其中中的参数名必须和server.xml中声明的连接名一样。
  
  3. 在$CATALINA_HOME/conf/catalina/localhost目录下找到需要进行数据库连接的当前程序的配置信息,比如这里是shopping.xml,在这个文件中添加如下信息:
  
  

  
  …
  
  
  
  …
  
  
  
  大功告成!
  
  在此基础上,参考Tomcat官方网站的用户手册,摸索出另外一种配置连接池的方法,这个方法不需要对server.xml进行修改,只要对需要使用到连接池的程序的配置文档进行修改就可以了。方法如下:
  
  1.$CATALINA_HOME/conf/catalina/localhost目录下找到需要数据库连接池的程序的配置文档,此处是shopping.xml。在 之间添加如下信息,声明一个数据库连接池:
  
 
 
  
  
  
  
  
  factory
  
  org.apache.commons.dbcp.BasicDataSourceFactory
  
  

  
  
  
  maxWait
  
  5000
  
  

  
  
  
  maxActive
  
  20
  
  

  
  
  
  password
  
  123
  
  

  
  
  
  url
  
  jdbc:mysql://localhost/shopdb?useUnicode=true&characterEncoding=gb2312
  
  

  
  
  
  driverClassName
  
  com.mysql.jdbc.Driver
  
  

  
  
  
  maxIdle
  
  10
  
  

  
  
  
  username
  
  shopadm
  
  

  
  
  
  这里的参数和上一种方法中添加到server.xml里的信息几乎是完全一样的。
  
  2.在对应程序的WEB-INF下建立一个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">
  
  <resource-ref>
  
  <description>DB Connection</description>
  
  <res-ref-name>jdbc/mysql</res-ref-name>
  
  <res-type>javax.sql.DataSource</res-type>
  
  <res-auth>Container</res-auth>
  
  </resource-ref>
  
  </web-app>
0
相关文章