diff --git a/sharding-proxy/src/main/java/io/shardingsphere/shardingproxy/backend/jdbc/connection/BackendConnection.java b/sharding-proxy/src/main/java/io/shardingsphere/shardingproxy/backend/jdbc/connection/BackendConnection.java index 1e5065c1e94bd..849c3593c3b22 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/shardingproxy/backend/jdbc/connection/BackendConnection.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/shardingproxy/backend/jdbc/connection/BackendConnection.java @@ -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); diff --git a/sharding-proxy/src/test/java/io/shardingsphere/shardingproxy/backend/jdbc/connection/BackendConnectionTest.java b/sharding-proxy/src/test/java/io/shardingsphere/shardingproxy/backend/jdbc/connection/BackendConnectionTest.java index 3501e8a1032cc..a8eb48b7c7d0a 100644 --- a/sharding-proxy/src/test/java/io/shardingsphere/shardingproxy/backend/jdbc/connection/BackendConnectionTest.java +++ b/sharding-proxy/src/test/java/io/shardingsphere/shardingproxy/backend/jdbc/connection/BackendConnectionTest.java @@ -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; } @@ -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;