熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

如何使用Java模擬.NET的連接池

2013-11-23 19:25:26  來源: Java核心技術 

  作者 : manboo

  NET的ADONET的本身包含連接池功能而java是在第三方開發包中提高的連接池功能因此需要去下載第三方的連接池包但是java的連接池一般都是在EJB或者B/S系統中使用的(雖然也有C/S下的連接池如Borland 在Jbuilder中提供的)在一個服務性系統中使用起來不是很方便再說使用第三方的開發包也不利於維護因此決定自己寫一個連接池的開發包此連接池中主要解決的是提高數據庫訪問性能並且盡可能減少連接數目

說明:

   此連接池有三個類和一個接口組成三個類分別是:

DBConnectionPool 數據庫連接池用戶可以通過此類來使用連接池的功能

PoolConnection 一個實現了javasqlConnection的warp類用來和數據庫進行通訊

theOnClose 實現了接口OnConnectionClose的一個類用還處理釋放數據庫連接的是動作決定是關閉數據庫還是返回池中

接口 :

  OnConnectionClose:此接口是一個聲明因為本人很不喜歡java的事件機制因此我經常自己寫一些接口來模擬時間沒有java的事件機制那麼強大也沒有那麼復雜但是效率要比java的事件機制要高那麼一點點(笑:)

本來要上傳這幾個小類的UML圖的但是我一點IE就死所以算了就只上傳代碼還望方家指正

代碼:

   package DBTools;

/**
* <p>T數據庫連接池工具 </p>
* <p>模擬NET的連接池俺一直以為NET比java優秀 </p>
* <p>Copyright: 可以隨便使用如果有改進最好通知俺</p>
* <p>Company:自己作品 </p>
* @author董平雷
* @version
*/
import javasql*;
import javautil*;
import javaio*;

interface OnConnectionClose {
 public void Action(PoolConnection sender);

}

public class DBConnectionPool {

 private static Vector pConnectionVector = new Vector();
 // private static int Count=;
 private static int minCount = ;
 private static String URL = ;
 private static String User = ;
 private static String Password = ;
 private static String DriverName=;
synchronized public static void setMinCount(int Value) {
  minCount = Value;
 }

synchronized public static int getMinCount() {
  return minCount;
 }

synchronized public static int getCout() {
  return pConnectionVectorsize();
 }

synchronized public static Connection getConnection() throws SQLException {
  PoolConnection pConnection = null;
  // int aCount=pConnectionVectorsize();

  for (int I = ; I < pConnectionVectorsize(); i++) {
   Object oCon = pConnectionVectorelementAt(i);
   if (oCon instanceof PoolConnection) {
    PoolConnection aCon = (PoolConnection) oCon;
    if (!aConisUsed()) {
     pConnection = aCon;
     break;
    }

   }

  }
  if (pConnection == null) {
   pConnection = getNewConnection();
   pConnectionVectoradd(pConnection);
  }
  return pConnection;

 }

 private static PoolConnection getNewConnection() throws SQLException {
  try
  {
    ClassforName( DriverName);
  }catch(ClassNotFoundException ex)
  {
   exprintStackTrace();
  }
  PoolConnection con = new PoolConnection(URL User Password);
  consetOnClose(new theOnClose(pConnectionVector));
  return con;
 }

synchronized public static void SetJDBC(String url String user String password) {
  URL = url;
  User = user;
  Password = password;

 }

synchronized public static void setURL(String url) {
  URL = url;

 }

synchronized public static String getUrl() {
  return URL;

 }
synchronized public static void setUser(String user)
 {
   User=user;
 }
synchronized public static String getUser()
 {
  return User;
 }
synchronized public static void setPassword(String password)
 {
  Password=password;
 }
synchronized public static String getPassword()
 {
  return Password;
 }

synchronized public static void setDriverName(String dName)
{
  DriverName=dName;

}
synchronized public static String getDriverName()
 {
  return DriverName;
 }
}  


   class theOnClose
  implements OnConnectionClose {
 private Vector v;
 public theOnClose(Vector vt) {
  v = vt;
 }

 public void Action(PoolConnection sender) {
  vremove(sender);

 }
}

