转载地址:http://hi.baidu.com/xiaopeng3017/blog/item/47a93a7aba05a1ed2f73b300.html
在动态web站点设计中,数据库已成为必不可少的一部分,但数据库连接和释放开销很大,对于一个访问量少的网站可能没有什么影响,但同时有很多用户来网站查询资料时,就会导致服务器响应慢甚至死机。连接池就是针对这个问题提出的。
数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而再不是重新建立一个;释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏。这项技术能明显提高对数据库操作的性能。
数据库连接池在初始化时将创建一定数量的数据库连接放到连接池中,这些数据库连接的数量是由最小数据库连接数来设定的。无论这些数据库连接是否被使用,连接池都将一直保证至少拥有这么多的连接数量。连接池的最大数据库连接数量限定了这个连接池能占有的最大连接数,当应用程序向连接池请求的连接数超过最大连接数量时,这些请求将被加入到等待队列中。
Resin提供了一个良好的连接池来供开发人员来实现数据库连接,具体配置如下:
在/conf/resin.conf中<resin></resin>标签中加入以下内容:
<database>
<jndi-name>jdbc/test</jndi-name>
<driver type="com.microsoft.jdbc.sqlserver.SQLServerDriver">
<url>jdbc:microsoft:sqlserver://localhost:1433;databasename=Northwind</url>
<user>sa</user>
<password>uuuu</password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
把相应的数据库的jar包拷贝到resin目录下的lib目录下面。
调用时:
Connection conn = null;
Statement stmt = null;
//ResultSet rs = null;
try{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/yourDBName");
conn = ds.getConnection();
stmt = conn.createStatement();
}
catch(Exception e){
e.printStackTrace();
}
下表是resin对具体参数的解释:
Attribute Meaning default
jndi-name JNDI name to store the pool under. Servlets, jsp, and other java code use this name. The path is relative to java:comp/env
driver Configure the database driver.
max-connections Pooling parameter - maximum number of allowed connections 20
max-idle-time Pooling parameter - maximum time an idle connection is kept in the pool 30 sec
max-active-time Pooling parameter - maximum time a connection allowed to be active 6 hours
max-pool-time Pooling parameter - maximum time a connection is kept in the pool 24 hours
connection-wait-time Pooling parameter - how long to wait for an idle connection (Resin 1.2.3) 10 minutes
max-overflow-connections Pooling parameter - how many "overflow" connection are allowed if the connection wait times out. 0
ping-table Reliability parameter - The database table used to "ping", checking that the connection is still live.
在程序中可以这样来使用:
创建一个获得数据库连接Connnection的类,提供出一个公用方法:JndiRes.java
import java.sql.Connection; import javax.naming.Context; import javax.naming.NamingException; import javax.sql.DataSource; /** * @author zcs * @description * @date 2012-6-11 下午01:59:19 */ public class JdniRes { /** * 功能:获取db2数据库的数据源 */ static private DataSource getDataSouceFromDB2() { DataSource _source = null; if (_source == null){ try { Context context = new javax.naming.InitialContext(); //从JNDI取出java环境上下文对象 _source = (DataSource) context.lookup("java:comp/env/jdbc/db2Demo");//取出数据源 } catch (NamingException e) { System.out.println("NamingException->"+e.toString()); } } if(_source == null){ System.out.println("_source为null"); } return _source; } /** * 功能:获取db2数据库的连接对象 */ public static Connection getConnectionFromDB2(){ try{ Connection c = getDataSouceFromDB2().getConnection(); //从连接池获取连接对象 //c.setReadOnly(false); return c; } catch (Exception e) { System.err.println("JndiRes.getConnection() error."); e.printStackTrace(); return null; } } }
JdniRes jr = new JdniRes(); //利用resin中间件数据连接池 Connection conn = null; conn = jr.getConnectionFromDB2();