DBPool

[b]DBPool[/b]
[url=http://www.snaq.net/java/DBPool/]http://www.snaq.net/java/DBPool/[/url]

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

config a file name is dbConfig.properties [color=red]note: name can be changed to any[/color]
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



[b]build a fater class[/b] [color=darkred]NOTE:can be used anywhere[/color]

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);
}
}



[b]a dome class[/b]

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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值