class PoolConnection
  implements Connection Serializable{
 private Connection aCon = null;
 private boolean closed = false;
 private boolean inUse = false;
 private String DriverName;
 private OnConnectionClose onClose = null;
 private void writeObject(ObjectOutputStream oos) throws IOException {
   oosdefaultWriteObject();
 }
 private void readObject(ObjectInputStream ois) throws ClassNotFoundException IOException {
  oisdefaultReadObject();
 }
 protected PoolConnection() {
 }

 public PoolConnection(String Url String User String Password) throws
   SQLException {

  aCon = DriverManagergetConnection(Url User Password);
  closed = false;
  inUse=true;

 }

 public PoolConnection(String Url) throws Exception {
  aCon = DriverManagergetConnection(Url);
  closed = false;
  inUse=true;
 }

 public Statement createStatement() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method createStatement() not yet implemented);
  return aConcreateStatement();
 }

 public PreparedStatement prepareStatement(String sql) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method prepareStatement() not yet implemented);
  return aConprepareStatement(sql);
 }

 public CallableStatement prepareCall(String sql) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method prepareCall() not yet implemented);
  return aConprepareCall(sql);
 }

 public String nativeSQL(String sql) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method nativeSQL() not yet implemented);
  return aConnativeSQL(sql);
 }

 public void setAutoCommit(boolean autoCommit) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method setAutoCommit() not yet implemented);
  aConsetAutoCommit(autoCommit);
 }

 public boolean getAutoCommit() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method getAutoCommit() not yet implemented);
  return aCongetAutoCommit();
 }

 public void commit() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method commit() not yet implemented);
  mit();
 }

 public void rollback() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method rollback() not yet implemented);
  aConrollback();
 }

 public void close() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method close() not yet implemented);
  if(DBConnectionPoolgetCout()<=DBConnectionPoolgetMinCount())
  {
  inUse = false;
  }else
  {
  closeConnection();
  }


 }

 public boolean isClosed() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method isClosed() not yet implemented);
  return closed;
 }

 public DatabaseMetaData getMetaData() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method getMetaData() not yet implemented);
  return aCongetMetaData();
 }

 public void setReadOnly(boolean readOnly) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method setReadOnly() not yet implemented);
  aConsetReadOnly(readOnly);
 }

 public boolean isReadOnly() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method isReadOnly() not yet implemented);
  return isReadOnly();
 }

 public void setCatalog(String catalog) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method setCatalog() not yet implemented);
  aConsetCatalog(catalog);
 }

 public String getCatalog() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method getCatalog() not yet implemented);
  return aCongetCatalog();
 }

 public void setTransactionIsolation(int level) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method setTransactionIsolation() not yet implemented);
  aConsetTransactionIsolation(level);
 }

 public int getTransactionIsolation() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method getTransactionIsolation() not yet implemented);
  return aCongetTransactionIsolation();
 }

 public SQLWarning getWarnings() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method getWarnings() not yet implemented);
  return aCongetWarnings();
 }

 public void clearWarnings() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method clearWarnings() not yet implemented);
  aConclearWarnings();
 }

 public Statement createStatement(int resultSetType int resultSetConcurrency) throws
   SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method createStatement() not yet implemented);
  return aConcreateStatement(resultSetType resultSetConcurrency);
 }

 public PreparedStatement prepareStatement(String sql int resultSetType
                      int resultSetConcurrency) throws
   SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method prepareStatement() not yet implemented);
  return aConprepareStatement(sql resultSetType resultSetConcurrency);
 }

 public CallableStatement prepareCall(String sql int resultSetType
                    int resultSetConcurrency) throws
   SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method prepareCall() not yet implemented);
  return aConprepareCall(sql resultSetType resultSetConcurrency);
 }

 public Map getTypeMap() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method getTypeMap() not yet implemented);
  return aCongetTypeMap();
 }

 public void setTypeMap(Map map) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method setTypeMap() not yet implemented);
  aConsetTypeMap(map);
 }

 public void setHoldability(int holdability) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method setHoldability() not yet implemented);
  aConsetHoldability(holdability);
 }

 public int getHoldability() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method getHoldability() not yet implemented);
  return aCongetHoldability();
 }

 public Savepoint setSavepoint() throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method setSavepoint() not yet implemented);
  return setSavepoint();
 }

 public Savepoint setSavepoint(String name) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method setSavepoint() not yet implemented);
  return setSavepoint(name);
 }

 public void rollback(Savepoint savepoint) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method rollback() not yet implemented);
  aConrollback(savepoint);
 }

 public void releaseSavepoint(Savepoint savepoint) throws SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method releaseSavepoint() not yet implemented);
  aConreleaseSavepoint(savepoint);
 }

 public Statement createStatement(int resultSetType int resultSetConcurrency
                  int resultSetHoldability) throws
   SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method createStatement() not yet implemented);
  return aConcreateStatement(resultSetType resultSetConcurrency
                resultSetHoldability);
 }

 public PreparedStatement prepareStatement(String sql int resultSetType
                      int resultSetConcurrency
                      int resultSetHoldability) throws
   SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method prepareStatement() not yet implemented);
  return aConprepareStatement(sql resultSetType resultSetConcurrency
                 resultSetHoldability);
 }

 public CallableStatement prepareCall(String sql int resultSetType
                    int resultSetConcurrency
                    int resultSetHoldability) throws
   SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method prepareCall() not yet implemented);
  return aConprepareCall(sql resultSetType resultSetConcurrency
              resultSetHoldability);
 }

 public PreparedStatement prepareStatement(String sql int autoGeneratedKeys) throws
   SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method prepareStatement() not yet implemented);
  return aConprepareStatement(sql autoGeneratedKeys);
 }

 public PreparedStatement prepareStatement(String sql int[] columnIndexes) throws
   SQLException {
  /**@todo Implement this javasqlConnection method*/
  // throw new javalangUnsupportedOperationException(Method prepareStatement() not yet implemented);
  return aConprepareStatement(sql columnIndexes);
 }

 public PreparedStatement prepareStatement(String sql String[] columnNames) throws
   SQLException {
  /**@todo Implement this javasqlConnection method*/
  //throw new javalangUnsupportedOperationException(Method prepareStatement() not yet implemented);
  return aConprepareStatement(sql columnNames);
 }

 public void closeConnection() throws SQLException {
  if (onClose != null) {
   onCloseAction(this);
  }
  aConclose();

 }

 public boolean isUsed() {
  return inUse;

 }

 public void use() {
  inUse = true;
 }

 public void setOnClose(OnConnectionClose Action) {
  onClose = Action;

 }

}  

  以上就是我所寫的連接池代碼

使用方法:
DBToolsDBConnectionPoolSetJDBC(jdbc:mysql://fireBird/trmg?useUnicode=true&characterEncoding=GB
Administrator);
   DBToolsDBConnectionPoolsetDriverName(commysqljdbcDriver);

javasqlConnection con = DBToolsDBConnectionPoolgetConnection();

當使用完畢了別忘記將con關閉:)

好像現在使用java的人不允許人說java的問題java的內存回收存在大問題內存洩漏的厲害建議如非必要不要使用new來生成新的對象這樣可能可以讓我們的系統可以活的更長久一些還有linux下java性能慘不忍睹在俺測試的平台中win反而是最高的郁悶郁悶不是罪


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26842.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.