世界因你而精彩  
日历
<2006年6月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678
统计
  • 随笔 - 169
  • 文章 - 1
  • 评论 - 138
  • 引用 - 0

导航

常用链接

留言簿(9)

随笔分类(163)

随笔档案(154)

文章档案(1)

新闻档案(1)

相册

收藏夹(1)

个人杂杂

友情链接

自学考试

资料搜索

最新随笔

搜索

  •  

积分与排名

  • 积分 - 357310
  • 排名 - 153

最新评论

阅读排行榜

评论排行榜

 

以下是转帖:

Tomcat 的数据库连接池设置与应用

 

1.将数据库驱动程序的JAR文件放在Tomcat的 common/lib 中;

2.在server.xml中设置数据源,以MySQL数据库为例,如下:
在<GlobalNamingResources> </GlobalNamingResources>节点中加入,
      <Resource
      name="jdbc/DBPool"
      type="javax.sql.DataSource"
      password="root"
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="2"
      maxWait="5000"
      username="root"
      url="jdbc:mysql://127.0.0.1:3306/test"
      maxActive="4"/>
   属性说明:name,数据源名称,通常取”jdbc/XXX”的格式;
            type,”javax.sql.DataSource”;
            password,数据库用户密码;
            driveClassName,数据库驱动;
            maxIdle,最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连
                     接将被标记为不可用,然后被释放。设为0表示无限制。
            MaxActive,连接池的最大数据库连接数。设为0表示无限制。
            maxWait ,最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示
                     无限制。

3.在你的web应用程序的web.xml中设置数据源参考,如下:
  在<web-app></web-app>节点中加入,
  <resource-ref>
    <description>MySQL DB Connection Pool</description>
    <res-ref-name>jdbc/DBPool</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
 </resource-ref>
  子节点说明: description,描述信息;
               res-ref-name,参考数据源名字,同上一步的属性name;
               res-type,资源类型,”javax.sql.DataSource”;
               res-auth,”Container”;
               res-sharing-scope,”Shareable”;

4.在web应用程序的context.xml中设置数据源链接,如下:
  在<Context></Context>节点中加入,
  <ResourceLink
   name="jdbc/DBPool" 
   type="javax.sql.DataSource" 
   global="jdbc/DBPool"/>
   属性说明:name,同第2步和第3步的属性name值,和子节点res-ref-name值;
             type,同样取”javax.sql.DataSource”;
             global,同name值。
 
至此,设置完成,下面是如何使用数据库连接池。
1.建立一个连接池类,DBPool.java,用来创建连接池,代码如下:
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBPool {
    private static DataSource pool;
    static {
         Context env = null;
          try {
              env = (Context) new InitialContext().lookup("java:comp/env");
              pool = (DataSource)env.lookup("jdbc/DBPool");
              if(pool==null) 
                  System.err.println("'DBPool' is an unknown DataSource");
               } catch(NamingException ne) {
                  ne.printStackTrace();
          }
      }
    public static DataSource getPool() {
        return pool;
    }
}

2.在要用到数据库操作的类或jsp页面中,用DBPool.getPool().getConnection(),
获得一个Connection对象,就可以进行数据库操作,
最后别忘了对Connection对象调用close()方法,
注意:这里不会关闭这个Connection,而是将这个Connection放回数据库连接池。

也是可以直接连的:
如连接oracle
public class ConnectionProvider
{
 public static DataSource ds;

 static
 {
  DriverAdapterCPDS cpds = new DriverAdapterCPDS();

  try
  {
   cpds.setDriver("oracle.jdbc.driver.OracleDriver");
  }
  catch (ClassNotFoundException e)
  {
   String msg = "Could not find driver in the classpath ";
   System.out.println(msg);
   throw new RuntimeException(msg);
  }
               
  cpds.setUrl("jdbc:oracle:thin:@192.168.0.167:1521:epcora");
  cpds.setUser("71c");
  cpds.setPassword("123456");

  Jdbc2PoolDataSource tds = new Jdbc2PoolDataSource();
  tds.setConnectionPoolDataSource(cpds);
  tds.setDefaultMaxActive(20);
  tds.setDefaultMaxWait(50);
  
  ds = tds;
 }
}
这样就不用在server.xml,web.xml设置了。

还是若用eclipse跟踪bug,设置连接数据库则须在“Java构建路径”-“库”里添加上
数据库驱动程序的JAR文件即可。

posted on 2006-06-22 15:18 张秀兰 阅读(4798) 评论(0)  编辑  收藏 所属分类: 学习园地资料查找

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © 张秀兰 Powered by: 博客园 模板提供:沪江博客