`

DBPool

阅读更多
DBPool
http://www.snaq.net/java/DBPool/

download jar
(http://www.snaq.net/java/DBPool/DBPool-5.0.zip)

config a file name is dbConfig.properties   note: name can be changed to any
the following is a example for config

#  name=       # the name of dblog file
#  logfile=    # the url of logfile 
#  dateformat= # format of save log
#  drivers=    # db drivers for different dbPool (eg:oracle.jdbc.driver.OracleDriver,: \t\n\r\fcom.mysql.jdbc.Driver)
# 
#  the following is a example for a db pool that name is message. if want to add a new pool,add a some context as following format.
#  <-- "Standard" properties start-->
#  message.url=         # url of db.
#  message.user=        # user of db.
#  message.password=    # password of db.
#  message.minpool=     # minimum number of pooled connections, or 0 for none.
#  message.maxpool=     # maximum number of pooled connections, or 0 for none.
#  message.maxconn=     # Deprecated, but checked for backwards-compatibility.
#  message.maxsize=     # maximum number of possible connections, or 0 for no limit.
#  message.expiry=      # Deprecated, but checked for backwards-compatibility.
#  message.idletimeout= # idleTimeout idle timeout (seconds) for idle pooled connections, or 0 for no timeout.
#  message.validator=   # Sets the validator class for {@link Connection} instances.
#  message.decoder=     # Sets the {@link PasswordDecoder} class.
#  message.init=        # Setup initial connections in pool (default: 0).
#  <-- "Standard" properties end-->
#  <-- "Advanced" properties start-->
#  message.cache=                  # Determines whether to perform statement caching (true or false).
#  message.access=                 # set Pool Access Random or FIFO (random or fifo) (default: fifo).
#  message.async=                  # Determines whether to perform asynchronous object destruction. If set to true then each time an object is destroyed
                                   # the operation is done in a separate thread, allowing the method to return immediately
#  message.recycleafterdelegateuse # Sets whether the connection may be recycled if the underlying raw/delegate connection has been used (default: false).
#  message.mbean=                  # Setup JMX MBean access to pool (if requested). (true or false).
#  <-- "Advanced" properties end-->
#  <-- "Custom logging" properties start-->
#  message.logfile=    # the url of this pool log file
#  message.dateformat= # log format
#  message.debug=      # is debug the pool
#  <-- "Custom logging" properties end-->
#
# @author leoLee<leo@protelsws.com>
name=dblog
logfile=\log\db.log
dateformat=yyyy-MM-dd HH:mm:ss
drivers=oracle.jdbc.driver.OracleDriver
message.url=jdbc:oracle:thin:@xx.xx.xx.xx:1521:PreIMS
message.user=xxxxx
message.password=xxxxx
message.minpool=1
message.maxpool=100
message.maxsize=100
message.idletimeout=3000
message.init=5



build a fater class  NOTE:can be used anywhere

package org.protelnet.com.db;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import snaq.db.ConnectionPoolManager;
import snaq.util.logging.LogUtil;


/**
 * 
 * @author leoLee<leo@protelsws.com>
 *
 */
public class PoolManager {
	/** Apache Commons Logging instance for writing log entries. */
	private static Log logger = LogFactory.getLog(PoolManager.class.getName());;
	/** Custom logging utility (backward-compatible). */
	private static LogUtil logUtil;
	  
	private static ConnectionPoolManager poolManager;
	
	private static Connection connection = null;
	
    private static final String CONFIG_FILENAME = "dbConfig.properties";
    private static final String CONFIG_DIRECTORY = System.getProperty("user.dir") + File.separator;// + "conf";
	
	private static ConnectionPoolManager getConnectionPoolManager(){
		if(poolManager == null)
			try {
				poolManager = ConnectionPoolManager.getInstance(new File(CONFIG_DIRECTORY, CONFIG_FILENAME));
				log_info("init poolManager end...");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return poolManager;
	}
	
	public static Connection getConnection(String poolName) throws SQLException{
		if(getConnectionPoolManager()==null)
			return null;
		connection = getConnectionPoolManager().getConnection(poolName);
		log_info("getConnection for "+poolName);
		return connection;
	}
	
	public static void release(String poolName){
		if(getConnectionPoolManager()==null){
			return ;
		}
		if(getConnectionPoolManager().getPool(poolName)!=null)
			getConnectionPoolManager().getPool(poolName).release();
	};
	
	public static void releaseAll(){
		if(getConnectionPoolManager()==null){
			return ;
		}
		getConnectionPoolManager().release();
	};
	
    protected static void log_info(String s)
    {
	    String msg = PoolManager.class.getName() + ": " + s;
	    logger.info(msg);
	    if (logUtil != null)
	      logUtil.log(msg);
    }
}



a dome class

package org.protelnet.com.msg.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.protelnet.com.db.PoolManager;


/**
 * ConnectionManager to get and maintain a connection
 * @author leoLee < leo@protelsws.com >
 */
public class ConnectionManager extends PoolManager{

    private final static String POOLNAME = "message";

    public ConnectionManager() {
    }

    
    /**
     * get connection 
     * @return java.sql.Connection
     * @throws SQLException 
     * @throws java.lang.Exception
     */
    public static Connection getConnection() throws SQLException{
    	return getConnection(POOLNAME);
    }

    /**
     * close connection
     * @throws java.lang.Exception
     */
    public static void closeConnection(Connection connection) throws Exception {
       if(connection!=null)
          connection.close();
    }
    
    public static void release(){
    	release(POOLNAME);
    }
    
    public static void releaseAll(){
    	PoolManager.releaseAll();
    }
    
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics