java

位置:IT落伍者 >> java >> 浏览文章

如何使用Java模拟.NET的连接池


发布日期:2021年05月29日
 
如何使用Java模拟.NET的连接池

作者 : 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;

}

synchronizedpublic 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;

}

synchronizedpublic static String getUrl() {

return URL;

}

synchronizedpublic static void setUser(String user)

{

User=user;

}

synchronizedpublic static String getUser()

{

return User;

}

synchronizedpublic static void setPassword(String password)

{

Password=password;

}

synchronized public static String getPassword()

{

return Password;

}

synchronized public static voidsetDriverName(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反而是最高的郁闷郁闷不是罪

               

上一篇:常见排序算法的java实现

下一篇:JavaME程序 Run Anywhere-- 利用反射机制来动态加载声