连接池配置文件

1.oracle连接池的配置文件
<?xml version='1.0' encoding='utf-8'?>
<Server>
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <GlobalNamingResources>
    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>
    <Resource auth="Container" description="User database that can be updated and saved" name="UserDatabase" type="org.apache.catalina.UserDatabase"/>
    <Resource name="jdbc/ccitwap" type="javax.sql.DataSource"/>
    <ResourceParams name="UserDatabase">
      <parameter>
        <name>factory</name>
        <value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
      </parameter>
      <parameter>
        <name>pathname</name>
        <value>conf/tomcat-users.xml</value>
      </parameter>
    </ResourceParams>
    <ResourceParams name="jdbc/ccitwap">
      <parameter>
        <name>maxWait</name>
        <value>5000</value>
      </parameter>
      <parameter>
        <name>maxActive</name>
        <value>4</value>
      </parameter>
      <parameter>
        <name>password</name>
        <value>ccitora</value>
      </parameter>
      <parameter>
        <name>url</name>
        <value>jdbc:oracle:thin:@192.168.1.57:1521:WSKFTEST</value>
      </parameter>
      <parameter>
        <name>driverClassName</name>
        <value>oracle.jdbc.driver.OracleDriver</value>
      </parameter>
      <parameter>
        <name>maxIdle</name>
        <value>2</value>
      </parameter>
      <parameter>
        <name>username</name>
        <value>ccitora</value>
      </parameter>
    </ResourceParams>
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8080" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25">
    </Connector>
    <Connector port="8009" protocol="AJP/1.3" protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8443">
    </Connector>
    <Engine defaultHost="localhost" name="Catalina">
      <Host appBase="webapps" name="localhost">
      <DefaultContext className="org.apache.catalina.core.StandardDefaultContext">
          <ResourceLink global="jdbc/ccitwap" name="jdbc/ccitwap" type="javax.sql.DataSource"/>
        </DefaultContext>
        <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_log." suffix=".txt" timestamp="true"/>
      </Host>
      <Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/>
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
    </Engine>
  </Service>
</Server>
-----------------------------------------------------------------------------------------------------------------------------------------

转  
  一、安装 JDBC Driver
        我用的是 tomcat5.5 mysql5.0 ,将数据库的驱动放至 {Tomcat_install}/common/lib 目录下。
二、配置 Connection Pool
        1. 配置全局的连接池,可以由多个工程引用。
Tomcat server.xml 文件中的 GlobalNamingResources 结点下面添加 , 连接池设置   
1.          < Resource  name = "jdbc/linux"  auth = "Container"  type = "javax.sql.DataSource"    maxActive = "100"  maxIdle = "30"  maxWait = "10000"  username = "root"  password = "" driverClassName = "com.mysql.jdbc.Driver"    url = "jdbc:mysql://localhost:3306/jaoso?useUnicode=true&characterEncoding=GBK"  />   
然后在 conf/catalina/localhost/ 下面添加你工程的配置文件,我的工程名叫 linuxDemo ,配置文件名叫 linuxDemo.xml
1.          < Context  docBase = "linuxDemo"  path = "/linuxDemo"  privileged = "true">
< ResourceLink  name = "jdbc/linux"  global = "jdbc/linux"  type = "javax.sql.DataSource"/>       
3.          Context >   
或者直接把上面这段放到 server.xml host 结点中就不用添加上面的 linuxDemo.xml , 其中这一行别忘了,
1.          < ResourceLink  name = "jdbc/linux"  global = "jdbc/linux"  type = "javax.sql.DataSource"/>      
是引用服务器上全局的连接池资源 这两种效果是一样的 , 现在就可以在 web 工程中引用连接池来创建数据库接了.
 2. 第二种方法:配置私有的连接池:只有该工程可以使用直接在 conf/catalina/localhost/ 中添加 linuxDemo.xml ,添加如下内容:
1.          < Context  docBase = "linuxDemo"  path = "/linuxDemo"  privileged = "true">       
2.          < Resource  name = "jdbc/linux"  auth = "Container"  type = "javax.sql.DataSource"       
3.                          maxActive = "100"  maxIdle = "30"  maxWait = "10000"       
4.                          username = "root"  password = ""  driverClassName = "com.mysql.jdbc.Driver"       
5.                          url = "jdbc:mysql://localhost:3306/jaoso?useUnicode=true&characterEncoding=GBK"  />       
6.          Context >      
这样就可以了。
3 ,第三种方法:也是私有的连接池
直接在服务器上 {Tomcat_install}/conf 目录下的 context.xml 添加工程配置和连接池配置,在结点 Context 中,添加:
1.          < Resource  name = "jdbc/linux"  auth = "Container"  type = "javax.sql.DataSource"   
2.                          maxActive = "100"  maxIdle = "30"  maxWait = "10000"   
3.                          username = "root"  password = ""  driverClassName = "com.mysql.jdbc.Driver"   
4.                          url = "jdbc:mysql://localhost:3306/jaoso?useUnicode=true&characterEncoding=GBK"  />   
就可以了
三、使用 JDBC 数据来源取得 Connection 对象
public class DatabaseConn {
 
 public static synchronized Connection getConnection() {
  try {
   Context initCtx = new InitialContext();
   Context envCtx = (Context) initCtx.lookup("java:comp/env");
   DataSource ds = (DataSource) envCtx.lookup("jdbc/linux");
   return ds.getConnection();
  } catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值