现在的位置: 首页 > 综合 > 正文

如何在 resin下配置数据库连接池

2013年10月16日 ⁄ 综合 ⁄ 共 1886字 ⁄ 字号 评论关闭

如何在 resin下配置数据库连接池

在动态web站点设计中,数据库已成为必不可少的一部分,但数据库连接和释放开销很大,对于一个访问量少的网站可能没有什么影响,但同时有很多用户来网站查询资料时,就会导致服务器响应慢甚至死机。连接池就是针对这个问题提出的。
数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而再不是重新建立一个;释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏。这项技术能明显提高对数据库操作的性能。
数据库连接池在初始化时将创建一定数量的数据库连接放到连接池中,这些数据库连接的数量是由最小数据库连接数来设定的。无论这些数据库连接是否被使用,连接池都将一直保证至少拥有这么多的连接数量。连接池的最大数据库连接数量限定了这个连接池能占有的最大连接数,当应用程序向连接池请求的连接数超过最大连接数量时,这些请求将被加入到等待队列中。
Resin提供了一个良好的连接池来供开发人员来实现数据库连接,具体配置如下:
在/conf/resin.conf中加入以下内容:
<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></password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>

调用时:

Context initCtx = new javax.naming.InitialContext(); 
Context envCtx = (Context) initCtx.lookup("java:comp/env"); 
DataSource ds = (DataSource)envCtx.lookup("jdbc/test"); ;
Connection con =  ds.getConnection();
下表是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. &n

抱歉!评论已关闭.