package com.trafficcast.pool;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import com.trafficcast.bean.ConfigureInfo;
public class PoolManager {
private static String driver = "com.mysql.jdbc.Driver",//
url = null,
name = "drcom",
password = "drcom";
private static Class driverClass = null;
private static ObjectPool connectionPool = null;
private static ObjectPool incidentsConnectionPool = null;
public PoolManager() {
}
/**
* initProperties
*/
private static void loadProperties() {
url = "jdbc:mysql://"+ ConfigureInfo.dbhost + ":" + ConfigureInfo.db_port +"/tciwms";
name = ConfigureInfo.username;
password = ConfigureInfo.password;
}
/**
* initProperties
*/
private static void loadIncidentsProperties() {
url = "jdbc:mysql://"+ ConfigureInfo.incidents_host + ":" + ConfigureInfo.db_port +"/incident_db";
name = ConfigureInfo.incidents_username;
password = ConfigureInfo.incidents_password;
}
/**
* Init the datasource
*/
private static synchronized void initDataSource() {
if (driverClass == null) {
try {
driverClass = Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
/**
* Start the conntion pool
*
* @throws Exception
*/
public static void StartPool() {
loadProperties();
initDataSource();
if (connectionPool != null) {
ShutdownPool();
}
try {
connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
url, name, password);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
connectionFactory, connectionPool, null, null, false, true);
Class.forName("org.apache.commons.dbcp.PoolingDriver");
PoolingDriver driver = (PoolingDriver) DriverManager
.getDriver("jdbc:apache:commons:dbcp:");
driver.registerPool("dbpool", connectionPool);
System.out.println("Start the conntion pool OK");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Start the incidents conntion pool
*
* @throws Exception
*/
public static void StartIncidnetsPool() {
loadIncidentsProperties();
initDataSource();
if (incidentsConnectionPool != null) {
ShutdownIncidentsPool();
}
try {
incidentsConnectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, name, password);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
connectionFactory, incidentsConnectionPool, null, null, false, true);
Class.forName("org.apache.commons.dbcp.PoolingDriver");
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
driver.registerPool("dbpool2", incidentsConnectionPool);
System.out.println("Start the incidents conntion pool OK");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void ShutdownIncidentsPool() {
try {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
driver.closePool("dbpool2");
} catch (SQLException e) {
e.printStackTrace();
}
}//end ShutdownIncidentsPool
/**
* Release the pool
*/
public static void ShutdownPool() {
try {
PoolingDriver driver = (PoolingDriver) DriverManager
.getDriver("jdbc:apache:commons:dbcp:");
driver.closePool("dbpool");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* get connection
*
* @return
*/
public static Connection getConnection() {
Connection conn = null;
if (connectionPool == null)
StartPool();
try {
conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:dbpool");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* get connection
*
* @return
*/
public static Connection getincIdentsConnection() {
Connection conn = null;
if (connectionPool == null)
StartIncidentsPool();
try {
conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:dbpool");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
private static void StartIncidentsPool() {
// TODO Auto-generated method stub
}
/**
* getConnection
*
* @param name
* @return
*/
public static Connection getConnection(String name) {
return getConnection();
}
/**
* freeConnection
*
* @param conn
*/
public static void freeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* freeConnection
*
* @param name
* @param con
*/
public static void freeConnection(String name, Connection con) {
freeConnection(con);
}
}// end class
Connection Pool Manager
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.