æ°æ®åºè¿æ¥æ¯æ常ç¨çæé«æ§è½çæ¹æ³ï¼æ»ç»äºä¸ä¸ï¼1 å®ä¹æ°æ®åºè®¿é®æ¥å£public interface IDatabaseConnection
{
public abstract Connection createConnection();
public abstract void closeConnection(Connection conn);
}2 å®ç°è¯¥æ¥å£2.1 ç´æ¥è¿æ¥ï¼è¿æ¥é度è¾æ
¢ï¼ç¨æ·éè¾å¤§æ¶èµæºå ç¨å¾å¤§public Connection createConnection()
{
try
{
SystemParameter SP = new SystemParameter();
Class.forName(DBDRIVER).newInstance();
Connection con =
DriverManager.getConnection(
DBDRIVER_URL,
DBUSER,
DBPASSWORD);
return con;
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
return null;
}
catch (SQLException e)
{ e.printStackTrace();
return null;
}
catch (InstantiationException e)
{ e.printStackTrace();
return null;
}
catch (IllegalAccessException e)
{ e.printStackTrace();
return null;
}
}
public void closeConnection(Connection conn)
{
if(conn!=null) try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
} }2.2 DBConnectionManagerãDBConnectionPoolæ¹å¼è¿ä¸ªè¿æ¥æ± æ¯ç½ä¸æ¯è¾å¤çè¿æ¥æ± 代ç public class DBConnectionManager
{
static private DBConnectionManager instance; /*å¯ä¸å®ä¾*/
static private int clients;
/*该计æ°ä»£è¡¨å¼ç¨DBConnectionManagerå¯ä¸å®ä¾ç客æ·ç¨åºæ»æ°ï¼
å®å°è¢«ç¨äºæ§å¶è¿æ¥æ± çå
³éæä½ */
//Added by leo on 2001-5-23
static private String DEFAULTCONNECTIONPOOL = "homepage"; private Vector drivers = new Vector();
private PrintWriter logPrint;
private Hashtable pools = new Hashtable();
private SystemParameter sp = new SystemParameter();
/**
è¿åå¯ä¸å®ä¾ãå¦ææ¯ç¬¬ä¸æ¬¡è°ç¨æ¤æ¹æ³ï¼åå建å®ä¾
*/
static synchronized public DBConnectionManager getInstance()
{
if (instance == null)
{
instance = new DBConnectionManager();
}
clients++;
return instance;
} /**
建æå½æ°ç§æ以é²æ¢å
¶å®å¯¹è±¡å建æ¬ç±»å®ä¾
*/
public DBConnectionManager()
{
init();
} /** Added by leo on 2001-5-23
使ç¨é»è®¤çè¿æ¥æ± å,å建并è¿åæ°è¿æ¥
*
* @return Connection å¯ç¨è¿æ¥ænull
*/
public Connection getConnection()
{
return getConnection(DEFAULTCONNECTIONPOOL);
} /**
è·å¾ä¸ä¸ªå¯ç¨ç(空é²ç)è¿æ¥.å¦æ没æå¯ç¨è¿æ¥,ä¸å·²æè¿æ¥æ°å°äºæ大è¿æ¥æ°
éå¶,åå建并è¿åæ°è¿æ¥
*
* @param name å¨å±æ§æ件ä¸å®ä¹çè¿æ¥æ± åå
* @return Connection å¯ç¨è¿æ¥ænull
*/
public Connection getConnection(String name)
{
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null)
{
return pool.getConnection();
}
return null;
} /**
è·å¾ä¸ä¸ªå¯ç¨è¿æ¥.è¥æ²¡æå¯ç¨è¿æ¥,ä¸å·²æè¿æ¥æ°å°äºæ大è¿æ¥æ°éå¶,
åå建并è¿åæ°è¿æ¥.å¦å,å¨æå®çæ¶é´å
çå¾
å
¶å®çº¿ç¨éæ¾è¿æ¥.
*
* @param name è¿æ¥æ± åå
* @param time 以毫ç§è®¡ççå¾
æ¶é´
* @return Connection å¯ç¨è¿æ¥ænull
*/
public Connection getConnection(String name, long time)
{
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null)
{
return pool.getConnection(time);
}
return null;
} /** Added by leo on 2001-5-23
å°è¿æ¥å¯¹è±¡è¿åç»é»è®¤çè¿æ¥æ±
*
* @param con è¿æ¥å¯¹è±¡
*/
public void freeConnection(Connection con)
{
String name = DEFAULTCONNECTIONPOOL;
freeConnection(name, con);
} /**
å°è¿æ¥å¯¹è±¡è¿åç»ç±ååæå®çè¿æ¥æ±
*
* @param name å¨å±æ§æ件ä¸å®ä¹çè¿æ¥æ± åå
* @param con è¿æ¥å¯¹è±¡
*/
public void freeConnection(String name, Connection con)
{
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null)
{
pool.freeConnection(con);
}
} /**
å
³éææè¿æ¥,æ¤é驱å¨ç¨åºç注å
*/
public synchronized void release()
{ // çå¾
ç´å°æåä¸ä¸ªå®¢æ·ç¨åºè°ç¨
if (--clients != 0)
{
return;
}
Enumeration allPools = pools.elements();
while (allPools.hasMoreElements())
{
DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();
pool.release();
}
Enumeration allDrivers = drivers.elements();
while (allDrivers.hasMoreElements())
{
Driver driver = (Driver) allDrivers.nextElement();
try
{
DriverManager.deregisterDriver(driver);
log("æ¤æ¶JDBC驱å¨ç¨åº" + driver.getClass().getName() + "ç注å");
}
catch (SQLException e)
{
log(e, "æ æ³æ¤æ¶ä¸åJDBC驱å¨ç¨åºç注å" + driver.getClass().getName());
}
}
} /**
æ ¹æ®æå®å±æ§å建è¿æ¥æ± å®ä¾.
*
* @param props è¿æ¥æ± å±æ§
*/
private void createPools()
{
String poolName = sp.DBPOOLNAME;
String url = sp.DBDRIVER_URL;
String user = sp.DBUSER;
String password = sp.DBPASSWORD;
int max = sp.DBMAXCONN;
DBConnectionPool pool =
new DBConnectionPool(poolName, url, user, password, max);
pools.put(poolName, pool);
log("æåå建è¿æ¥æ± " + poolName); } /**
读åå±æ§å®æåå§å
*/
private void init()
{ logPrint=new PrintWriter(System.out);
loadDrivers();
createPools();
} /**
è£
è½½å注åææJDBC驱å¨ç¨åº
*
* @param props å±æ§
*/
private void loadDrivers()
{
String driverClassName = sp.DBDRIVER;
try
{
Driver driver =
(Driver) Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
log("æåJDBC注å驱å¨ç¨åº" + driverClassName);
}
catch (Exception e)
{
log("æ æ³æ³¨åJDBC驱å¨ç¨åºï¼" + driverClassName + "ï¼é误ï¼" + e);
}
} /**
å°ææ¬ä¿¡æ¯åå
¥æ¥å¿æ件
缺ç为packaged
*/
void log(String msg)
{
logPrint.println(new java.util.Date() + ": " + msg);
} /**
å°ææ¬ä¿¡æ¯ä¸å¼å¸¸åå
¥æ¥å¿æ件
*/
void log(Throwable e, String msg)
{
logPrint.println(new java.util.Date() + ": " + msg);
e.printStackTrace(logPrint);
}}------------------------------------------------------class DBConnectionPool
{
private int checkedOut;
private PrintWriter logPrint;
private Vector freeConnections = new Vector();
private int maxConn;
private String name;
private String password;
private String URL;
private String user; /**
å建æ°çè¿æ¥æ±
*@param name è¿æ¥æ± åå
*@param URL æ°æ®åºçJDBC URL
*@param user æ°æ®åºå¸å·,ænull
*@param password å¯ç ,ænull
*@param maxConn æ¤è¿æ¥æ± å
许建ç«çæ大è¿æ¥æ°
*/
public DBConnectionPool(String name, String URL, String user, String password, int maxConn)
{
this.name = name;
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
logPrint = new PrintWriter(System.out);
} /**
å°ä¸å使ç¨çè¿æ¥è¿åç»è¿æ¥æ±
*@param con 客æ·ç¨åºéæ¾çè¿æ¥
*/
public synchronized void freeConnection(Connection con)
{
freeConnections.addElement(con);
checkedOut--;
notifyAll();
} /**
ä»è¿æ¥æ± è·å¾ä¸ä¸ªå¯ç¨è¿æ¥ï¼å¦æ²¡æ空é²çè¿æ¥ä¸å½åè¿æ¥æ°å°äºæ大è¿æ¥æ°ï¼åå建æ°è¿æ¥ï¼
å¦åæ¥ç»è®°ä¸ºå¯ç¨çè¿æ¥ä¸åææï¼åä»åéå é¤ä¹ï¼ç¶åéå½è°ç¨èªå·±ä»¥å°è¯æ°çå¯ç¨è¿æ¥
*/
public synchronized Connection getConnection()
{
Connection con = null;
if(freeConnections.size() > 0)
{// è·ååéä¸ç¬¬ä¸ä¸ªå¯ç¨è¿æ¥
con = (Connection)freeConnections.firstElement();
freeConnections.removeElementAt(0);
try
{
if(con.isClosed())
{
log("ä»è¿æ¥æ± " + name + "å é¤ä¸ä¸ªæ æè¿æ¥");
con = getConnection();
}
}
catch(SQLException e)
{
log("ä»è¿æ¥æ± " + name + "å é¤ä¸ä¸ªæ æè¿æ¥");
con = getConnection();
}
}
else if(maxConn == 0 || checkedOut < maxConn)
{
con = newConnection();
}
if(con != null)
{
checkedOut++;
}
return con;
} /**
ä»è¿æ¥æ± è·åå¯ç¨è¿æ¥ï¼å¯ä»¥æå®å®¢æ·ç¨åºè½å¤çå¾
çæé¿æ¶é´
*@param timeout 以毫ç§è®¡ççå¾
æ¶é´éå¶
*/
public synchronized Connection getConnection(long timeout)
{
long startTime = new Date().getTime();
Connection con;
while((con = getConnection()) == null)
{
try
{
wait(timeout);
}
catch(InterruptedException e){}
if((new Date().getTime() - startTime) >= timeout)
{
return null;
}
}
return con;
} /**
å
³éææè¿æ¥
*/
public synchronized void release()
{
Enumeration allConnections = freeConnections.elements();
while(allConnections.hasMoreElements())
{
Connection con = (Connection)allConnections.nextElement();
try
{
con.close();
log("å
³éè¿æ¥æ± " + name + "ä¸çä¸ä¸ªè¿æ¥");
}
catch(SQLException e)
{
log(e, "æ æ³å
³éè¿æ¥æ± " + name + "ä¸çè¿æ¥");
}
}
freeConnections.removeAllElements();
} /**
å建æ°çè¿æ¥
*/
private Connection newConnection()
{
Connection con = null;
try
{
if(user == null)
{
con = DriverManager.getConnection(URL);
}
else
{
con = DriverManager.getConnection(URL, user, password);
}
log("è¿æ¥æ± " + name + "å建ä¸ä¸ªæ°çè¿æ¥");
}
catch(SQLException e)
{
log(e, "æ æ³å建ä¸åURLçè¿æ¥" + URL);
return null;
}
return con;
} /**
å°ææ¬ä¿¡æ¯åå
¥æ¥å¿æ件
缺ç为packaged
*/
void log(String msg)
{
logPrint.println(new Date() + ": " + msg);
} /**
å°ææ¬ä¿¡æ¯ä¸å¼å¸¸åå
¥æ¥å¿æ件
*/
void log(Throwable e, String msg)
{
logPrint.println(new Date() + ": " + msg);
e.printStackTrace(logPrint);
}
}
---------------------------------DBConnectionManager connMgr = DBConnectionManager.getInstance();
public Connection createConnection()
{ Connection dBConn = connMgr.getConnection(new SystemParameter().DBPOOLNAME);
return dBConn;
} public void closeConnection(Connection conn)
{
connMgr.freeConnection(new SystemParameter().DBPOOLNAME, conn);
connMgr.release(); }
ç»è¿æµè¯ï¼ä½¿ç¨è¿ç§æ¹æ³ï¼ä¼é ææ°æ®åºå¤§éæ»éï¼è¯æ该è¿æ¥æ± å®ç°æ¹æ³åå¨éæ£ä¹å¯è½æ¯å 为å®ä¸å±äºwebserver容å¨èªèº«çï¼ä¸å
·æå¯ç®¡çæ§ã2.3 tomcatè¿æ¥æ± public Connection createConnection()
{ try
{
Context initCtx = new InitialContext();
DataSource ds;
ds = (DataSource) initCtx.lookup("java:comp/env/"+new SystemParameter().JDBC_SOURCE);
return ds.getConnection();
}
catch (NamingException e)
{ e.printStackTrace();
return null;
}
catch (SQLException e)
{ e.printStackTrace();
return null;
} } public void closeConnection(Connection conn)
{
if(conn!=null)try
{
conn.close();
conn=null;
}
catch (SQLException e)
{
e.printStackTrace();
}
}2.4 weblogicè¿æ¥æ± public Connection createConnection()
{
Context ctx = null;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
ht.put(Context.PROVIDER_URL, URL);
// ht.put(Context.SECURITY_PRINCIPAL, "admin");
// ht.put(Context.SECURITY_CREDENTIALS, "11111111"); Connection conn = null; try
{
ctx = new InitialContext(ht);
DataSource ds = (DataSource) ctx.lookup(POOL_NAME);
return ds.getConnection();
}
catch (NamingException e)
{ e.printStackTrace();
}
catch (SQLException e)
{ e.printStackTrace();
}
return null; } public void closeConnection(Connection conn)
{
if (conn != null)
try
{
conn.close();
conn = null;
}
catch (SQLException e)
{ e.printStackTrace();
} }3ï¼4çæ¹æ³æ¯è¾å¥½
温馨提示:内容为网友见解,仅供参考