JDBC自定义连接池过程详解
这篇文章主要介绍了JDBC自定义连接池过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
开发中,"获得连接"和"释放资源"是非常消耗系统资源的,为了解决此类性能问题可以采用连接池技术来共享连接Connection。
1、概述
用池来管理Connection,这样可以重复使用Connection.这样我们就不用创建Connection,用池来管理Connection对象,当使用完Connection对象后,将Connection对象归还给池,这样后续还可以从池中获取Connection对象,可以重新再利用这个连接对象啦。
java为数据库连接池提供了公共接口:javax.sql.DataSource,各个厂商需要让自己的连接池实现这个接口。
常见的连接池:DBCP,C3P0
2、自定义连接池
编写自定义连接池
1、创建连接池并实现接口javax.sql.DataSource,并使用接口中的getConnection()方法
2、提供一个集合,用于存放连接,可以采用LinkedList
3、后面程序如果需要,可以调用实现类getConnection(),并从list中获取链接。为保证当前连接只能提供给一个线程使用,所以我们需要将连接先从连接池中移除
4、当用户用完连接后,将连接归还到连接池中
3、自定义连接池采用装饰者设计模式
publicclassConnectionPoolimplementsConnection{
privateConnectionconnection;
privateLinkedListpool;
publicConnectionPool(Connectionconnection,LinkedListpool){
this.connection=connection;
this.pool=pool;
}
@Override
publicPreparedStatementprepareStatement(Stringsql)throwsSQLException{
returnconnection.prepareStatement(sql);
}
@Override
publicvoidclose()throwsSQLException{
pool.add(connection);
}
@Override
publicStatementcreateStatement()throwsSQLException{
returnnull;
}
@Override
publicCallableStatementprepareCall(Stringsql)throwsSQLException{
returnnull;
}
@Override
publicStringnativeSQL(Stringsql)throwsSQLException{
returnnull;
}
@Override
publicvoidsetAutoCommit(booleanautoCommit)throwsSQLException{
}
@Override
publicbooleangetAutoCommit()throwsSQLException{
returnfalse;
}
@Override
publicvoidcommit()throwsSQLException{
}
@Override
publicvoidrollback()throwsSQLException{
}
@Override
publicbooleanisClosed()throwsSQLException{
returnfalse;
}
@Override
publicDatabaseMetaDatagetMetaData()throwsSQLException{
returnnull;
}
@Override
publicvoidsetReadOnly(booleanreadOnly)throwsSQLException{
}
@Override
publicbooleanisReadOnly()throwsSQLException{
returnfalse;
}
@Override
publicvoidsetCatalog(Stringcatalog)throwsSQLException{
}
@Override
publicStringgetCatalog()throwsSQLException{
returnnull;
}
@Override
publicvoidsetTransactionIsolation(intlevel)throwsSQLException{
}
@Override
publicintgetTransactionIsolation()throwsSQLException{
return0;
}
@Override
publicSQLWarninggetWarnings()throwsSQLException{
returnnull;
}
@Override
publicvoidclearWarnings()throwsSQLException{
}
@Override
publicStatementcreateStatement(intresultSetType,intresultSetConcurrency)throwsSQLException{
returnnull;
}
@Override
publicPreparedStatementprepareStatement(Stringsql,intresultSetType,intresultSetConcurrency)throwsSQLException{
returnnull;
}
@Override
publicCallableStatementprepareCall(Stringsql,intresultSetType,intresultSetConcurrency)throwsSQLException{
returnnull;
}
@Override
publicMap>getTypeMap()throwsSQLException{
returnnull;
}
@Override
publicvoidsetTypeMap(Map>map)throwsSQLException{
}
@Override
publicvoidsetHoldability(intholdability)throwsSQLException{
}
@Override
publicintgetHoldability()throwsSQLException{
return0;
}
@Override
publicSavepointsetSavepoint()throwsSQLException{
returnnull;
}
@Override
publicSavepointsetSavepoint(Stringname)throwsSQLException{
returnnull;
}
@Override
publicvoidrollback(Savepointsavepoint)throwsSQLException{
}
@Override
publicvoidreleaseSavepoint(Savepointsavepoint)throwsSQLException{
}
@Override
publicStatementcreateStatement(intresultSetType,intresultSetConcurrency,intresultSetHoldability)throwsSQLException{
returnnull;
}
@Override
publicPreparedStatementprepareStatement(Stringsql,intresultSetType,intresultSetConcurrency,intresultSetHoldability)throwsSQLException{
returnnull;
}
@Override
publicCallableStatementprepareCall(Stringsql,intresultSetType,intresultSetConcurrency,intresultSetHoldability)throwsSQLException{
returnnull;
}
@Override
publicPreparedStatementprepareStatement(Stringsql,intautoGeneratedKeys)throwsSQLException{
returnnull;
}
@Override
publicPreparedStatementprepareStatement(Stringsql,int[]columnIndexes)throwsSQLException{
returnnull;
}
@Override
publicPreparedStatementprepareStatement(Stringsql,String[]columnNames)throwsSQLException{
returnnull;
}
@Override
publicClobcreateClob()throwsSQLException{
returnnull;
}
@Override
publicBlobcreateBlob()throwsSQLException{
returnnull;
}
@Override
publicNClobcreateNClob()throwsSQLException{
returnnull;
}
@Override
publicSQLXMLcreateSQLXML()throwsSQLException{
returnnull;
}
@Override
publicbooleanisValid(inttimeout)throwsSQLException{
returnfalse;
}
@Override
publicvoidsetClientInfo(Stringname,Stringvalue)throwsSQLClientInfoException{
}
@Override
publicvoidsetClientInfo(Propertiesproperties)throwsSQLClientInfoException{
}
@Override
publicStringgetClientInfo(Stringname)throwsSQLException{
returnnull;
}
@Override
publicPropertiesgetClientInfo()throwsSQLException{
returnnull;
}
@Override
publicArraycreateArrayOf(StringtypeName,Object[]elements)throwsSQLException{
returnnull;
}
@Override
publicStructcreateStruct(StringtypeName,Object[]attributes)throwsSQLException{
returnnull;
}
@Override
publicvoidsetSchema(Stringschema)throwsSQLException{
}
@Override
publicStringgetSchema()throwsSQLException{
returnnull;
}
@Override
publicvoidabort(Executorexecutor)throwsSQLException{
}
@Override
publicvoidsetNetworkTimeout(Executorexecutor,intmilliseconds)throwsSQLException{
}
@Override
publicintgetNetworkTimeout()throwsSQLException{
return0;
}
@Override
publicTunwrap(Classiface)throwsSQLException{
returnnull;
}
@Override
publicbooleanisWrapperFor(Class>iface)throwsSQLException{
returnfalse;
}
}
DataSourcePool
publicclassDataSourcePoolimplementsDataSource{
//1.创建1个容器用于存储Connection对象
privatestaticLinkedListpool=newLinkedList();
//2.创建5个连接放到容器中去
static{
for(inti=0;i<5;i++){
Connectionconn=JDBCUtils.getConnection();
//放入池子中connection对象已经经过改造了
ConnectionPoolconnectionPool=newConnectionPool(conn,pool);
pool.add(connectionPool);
}
}
/**
*重写获取连接的方法
*/
@Override
publicConnectiongetConnection()throwsSQLException{
Connectionconn=null;
//3.使用前先判断
if(pool.size()==0){
//4.池子里面没有,我们再创建一些
for(inti=0;i<5;i++){
conn=JDBCUtils.getConnection();
//放入池子中connection对象已经经过改造了
ConnectionPoolconnectionPool=newConnectionPool(conn,pool);
pool.add(connectionPool);
}
}
//5.从池子里面获取一个连接对象Connection
conn=pool.remove(0);
returnconn;
}
@Override
publicConnectiongetConnection(Stringusername,Stringpassword)throwsSQLException{
returnnull;
}
@Override
publicTunwrap(Classiface)throwsSQLException{
returnnull;
}
@Override
publicbooleanisWrapperFor(Class>iface)throwsSQLException{
returnfalse;
}
@Override
publicPrintWritergetLogWriter()throwsSQLException{
returnnull;
}
@Override
publicvoidsetLogWriter(PrintWriterout)throwsSQLException{
}
@Override
publicvoidsetLoginTimeout(intseconds)throwsSQLException{
}
@Override
publicintgetLoginTimeout()throwsSQLException{
return0;
}
@Override
publicLoggergetParentLogger()throwsSQLFeatureNotSupportedException{
returnnull;
}
}
测试代码如下
@Test
publicvoidtest1(){
Connectionconn=null;
PreparedStatementpstmt=null;
//1.创建自定义连接池对象
DataSourcedataSource=newDataSourcePool();
try{
//2.从池子中获取连接
conn=dataSource.getConnection();
Stringsql="insertintoUSERvalues(?,?)";
//3.必须在自定义的connection类中重写prepareStatement(sql)方法
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,"李四");
pstmt.setString(2,"1234");
introws=pstmt.executeUpdate();
System.out.println("rows:"+rows);
}catch(Exceptione){
thrownewRuntimeException(e);
}finally{
JDBCUtils.relase(conn,pstmt,null);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。