Permalink
Browse files

defaultReadonly/autoCommit/etc now get reset back to defaults on conn…

…ection close.
  • Loading branch information...
wwadge committed Jun 1, 2011
1 parent 8a1e5a2 commit 99d50d93137124d238a88bb430afc76c3babb5f1
@@ -58,8 +58,6 @@
*
*/
public class BoneCP implements Serializable {
/** Warning message. */
private static final String DISABLED_AUTO_COMMIT_WARNING = "Auto-commit was disabled but no commit/rollback was issued by the time this connection was closed. Performing rollback! Enable config setting detectUnresolvedTransactions for more debugging info.";
/** Warning message. */
private static final String THREAD_CLOSE_CONNECTION_WARNING = "Thread close connection monitoring has been enabled. This will negatively impact on your performance. Only enable this option for debugging purposes!";
/** Serialization UID */
@@ -297,19 +295,6 @@ protected Connection obtainRawInternalConnection()
result = DriverManager.getConnection(url, username, password);
}
if (this.defaultAutoCommit != null){
result.setAutoCommit(this.defaultAutoCommit);
}
if (this.defaultReadOnly != null){
result.setReadOnly(this.defaultReadOnly);
}
if (this.defaultCatalog != null){
result.setCatalog(this.defaultCatalog);
}
if (this.defaultTransactionIsolationValue != -1){
result.setTransactionIsolation(this.defaultTransactionIsolationValue);
}
return result;
}
@@ -649,32 +634,6 @@ protected void internalReleaseConnection(ConnectionHandle connectionHandle) thro
*/
protected void putConnectionBackInPartition(ConnectionHandle connectionHandle) throws SQLException {
if (this.resetConnectionOnClose && !connectionHandle.getInternalConnection().getAutoCommit() && !connectionHandle.isTxResolved()){
if (connectionHandle.getAutoCommitStackTrace() != null){
logger.warn(connectionHandle.getAutoCommitStackTrace());
connectionHandle.setAutoCommitStackTrace(null);
} else {
logger.warn(DISABLED_AUTO_COMMIT_WARNING);
}
connectionHandle.getInternalConnection().rollback();
connectionHandle.getInternalConnection().setAutoCommit(true);
}
// restore sanity
if (this.defaultAutoCommit != null){
connectionHandle.getInternalConnection().setAutoCommit(this.defaultAutoCommit);
}
if (this.defaultReadOnly != null){
connectionHandle.getInternalConnection().setReadOnly(this.defaultReadOnly);
}
if (this.defaultCatalog != null){
connectionHandle.getInternalConnection().setCatalog(this.defaultCatalog);
}
if (this.defaultTransactionIsolationValue != -1){
connectionHandle.getInternalConnection().setTransactionIsolation(this.defaultTransactionIsolationValue);
}
if (this.cachedPoolStrategy && connectionHandle.inUseInThreadLocalContext.get()){
// this might fail if we have a thread that takes up more than one thread
// (we only track one)
@@ -76,6 +76,16 @@
private long connectionCreationTimeInMs = System.currentTimeMillis();
/** Pool handle. */
private BoneCP pool;
/** Config setting. */
private Boolean defaultReadOnly;
/** Config setting. */
private String defaultCatalog;
/** Config setting. */
private int defaultTransactionIsolationValue;
/** Config setting. */
private Boolean defaultAutoCommit;
/** Config setting. */
private boolean resetConnectionOnClose;
/**
* If true, this connection might have failed communicating with the
* database. We assume that exceptions should be rare here i.e. the normal
@@ -202,6 +212,11 @@ private ConnectionHandle(String url, String username, String password,
this.url = url;
this.connection = obtainInternalConnection();
this.finalizableRefs = this.pool.getFinalizableRefs();
this.defaultReadOnly = pool.getConfig().getDefaultReadOnly();
this.defaultCatalog = pool.getConfig().getDefaultCatalog();
this.defaultTransactionIsolationValue = pool.getConfig().getDefaultTransactionIsolationValue();
this.defaultAutoCommit = pool.getConfig().getDefaultAutoCommit();
this.resetConnectionOnClose = pool.getConfig().isResetConnectionOnClose();
this.connectionTrackingDisabled = pool.getConfig().isDisableConnectionTracking();
this.statisticsEnabled = pool.getConfig().isStatisticsEnabled();
this.statistics = pool.getStatistics();
@@ -221,6 +236,20 @@ private ConnectionHandle(String url, String username, String password,
this.callableStatementCache = new StatementCache(cacheSize, pool.getConfig().isStatisticsEnabled(), pool.getStatistics());
this.statementCachingEnabled = true;
}
if (this.defaultAutoCommit != null){
setAutoCommit(this.defaultAutoCommit);
}
if (this.defaultReadOnly != null){
setReadOnly(this.defaultReadOnly);
}
if (this.defaultCatalog != null){
setCatalog(this.defaultCatalog);
}
if (this.defaultTransactionIsolationValue != -1){
setTransactionIsolation(this.defaultTransactionIsolationValue);
}
}
/** Obtains a database connection, retrying if necessary.
@@ -416,6 +445,32 @@ private void checkClosed() throws SQLException {
public void close() throws SQLException {
try {
if (!this.logicallyClosed) {
if (this.resetConnectionOnClose && !getAutoCommit() && !isTxResolved()){
if (this.autoCommitStackTrace != null){
logger.warn(this.autoCommitStackTrace);
this.autoCommitStackTrace = null;
} else {
logger.warn(DISABLED_AUTO_COMMIT_WARNING);
}
rollback();
setAutoCommit(true);
}
// restore sanity
if (this.defaultAutoCommit != null){
setAutoCommit(this.defaultAutoCommit);
}
if (this.defaultReadOnly != null){
setReadOnly(this.defaultReadOnly);
}
if (this.defaultCatalog != null){
setCatalog(this.defaultCatalog);
}
if (this.defaultTransactionIsolationValue != -1){
this.setTransactionIsolation(this.defaultTransactionIsolationValue);
}
this.logicallyClosed = true;
this.threadUsingConnection = null;
if (this.threadWatch != null){
@@ -923,34 +923,6 @@ public void testPutConnectionBackInPartitionWithResetConnectionOnClose() throws
}
/**
* Test method for {@link com.jolbox.bonecp.BoneCP#putConnectionBackInPartition(com.jolbox.bonecp.ConnectionHandle)}.
* @throws InterruptedException
* @throws SQLException
*/
@Test
public void testPutConnectionBackInPartitionWithResetConnectionOnCloseWithStackTrace() throws InterruptedException, SQLException {
expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes();
expect(mockPartition.getAvailableConnections()).andReturn(1).anyTimes();
expect(mockConnection.getOriginatingPartition()).andReturn(mockPartition).anyTimes();
expect(mockConnectionHandles.tryTransfer(mockConnection)).andReturn(false).anyTimes();
expect(mockConnectionHandles.offer(mockConnection)).andReturn(true).once();
expect(mockConnection.isTxResolved()).andReturn(false).once();
expect(mockConnection.getAutoCommitStackTrace()).andReturn("Foo").once();
mockConnection.setAutoCommitStackTrace(null);
Connection mockInternalConnection = createNiceMock(Connection.class);
expect(mockInternalConnection.getAutoCommit()).andReturn(false).once();
expect(mockConnection.getInternalConnection()).andReturn(mockInternalConnection).anyTimes();
mockInternalConnection.rollback();
mockInternalConnection.setAutoCommit(true);
replay(mockInternalConnection, mockPartition, mockConnectionHandles, mockConnection);
testClass.resetConnectionOnClose = true;
testClass.putConnectionBackInPartition(mockConnection);
verify(mockPartition, mockConnectionHandles, mockConnection);
}
/**
* Test method for com.jolbox.bonecp.BoneCP isConnectionHandleAlive.
@@ -74,6 +74,7 @@ public static void setup() throws IllegalArgumentException, IllegalAccessExcepti
// expect(mockConfig.getConnectionHook()).andReturn(null).anyTimes();
expect(mockConfig.getAcquireRetryDelayInMs()).andReturn(1000L).anyTimes();
expect(mockConfig.getAcquireRetryAttempts()).andReturn(0).anyTimes();
expect(mockConfig.getDefaultTransactionIsolationValue()).andReturn(-1).anyTimes();
expect(mockConfig.getConnectionHook()).andReturn(new CoverageHook()).anyTimes();
expect(mockConfig.isLazyInit()).andReturn(true).anyTimes();

0 comments on commit 99d50d9

Please sign in to comment.