`
yf86810_163_com
  • 浏览: 86726 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

tomcat dbcp 私有连接池配置方法之一

    博客分类:
  • JAVA
阅读更多
这种方法是我认为比较好的配置方法:

Eclipse环境下

第一步:建立Context.xml文件并放入WebRoot\META-INF目录下,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 连接池配置文件context.xml -->
<Context crossContext="true" debug="5"  reloadable="true">
    <Resource 
        name="jdbc/yourProject"<!-- yourProject为你的项目名,下面的也一样-->
        type="javax.sql.DataSource" 
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
        url="jdbc:sqlserver://192.168.0.32;DatabaseName=factory;"
        username="yf"
        password="yf456"
        auth="Container"
        maxActive="100"
        maxIdle="30"
        maxWait="10000"               
  />
</Context>



第二步:在web.xml文件下加入:

<resource-ref>   
  <res-ref-name>jdbc/factoryManager</res-ref-name>   
  <res-type>javax.sql.DataSource</res-type>   
  <res-auth>Container</res-auth>   
</resource-ref>



第三步:使用tomcat连接池:

package com.dfjy.fm.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.dfjy.fm.exception.MyException;

/*
 * 连接池类
 */
public class DBPool {
 private static DataSource m_pool = null;
 private static Connection m_conn = null;
 static {
  Context env = null;
  try {
   env = (Context) new InitialContext().lookup("java:comp/env");
   m_pool = (DataSource) env.lookup("jdbc/factoryManager");
   System.out.println(m_pool);
   if (m_pool == null) {
    System.err.println("'DBPool' is an unknown DataSource");
   } else {
    m_conn = m_pool.getConnection();
   }
  } catch (NamingException ne) {
   ne.printStackTrace();
  } catch (SQLException e) {
   System.out.println("得到连接失败!");
   throw new MyException();
  }
 }

 public static Connection getConnection() {
  return m_conn;
 }

 public static DataSource getPool() {
  return m_pool;
 }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics