串連池JAVA實現__JAVA

來源:互聯網
上載者:User
package com.dalong.connectionpool;
import java.util. HashMap;
import java.util.Vector;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
/**
* <p>Title: </p>
* <p>Description: test</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: home</p>
* @author dalong
* @version 1.0
*/

public class ConnectionPool {

// 串連池的管理器,首先初始化,僅僅有一個對象,管理 串連池
private static HashMap connectionPoolManager=new HashMap();
//沒有用過的 串連池,用vector實現同步
private static Vector noUseConnectionPool;
//沒有用過的 串連池
private static HashMap nowUseConnectionPool;

private static String dbDriver="odbc:jdbc:OdbcJdbcDriver";
private static String dbUrl="dalong@XX";
private static String userName="dalong";
private static String userPassword="dalong";

//預設為100個 串連池
private static int MAX_POOL=100;

//singleTon 設計模式
private ConnectionPool(String driver,String url,String name,String password,int max)
throws ClassNotFoundException {
Class.forName(driver);
dbUrl=url;
userName=name;
userPassword=password;
MAX_POOL=max;
}
public static ConnectionPool getConnManagerInstance(String poolName)
throws ClassNotFoundException{
ConnectionPool tempPool=(ConnectionPool)connectionPoolManager.get(poolName);
if(tempPool==null){
tempPool=new ConnectionPool(dbDriver,dbUrl,userName,userPassword,MAX_POOL);
connectionPoolManager.put(poolName,tempPool);
return tempPool;
}else
return tempPool;
}

//通過 串連池獲得真正的連結
public static Connection getConnection() throws java.sql.SQLException{
Connection conn=null;
synchronized(noUseConnectionPool){
if(noUseConnectionPool.size()>0){
conn=(Connection)noUseConnectionPool.firstElement();
noUseConnectionPool.remove(conn);
return conn;
}
}
//如果資料庫 串連池沒有連結了,自己建立一個
if(conn==null){
conn=createConnection(dbDriver,dbUrl,userName,userPassword);
}else if(conn.isClosed()){
nowUseConnectionPool.remove(conn);
conn=createConnection(dbDriver,dbUrl,userName,userPassword);
}
conn.setAutoCommit(false);
nowUseConnectionPool.put(conn,conn);
return conn;
}

//如果 串連池沒有連結了,就需要產生一個連結
private static Connection createConnection(String driver,String url,String user,String password)
throws java.sql.SQLException{
Connection conn=DriverManager.getConnection(url,user,password);
return conn;
}
public static void releaseConnection(Connection conn,boolean isCommit)
throws java.sql.SQLException{
if(isCommit)
conn.commit();
else
conn.rollback();
nowUseConnectionPool.remove(conn);
if(noUseConnectionPool.size() + nowUseConnectionPool.size()<MAX_POOL){
synchronized(noUseConnectionPool){
noUseConnectionPool.add(conn);
}
}else{
conn.close();
}
}

public static void main(String[] args) {
//測試類比10個客戶
for (int i = 0; i < 10; i++) {
try {
//xxxx 一般為屬性檔案讀取
ConnectionPool pool = ConnectionPool.getConnManagerInstance("xxxx");
Connection conn = pool.getConnection();

}catch (SQLException ex1) {
//處理異常
}
catch (ClassNotFoundException ex) {
//處理異常
}
}
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.