📄 dbconnectionmanager.java
字号:
package com.hoperun.connectionPool;
/**
* DBConnectionManager
*@author zhou_chenxi
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
public class DBConnectionManager {
//The only instance of the class DBConnectionManager.
static private DBConnectionManager instance;
//The number of clinents.
static private int clients;
//A container丆preserve the drivers of the datebase.
private ArrayList drivers=new ArrayList();
//Access the name of the target in the connectionpool and this target itself,
//in the form of name/value.
private HashMap pools = new HashMap();
/**
* If the only instance has been created丆return it;
* else create it in the init() from tje privte function.
*/
static synchronized public DBConnectionManager getInstance(){
if (instance == null) {
instance = new DBConnectionManager();
}
return instance;
}
/**
* Build the private function in order to prevent
* other targets from establishing this kind of instance.
* @call init()
*/
private DBConnectionManager(){
init();
}
/**
* Free a connection by the name of the connectionpool.
* @param name
* @param con
*/
public void freeConnection(String name,Connection con){
//Get the connectionpool by its name.
DBConnectionPool poolTemp = (DBConnectionPool) pools.get(name);
if (poolTemp!= null) {
//Free the connection.
poolTemp.freeConnection(con);
System.out.println("Free the connection乧乧");
clients--;
//For test,show the connections.
System.out.println("The number of free connections :"+DBConnectionPool.freeConnections.size());
}
}
/**
* get a connection from the connectionpool by the name
* @call getConnection(time).Put an acquiescence value myself.
* @param name
*/
public Connection getConnection(String name){
Connection con=null;
//Get the connectionpool.
DBConnectionPool poolTemp = (DBConnectionPool) pools.get(name);
//Get the connection.Acquiescence the timeout is 10000.
if(poolTemp!=null){
con=poolTemp.getConnection(10000);
if (con!= null)
System.out.println("Get the connection乧乧");
}
clients++;
//For test.Show the number of the clients.
System.out.println("The clients' number is:"+clients);
return con;
}
/**
* get a connection from the connectionpool by the name and time,
* @call getConnection(time)
* @param name
* @param time
*/
public Connection getConnection(String name,long time){
Connection con=null;
//Get the connectionpool.
DBConnectionPool poolTemp = (DBConnectionPool) pools.get(name);
//Get the connection.
if(poolTemp!=null){
con=poolTemp.getConnection(time);
if (con!= null)
System.out.println("Get the connection乧乧");
}
clients++;
//For test.Show the number of the clients.
System.out.println("The clients' number is:"+clients);
return con;
}
/**
* release all of the resource
*/
public synchronized void release(){
if (clients!= 0){
return;
}
//release the pools
Iterator allPools = pools.keySet().iterator();
while (allPools.hasNext()) {
DBConnectionPool pool = (DBConnectionPool) pools.get(allPools.next());
if(pool!=null)
pool.release();
System.out.println("Release the connectionpool乧乧");
}
pools.clear();
/**release the drivers
Iterator allDrivers = drivers.iterator();
while (allDrivers.hasNext()) {
Driver driver = (Driver) allDrivers.next();
try {
DriverManager.deregisterDriver(driver);
}
catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("Drivers released done.");*/
System.out.println("All source released.");
}
/**
* Create one or more ConnectionPools according to the infomation of property file.
* @param props
*/
private void createPools(Properties props){
String url=props.getProperty("URL").trim();
String user=props.getProperty("USER").trim();
String name=props.getProperty("NAME").trim();
String password=props.getProperty("PASSWORD").trim();
String maxcon=props.getProperty("MAXCONN").trim();
int maxConn=Integer.valueOf(maxcon).intValue();
if (maxConn<=0){
System.out.println("The connectins are full.");
maxConn=1;
}
DBConnectionPool poolTemp = new DBConnectionPool(name, url,
user, password, maxConn);
//Put the pool's info to the Hashmap.
pools.put(name, poolTemp);
}
/**
* Initial the example of the ConnectionPool.
*/
private void init(){
Properties propsTemp = new Properties();
try {
//set the filepath of the ORCALE.Properties.
InputStream fileinputstream = new
FileInputStream("./config/ORCALE.Properties");
//Load the properties file.
propsTemp.load(fileinputstream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
loadDrivers(propsTemp);
createPools(propsTemp);
}
/**
* Load the properties driver.
* @param props
*/
private void loadDrivers(Properties props){
String driverClasses = props.getProperty("DRIVER").trim();
try {
Driver driver = (Driver)
Class.forName(driverClasses).newInstance();
DriverManager.registerDriver(driver);
drivers.add(driver);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -