Java模擬.NET的連線池 (轉)
在之前:
的的本身包含連線池功能,而是在第三方開發包中提高的連線池功能因此,需要去第三方的連線池包,但是java的連線池一般都是在或者B/S中使用的(雖然也有C/S下的連線池如Borland 在Jbuilder中提供的),在一個服務性系統中使用起來不是很方便.再說使用第三方的開發包也不利於維護.因此決定自己寫一個連線池的開發包.此連線池中主要解決的是提高訪問,並且儘可能減少連線數目.
說明:
此連線池有三個類和一個介面組成,三個類分別是:
ConnectionPool 資料庫連線池,可以透過此類來使用連線池的功能.
PoolConnection 一個實現了java..Connection的w類,用來和資料庫進行通訊.
theOnClose 實現了介面OnConnectionClose的一個類用還處理釋放資料庫連線的是動作決定是關閉資料庫還是返回池中
介面 :
OnConnectionClose:此介面是一個宣告,因為本人很不喜歡java的事件機制因此,我經常自己寫一些介面來模擬時間,沒有java的事件機制那麼強大也沒有那麼複雜.但是要比java的事件機制要高那麼一點點(笑:).
本來要這幾個小類的UML圖的但是我一點IE就死,所以算了.就只上傳程式碼.還望方家指正.
程式碼:
package DBTools;
/**
*
T資料庫連線池工具
*
模擬.NET的連線池,俺一直以為.NET比java優秀
*
Copyright: 可以隨便使用,如果有改進最好通知俺
*
Company:自己作品
* @author董平雷
* @version 1.0
*/
import java.sql.*;
import java.util.*;
import java.io.*;
interface OnConnectionClose {
public void Action(PoolConnection sender);
}
public class DBConnectionPool {
private static Vector pConnectionVector = new Vector();
// private static int Count=0;
private static int minCount = 1;
private static String URL = "";
private static String User = "";
private static String Pass = "";
private static String Name="";
synchronized public static void setMinCount(int Value) {
minCount = Value;
}
synchronized public static int getMinCount() {
return minCount;
}
synchronized public static int getCout() {
return pConnectionVector.size();
}
synchronized public static Connection getConnection() throws SQLException {
PoolConnection pConnection = null;
// int aCount=pConnectionVector.size();
for (int i = 0; i < pConnectionVector.size(); i++) {
oCon = pConnectionVector.elementAt(i);
if (oCon instanceof PoolConnection) {
PoolConnection aCon = (PoolConnection) oCon;
if (!aCon.isUsed()) {
pConnection = aCon;
break;
}
}
}
if (pConnection == null) {
pConnection = getNewConnection();
pConnectionVector.add(pConnection);
}
return pConnection;
}
private static PoolConnection getNewConnection() throws SQLException {
try
{
Class.forName( DriverName);
}catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
PoolConnection con = new PoolConnection(URL, User, Password);
con.setOnClose(new theOnClose(pConnectionVector));
return con;
}
synchronized public static void Set(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) {
v.remove(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 {
oos.defaultWriteObject();
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
}
protected PoolConnection() {
}
public PoolConnection(String Url, String User, String Password) throws
SQLException {
aCon = DriverManager.getConnection(Url, User, Password);
closed = false;
inUse=true;
}
public PoolConnection(String Url) throws Exception {
aCon = DriverManager.getConnection(Url);
closed = false;
inUse=true;
}
public Statement createStatement() throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method createStatement() not yet implemented.");
return aCon.createStatement();
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql);
}
public CallableStatement prepareCall(String sql) throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareCall() not yet implemented.");
return aCon.prepareCall(sql);
}
public String nativeSQL(String sql) throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method nativeSQL() not yet implemented.");
return aCon.nativeSQL(sql);
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setAutoCommit() not yet implemented.");
aCon.setAutoCommit(autoCommit);
}
public boolean getAutoCommit() throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method getAutoCommit() not yet implemented.");
return aCon.getAutoCommit();
}
public void commit() throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method commit() not yet implemented.");
aCon.commit();
}
public void rollback() throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method rollback() not yet implemented.");
aCon.rollback();
}
public void close() throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method close() not yet implemented.");
if(DBConnectionPool.getCout()<=DBConnectionPool.getMinCount())
{
inUse = false;
}else
{
closeConnection();
}
}
public boolean isClosed() throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method isClosed() not yet implemented.");
return closed;
}
public DatabaseMetaData getMetaData() throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method getMetaData() not yet implemented.");
return aCon.getMetaData();
}
public void setReadOnly(boolean readOnly) throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setReadOnly() not yet implemented.");
aCon.setReadOnly(readOnly);
}
public boolean isReadOnly() throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method isReadOnly() not yet implemented.");
return isReadOnly();
}
public void setCatalog(String catalog) throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setCatalog() not yet implemented.");
aCon.setCatalog(catalog);
}
public String getCatalog() throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method getCatalog() not yet implemented.");
return aCon.getCatalog();
}
public void setTransactionIsolation(int level) throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setTransactionIsolation() not yet implemented.");
aCon.setTransactionIsolation(level);
}
public int getTransactionIsolation() throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method getTransactionIsolation() not yet implemented.");
return aCon.getTransactionIsolation();
}
public SQLWarning getWarnings() throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method getWarnings() not yet implemented.");
return aCon.getWarnings();
}
public void clearWarnings() throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method clearWarnings() not yet implemented.");
aCon.clearWarnings();
}
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws
SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method createStatement() not yet implemented.");
return aCon.createStatement(resultSetType, resultSetConcurrency);
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws
SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws
SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareCall() not yet implemented.");
return aCon.prepareCall(sql, resultSetType, resultSetConcurrency);
}
public Map getTypeMap() throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method getTypeMap() not yet implemented.");
return aCon.getTypeMap();
}
public void setTypeMap(Map map) throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method setTypeMap() not yet implemented.");
aCon.setTypeMap(map);
}
public void setHoldability(int holdability) throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setHoldability() not yet implemented.");
aCon.setHoldability(holdability);
}
public int getHoldability() throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method getHoldability() not yet implemented.");
return aCon.getHoldability();
}
public Savepoint setSavepoint() throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method setSavepoint() not yet implemented.");
return setSavepoint();
}
public Savepoint setSavepoint(String name) throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setSavepoint() not yet implemented.");
return setSavepoint(name);
}
public void rollback(Savepoint savepoint) throws SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method rollback() not yet implemented.");
aCon.rollback(savepoint);
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method releaseSavepoint() not yet implemented.");
aCon.releaseSavepoint(savepoint);
}
public Statement createStatement(int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws
SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method createStatement() not yet implemented.");
return aCon.createStatement(resultSetType, resultSetConcurrency,
resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws
SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql, resultSetType, resultSetConcurrency,
resultSetHoldability);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws
SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareCall() not yet implemented.");
return aCon.prepareCall(sql, resultSetType, resultSetConcurrency,
resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws
SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql, autoGeneratedKeys);
}
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws
SQLException {
Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql, columnIndexes);
}
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws
SQLException {
Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql, columnNames);
}
public void closeConnection() throws SQLException {
if (onClose != null) {
onClose.Action(this);
}
aCon.close();
}
public boolean isUsed() {
return inUse;
}
public void use() {
inUse = true;
}
public void setOnClose(OnConnectionClose Action) {
onClose = Action;
}
}
以上就是我所寫的連線池程式碼.
使用方法:
DBTools.DBConnectionPool.SetJDBC("jdbc:://fireBird/trmg?useUnicode=true&characterEncoding=GB2312",
"Administrator","");
DBTools.DBConnectionPool.setDriverName("com.mysql.jdbc.Driver");
java.sql.Connection con = DBTools.DBConnectionPool.getConnection();
當使用完畢了別忘記將con關閉:).
好像現在使用java的人不允許人說java的問題,java的回收存在大問題.記憶體洩漏的厲害,建議如非必要不要使用new來生成新的.這樣可能可以讓我們的系統可以活的更長久一些.還有下java效能慘不忍睹,在俺測試的平臺中反而是最高的.鬱悶鬱悶不是罪.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-962946/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【JDBC】java連線池模擬測試 連線oracleJDBCJavaOracle
- 【JDBC】java連線池模擬測試連線Oracle資料庫指令碼參考JDBCJavaOracle資料庫指令碼
- ADO.NET連線池寫法
- Java篇-DBUtils與連線池Java
- charles手機模擬器的連線
- java操作redis叢集連線池JavaRedis
- 連線池
- Java Druid資料庫連線池+SpringJDBCJavaUI資料庫SpringJDBC
- [轉帖]HikariCP連線池引數解釋
- Tomcat 的 JDBC 連線池TomcatJDBC
- django連線池Django
- HTTP連線池HTTP
- Java 客戶端 Jedis和JedisPool 連線池Java客戶端
- Java GenericObjectPool 物件池化技術--SpringBoot sftp 連線池工具類JavaObject物件Spring BootFTP
- Android Studio與夜神模擬器連線Android
- ADO.NET入門教程之資料庫連線池資料庫
- Http持久連線與HttpClient連線池HTTPclient
- 連線池和連線數詳解
- golang連線MySQL時候的連線池設定GolangMySql
- HikariCP連線池的學習
- ElasticSearch連線池建立Elasticsearch
- 自定義連線池
- ServiceStack.Redis的原始碼分析(連線與連線池)Redis原始碼
- C#中的連線池管理C#
- Swoole MySQL 連線池的實現MySql
- Druid-目前最好的連線池UI
- 資料庫連線池-Druid資料庫連線池原始碼解析資料庫UI原始碼
- go 語言連線池Go
- Golang SQL連線池梳理GolangSQL
- 【MySQL】自定義資料庫連線池和開源資料庫連線池的使用MySql資料庫
- python+Appium+夜神模擬器連線---踩坑PythonAPP
- 《四 資料庫連線池原始碼》手寫資料庫連線池資料庫原始碼
- 資料庫連線池設計和實現(Java版本)資料庫Java
- Java技術分享:什麼是資料庫連線池?Java資料庫
- PAT-B 1025 反轉連結串列【模擬+對映】
- 設計一個可靠的連線池
- HikariCP-史上最快速的連線池
- 【JDBC】使用OracleDataSource建立連線池用於連線OracleJDBCOracle
- C# HttpClient使用和注意事項,.NET Framework連線池併發限制C#HTTPclientFramework