Skip to content

Commit

Permalink
Consistent suppression of get/clearWarnings without target connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Aug 1, 2019
1 parent 762ea3e commit d8e624e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -354,18 +354,12 @@ else if (method.getName().equals("setHoldability")) {
this.holdability = (Integer) args[0];
return null;
}
else if (method.getName().equals("commit")) {
else if (method.getName().equals("commit") || method.getName().equals("rollback")) {
// Ignore: no statements created yet.
return null;
}
else if (method.getName().equals("rollback")) {
// Ignore: no statements created yet.
return null;
}
else if (method.getName().equals("getWarnings")) {
return null;
}
else if (method.getName().equals("clearWarnings")) {
else if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
// Ignore: no warnings to expose yet.
return null;
}
else if (method.getName().equals("close")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,6 @@ else if (method.getName().equals("isWrapperFor")) {
return true;
}
}
else if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
// Avoid creation of target Connection on pre-close cleanup (e.g. in Hibernate Session)
return null;
}
else if (method.getName().equals("close")) {
// Handle close method: only close if not within a transaction.
DataSourceUtils.doReleaseConnection(this.target, this.targetDataSource);
Expand All @@ -218,6 +214,10 @@ else if (method.getName().equals("isClosed")) {
}

if (this.target == null) {
if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
// Avoid creation of target Connection on pre-close cleanup (e.g. Hibernate Session)
return null;
}
if (this.closed) {
throw new SQLException("Connection handle already closed");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,8 +48,18 @@
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.support.TransactionTemplate;

import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.inOrder;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.times;
import static org.mockito.BDDMockito.verify;
import static org.mockito.BDDMockito.willThrow;

/**
* @author Juergen Hoeller
Expand Down Expand Up @@ -117,6 +127,7 @@ private void doTestTransactionCommitRestoringAutoCommit(
if (lazyConnection) {
given(con.getAutoCommit()).willReturn(autoCommit);
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
given(con.getWarnings()).willThrow(new SQLException());
}

if (!lazyConnection || createStatement) {
Expand All @@ -142,6 +153,10 @@ protected void doInTransactionWithoutResult(TransactionStatus status) throws Run
if (createStatement) {
tCon.createStatement();
}
else {
tCon.getWarnings();
tCon.clearWarnings();
}
}
catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
Expand Down Expand Up @@ -669,7 +684,6 @@ public void testPropagationRequiresNewWithExistingTransactionAndUnrelatedFailing
SQLException failure = new SQLException();
given(ds2.getConnection()).willThrow(failure);


final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);

Expand Down Expand Up @@ -974,12 +988,12 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).setAutoCommit(true);
verify(con).close();

}

@Test
public void testTransactionAwareDataSourceProxy() throws Exception {
given(con.getAutoCommit()).willReturn(true);
given(con.getWarnings()).willThrow(new SQLException());

TransactionTemplate tt = new TransactionTemplate(tm);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
Expand All @@ -990,6 +1004,9 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
assertEquals(con, DataSourceUtils.getConnection(ds));
TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
try {
Connection tCon = dsProxy.getConnection();
tCon.getWarnings();
tCon.clearWarnings();
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
// should be ignored
dsProxy.getConnection().close();
Expand Down

0 comments on commit d8e624e

Please sign in to comment.