public void closeConnection(Connection conn) throws SQLException {
if ( log.isDebugEnabled() ) checkedOut--;
//synchronized pool to avoid concurrence error.
synchronized (pool) { int currentSize = pool.size(); if ( currentSize < poolSize ) { if ( log.isTraceEnabled() ) log.trace("returning connection to pool, pool size: " + (currentSize + 1) );
//add to pool pool.add(conn); return; } }
log.debug("closing JDBC connection");
conn.close();
}
|
public Connection getConnection() throws SQLException {
if ( log.isTraceEnabled() ) log.trace( "total checked-out connections: " + checkedOut );
//synchronized pool to avoid concurrence error
synchronized (pool) { if ( !pool.isEmpty() ) {
//if the pool is not empty,return connection from the pool
int last = pool.size() - 1; if ( log.isTraceEnabled() ) { log.trace("using pooled JDBC connection, pool size: " + last); checkedOut++; } Connection pooled = (Connection) pool.remove(last); if (isolation!=null) pooled.setTransactionIsolation( isolation.intValue() ); if ( pooled.getAutoCommit()!=autocommit ) pooled.setAutoCommit(autocommit); return pooled; } }
log.debug("opening new JDBC connection"); //create new connection. Connection conn = DriverManager.getConnection(url, connectionProps); if (isolation!=null) conn.setTransactionIsolation( isolation.intValue() ); if ( conn.getAutoCommit()!=autocommit ) conn.setAutoCommit(autocommit);
if ( log.isDebugEnabled() ) { log.debug( "created connection to: " + url + ", Isolation Level: " + conn.getTransactionIsolation() ); } if ( log.isTraceEnabled() ) checkedOut++;
return conn; }
|
so you can see that when the connection will close,it actually not close and add to the pool if the pool size is not full.
in other way , we can firstly create a number of connection in the pool and when the pool is used out then create new connection to the call.
I will see the C3P0ConnectionProvider next.
A connection provider that uses java.sql.DriverManager. This provider
also implements a very
rudimentary connection pool.
Note that you have to copy the required library into your classpath and use different connection pooling settings if you want to use a
production-quality third party JDBC pooling software.
posted on 2006-11-08 12:23
R.Zeus 阅读(658)
评论(0) 编辑 收藏 所属分类:
Hibernate