jdbcconnectionpool.java
来自「短信系统SMS:支持普通短信、长短信和wap push短信的发送。」· Java 代码 · 共 202 行
JAVA
202 行
package com.ekun.common.db;
import java.sql.*;
import com.bitmechanic.sql.*;
import com.ekun.common.log.*;
public class JdbcConnectionPool
{
private static String alias = "";
private static String dbDriver = "";
private static String dbUrl = "";
private static String dbUser = "";
private static String dbPass = "";
private static int dbMaxConn = 0;
private static int dbIdleTimeout = 0;
private static int dbCheckoutTimeout = 0;
private static int dbMaxCheckoutCount = 0;
private static boolean dbTrace = false;
private static int dbTraceLevel = 0; //
private static int dbReapTimeInterval = 300; //清理连接池的时间间隔,默认半小时
private static boolean useStatementCache = true;
private static ConnectionPoolManager poolManager = null;
//public static
public static boolean dbDumpInfo = false;
protected static Logger logger = LoggerFactory.getLogger("jdbclog.JdbcConnectionPool");
/**
* Read and set jdbc connection pool properties.
*/
public static void setJdbcProperties()
{
DBConfigure confInstance = DBConfigure.getInstance();
dbDriver = confInstance.getJDBCDriver();
dbUrl = confInstance.getJDBCUrl();
dbUser = confInstance.getDBUserName();
dbPass = confInstance.getDBPassword();
dbMaxConn = confInstance.getMaxConn();
dbIdleTimeout = confInstance.getDbIdleTimeout();
dbCheckoutTimeout = confInstance.getCheckouttimeout();
dbTrace = (confInstance.getTrace() == 1) ? true : false;
if (poolManager != null)
{
ConnectionPool cPool = null;
try
{
cPool = poolManager.getPool("ekunsys");
cPool.setMaxConn(dbMaxConn); //最大连接数
cPool.setTimeoutSecond(dbIdleTimeout); //可用连接的最大空闲时间
cPool.setCheckoutTimeout(dbCheckoutTimeout); //连接的超时时间
cPool.setMaxCheckout(dbMaxCheckoutCount); //连接的最大使用次数
cPool.setTracing(dbTrace);
}
catch (Exception e)
{
logger.error("Jdbc Pool加载配置出错!", e);
}
}
dbDumpInfo = (confInstance.getDumpinfo() == 0 ? false : true);
dbTraceLevel = confInstance.getTracelevel();
//连接池回收线程的启动间隔,单位: 秒
dbReapTimeInterval = confInstance.getReaptimeinterval();
if (poolManager != null)
{
poolManager.setSleepInterval(dbReapTimeInterval);
//连接池刷新配置线程的启动间隔,单位: 秒
}
}
/**
* Initialize the ConnectionPoolManager and create a connection pool named ekunsys
*/
private static void init()
{
setJdbcProperties();
try
{
//
// Reap stale connections every 1 minutes
poolManager = new ConnectionPoolManager(dbReapTimeInterval); //the interval time is not minutes but seconds
poolManager.addAlias("ekunsys", dbDriver, dbUrl, dbUser, dbPass, dbMaxConn,
dbIdleTimeout, dbCheckoutTimeout,
dbMaxCheckoutCount, useStatementCache);
}
catch (Exception e)
{
e.printStackTrace();
logger.error("Jdbc Pool初始化出错!", e);
}
}
/**
* Get a persistent DB connection from the connection pool.
* The returned connection will not be closed automatically by the ConnectionPool,
* but the caller program should close it.
* @return Connection.
* @throws Exception
*/
public static Connection getPersistentConnection() throws Exception
{
Connection conn = null;
ConnectionPool connPool = null;
if (!JdbcConnectionPool.getPoolStatus())
{
return null;
}
connPool = poolManager.getPool("ekunsys");
conn = connPool.getPersistentConnection();
return conn;
}
/**
* Get a persistent DB connection from the connection pool.
* @return Connection.
* @throws SQLException
*/
public static Connection getConnection() throws SQLException
{
Connection conn = null;
setJdbcProperties();
ConnectionPool connPool = null;
if (!JdbcConnectionPool.getPoolStatus())
{
return null;
}
connPool = poolManager.getPool("ekunsys");
conn = connPool.getConnection();
return conn;
}
/**
* Is the ConnectionPoolManager initialized
* @return ture:initialized, false not be initialized
*/
public static boolean getPoolStatus()
{
setJdbcProperties();
return (getInstance() == null ? false : true);
}
/**
* If the ConnectionPoolManager has not been initialized then
* initilize it .
* @return poolManager
*/
private static synchronized ConnectionPoolManager getInstance()
{
if (poolManager == null)
{
try
{
init();
}
catch (Exception e)
{
logger.error("JdbcConnectionPool.getInstance() creat ConnectionPoolManager error: " +
e.getMessage());
}
}
return poolManager;
}
public static int getCacheSize()
{
int catchSize = 0;
try
{
catchSize = poolManager.getPool("ekunsys").size();
}
catch (Exception e)
{
logger.error("JdbcConnectionPool.getCacheSize() error:" +
e.getMessage());
}
return catchSize;
}
public static ConnectionPoolManager getPoolManager()
{
return poolManager;
}
public static void main(String[] s) throws Exception
{
Connection conn = null;
for (int i = 0; i < 1000; i++)
{
conn = JdbcConnectionPool.getConnection();
conn.close();
try
{
Thread.sleep(2000);
}
catch (Exception e)
{}
System.out.println("Get connection finished!");
}
System.exit(1);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?