使用Commons-Pool写的数据库连接池

类别:Java 点击:0 评论:0 推荐:
1.数据库链接的接口类: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.*; /** * 数据库链接的接口类.直接调用静态方法getDBConnection()取得Connection对象. * 相关的参数由PoolConfigServlet类根据web.xml设置,所以web.xml需要注册PoolConfigServlet * * Copyright: Copyright (c) 2004 * @author kelefa yang * @version 1.0 * @see PoolConfigServlet */ public class DatabaseConnection { /** 数据库的用户名 */ public static String USER = "sa"; /** 数据库的用密码 */ public static String PASS = "yf1"; /** 数据库的启动程序类名 */ public static String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; /** 数据库的链接地址 */ public static String DBURL = "jdbc:microsoft:sqlserver://192.9.200.23:1433;DatabaseName=FC"; /** 是否使用jndi */ public static boolean useJNDI = false; /** jndi的名字,useJNDI==false时无效 */ public static String JNDI = "wishJndi"; /** * 是否对数据库链接进行编码转换 * @deprecated 没有进行测试,应该直接设置数据库或在链接url加上编码参数 */ public static boolean convertConnetion = false; /** 是否缓冲链接 */ public static boolean POOL_CONNECTION = false; /** * 没有必要生成实例 */
private DatabaseConnection() {}
/** * 根据相应参数取回实际的数据库链接. * 如果useJNDI为真,根据JNDI名字从数据源取链接;否则直接从jdbc取链接. * 如果convertConnetion为真,对链接再封装,实现编码的转换. * @throws Exception 当useJNDI==false,并且USER,PASS,DBDRIVER,DBURL其中一个为空时 * 抛出"DatabaseConnection didn't init!!"异常 * @return Connection 数据库链接,取不到时返回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() ); /** 数据库链接池 */ 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. 数据库链接工厂类ConnectionFactory 

package net.pingsoft.kelefa.pool;
import org.apache.commons.pool.*; import java.sql.Connection;
/** * 数据库链接对象工厂,负责创建数据库链接对象,并把它封装成可缓冲的对象以及关闭数据库链接。 * 这个工厂实例作为org.apache.commons.pool.impl.GenericObjectPoolFactory的构造函数的 * 参数。 * * Copyright: Copyright (c) 2004 * @author kelefa yang * @version 1.0 * @see org.apache.commons.pool.impl.GenericObjectPoolFactory */ public class ConnectionFactory extends BasePoolableObjectFactory { public ConnectionFactory() { }
/** * Creates a Connection instance that can be returned by the pool. * * @return an instance that can be returned by the pool. * @throws Exception * @todo Implement this org.apache.commons.pool.PoolableObjectFactory method */ public Object makeObject() throws Exception { Connection conn = DatabaseConnection.getConnection(); if (conn==null) return null; return new PoolableConnection(conn); }
/** * close a Connection instance no longer needed by the pool. * * @param obj the instance to be destroyed * @throws Exception * @todo Implement this org.apache.commons.pool.PoolableObjectFactory method */ public void destroyObject( Object obj ) throws Exception { PoolableConnection conn = (PoolableConnection)obj; conn.setPool( null ); conn.close(); } }
3. 重载Connection.close()方法,使得可以返回数据库链接池

package net.pingsoft.kelefa.pool;
import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.pool.*;



/** * 重载Connection.close()方法,使得可以返回数据库链接池. * * Copyright: Copyright (c) 2004 * @author kelefa yang * @version 1.0 */ public class PoolableConnection extends ConnectionWrap { /** 数据库链接池 */ private ObjectPool pool; public PoolableConnection(Connection conn) { super(conn); } /** * 如果数据库链接池存在,则将这个链接返回数据库链接池;否则关闭这个数据库链接 * @throws SQLException */ public void close() throws SQLException { try { if (pool != null) pool.returnObject( this ); else super.close(); } catch( Exception ex ) { } } /** * 设置这个链接所在的链接池. * 如果pool等于null,链接用完直接关闭,否则用完后返回链接池中. * @param pool 数据库链接池 */ public void setPool(ObjectPool pool) { this.pool = pool; } }

本文地址:http://com.8s8s.com/it/it13490.htm