Resin 配置連線池

passport_daizi發表於2012-06-11

轉載地址: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();

最後記得關閉資料庫連線。

相關文章