国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

使用Commons-Pool寫的數(shù)據(jù)庫(kù)連接池

2019-11-17 06:33:30
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1.數(shù)據(jù)庫(kù)鏈接的接口類:DatabaseConnection

package net.pingsoft.kelefa.pool;
import java.sql.*;import javax.sql.*;import com.wish.JDBC.WConnection;import org.apache.commons.pool.*;import org.apache.commons.pool.impl.*;/** * 數(shù)據(jù)庫(kù)鏈接的接口類.直接調(diào)用靜態(tài)方法getDBConnection()取得Connection對(duì)象. * 相關(guān)的參數(shù)由PoolConfigServlet類根據(jù)web.xml設(shè)置,所以web.xml需要注冊(cè)PoolConfigServlet * * Copyright: Copyright (c) 2004 * @author kelefa yang * @version 1.0 * @see PoolConfigServlet */public class DatabaseConnection{ /** 數(shù)據(jù)庫(kù)的用戶名 */ public static String USER = "sa"; /** 數(shù)據(jù)庫(kù)的用密碼 */ public static String PASS = "yf1"; /** 數(shù)據(jù)庫(kù)的啟動(dòng)程序類名 */ public static String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; /** 數(shù)據(jù)庫(kù)的鏈接地址 */ public static String DBURL = "jdbc:microsoft:sqlserver://192.9.200.23:1433;DatabaseName=FC"; /** 是否使用jndi */ public static boolean useJNDI = false; /** jndi的名字,useJNDI==false時(shí)無(wú)效 */ public static String JNDI = "wishJndi"; /** * 是否對(duì)數(shù)據(jù)庫(kù)鏈接進(jìn)行編碼轉(zhuǎn)換 * @dePRecated 沒有進(jìn)行測(cè)試,應(yīng)該直接設(shè)置數(shù)據(jù)庫(kù)或在鏈接url加上編碼參數(shù) */ public static boolean convertConnetion = false; /** 是否緩沖鏈接 */ public static boolean POOL_CONNECTION = false; /** * 沒有必要生成實(shí)例 */
private DatabaseConnection() {}
/** * 根據(jù)相應(yīng)參數(shù)取回實(shí)際的數(shù)據(jù)庫(kù)鏈接. * 假如useJNDI為真,根據(jù)JNDI名字從數(shù)據(jù)源取鏈接;否則直接從jdbc取鏈接. * 假如convertConnetion為真,對(duì)鏈接再封裝,實(shí)現(xiàn)編碼的轉(zhuǎn)換. * @throws Exception 當(dāng)useJNDI==false,并且USER,PASS,DBDRIVER,DBURL其中一個(gè)為空時(shí) * 拋出"DatabaseConnection didn't init!!"異常 * @return Connection 數(shù)據(jù)庫(kù)鏈接,取不到時(shí)返回null */ static Connection getConnection() throws Exception { Connection conn = null; if( !useJNDI ) { if( ( USER == null ) ( PASS == null ) ( DBDRIVER == null ) ( DBURL == null ) ) throw new Exception( "DatabaseConnection didn't init!!" ); Class.forName( DBDRIVER ); conn = DriverManager.getConnection( DBURL, USER, PASS ); } else { DataSource ds = ServiceLocator.getInstance().getDataSource( JNDI ); conn = ds.getConnection(); } if( convertConnetion && conn != null ) return new WConnection( conn ); else return conn; }
/** 鏈接池工廠 */ private static GenericObjectPoolFactory poolFactory = new GenericObjectPoolFactory( new ConnectionFactory() ); /** 數(shù)據(jù)庫(kù)鏈接池 */ private static ObjectPool pool = poolFactory.createPool();

/** * if POOL_CONNECTION is true, return the pooled connetion. * POOL_CONNECTION will be set in Class PoolConfigServlet when webapp start, * you can change the value in web.xml. *
   * <init-param>   *    <param-name>poolConnection</param-name>   *    <param-value>true</param-value>   * </init-param>   * 
* @throws Exception * @return Connection */ public static Connection getDBConnection() throws Exception { if( POOL_CONNECTION ) { Object obj = pool.borrowObject(); if( null == obj ) return null; PoolableConnection conn = ( PoolableConnection )obj; conn.setPool( pool ); return conn; } else return getConnection(); }}
2. 數(shù)據(jù)庫(kù)鏈接工廠類ConnectionFactory 
package net.pingsoft.kelefa.pool;


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 交口县| 邛崃市| 浦东新区| 射洪县| 神木县| 百色市| 门源| 濮阳市| 达日县| 织金县| 南陵县| 亳州市| 德令哈市| 罗定市| 保山市| 措勤县| 庆阳市| 方山县| 丰原市| 当涂县| 秭归县| 那坡县| 广昌县| 芮城县| 寻甸| 竹山县| 克什克腾旗| 宣威市| 和平县| 金山区| 长垣县| 介休市| 霍邱县| 德兴市| 嵊州市| 云龙县| 茂名市| 浦江县| 洪湖市| 濉溪县| 咸宁市|