Skip to content

Commit

Permalink
Merge pull request #1510 from cherrylzhao/dev-new
Browse files Browse the repository at this point in the history
#1238 Release connection while channel have not transaction.
  • Loading branch information
terrymanu authored Nov 24, 2018
2 parents 16e6e75 + e958289 commit e36c977
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void close(final boolean forceClose) throws SQLException {
MasterVisitedManager.clear();
exceptions.addAll(closeStatements());
exceptions.addAll(closeResultSets());
if (ConnectionStatus.TERMINATED == status || forceClose) {
if (ConnectionStatus.TRANSACTION != status || forceClose) {
exceptions.addAll(releaseConnections(forceClose));
}
throwSQLExceptionIfNecessary(exceptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,13 @@ private void assertOneThreadResult() {
}

@Test
public void assertAutoCloseConnection() throws SQLException {
public void assertAutoCloseConnectionWithoutTransaction() throws SQLException {
BackendConnection actual;
try (BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL)) {
backendConnection.setLogicSchema(logicSchema);
backendConnection.setTransactionType(TransactionType.XA);
MockConnectionUtil.setCachedConnections(backendConnection, "ds1", 10);
when(backendDataSource.getConnections((ConnectionMode) any(), anyString(), eq(2))).thenReturn(MockConnectionUtil.mockNewConnections(2));
backendConnection.getConnections(ConnectionMode.MEMORY_STRICTLY, "ds1", 12);
backendConnection.setStatus(ConnectionStatus.TERMINATED);
mockResultSetAndStatement(backendConnection);
actual = backendConnection;
}
Expand All @@ -169,6 +167,24 @@ public void assertAutoCloseConnection() throws SQLException {
assertTrue(actual.getCachedStatements().isEmpty());
}

@Test
public void assertAutoCloseConnectionWithTransaction() throws SQLException {
BackendConnection actual;
try (BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL)) {
backendConnection.setLogicSchema(logicSchema);
MockConnectionUtil.setCachedConnections(backendConnection, "ds1", 10);
when(backendDataSource.getConnections((ConnectionMode) any(), anyString(), eq(2))).thenReturn(MockConnectionUtil.mockNewConnections(2));
backendConnection.getConnections(ConnectionMode.MEMORY_STRICTLY, "ds1", 12);
backendConnection.setStatus(ConnectionStatus.TRANSACTION);
mockResultSetAndStatement(backendConnection);
actual = backendConnection;
}
assertThat(actual.getConnectionSize(), is(12));
assertThat(actual.getCachedConnections().get("ds1").size(), is(12));
assertTrue(actual.getCachedResultSets().isEmpty());
assertTrue(actual.getCachedStatements().isEmpty());
}

@Test
public void assertAutoCloseConnectionWithException() {
BackendConnection actual = null;
Expand Down

0 comments on commit e36c977

Please sign in to comment.