1.數(shù)據(jù)庫(kù)鏈接的接口類:DatabaseConnection
package net.pingsoft.kelefa.pool;2. 數(shù)據(jù)庫(kù)鏈接工廠類ConnectionFactory
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(); }}
package net.pingsoft.kelefa.pool;
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注