Skip to content

Commit f5ec220

Browse files
authored
Alleviate connection leaks caused by Seata Client throwing exceptions (#34463)
1 parent f0880fd commit f5ec220

File tree

7 files changed

+399
-362
lines changed

7 files changed

+399
-362
lines changed

RELEASE-NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
### Bug Fixes
1515

16+
1. Alleviate connection leaks caused by Seata Client throwing exceptions - [#34463](https://github.com/apache/shardingsphere/pull/34463)
17+
1618
### Change Logs
1719

1820
1. [MILESTONE](https://github.com/apache/shardingsphere/milestone/31)

infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/proxy-config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource"},
1212
"interfaces":["org.apache.hive.service.rpc.thrift.TCLIService$Iface"]
1313
},
14+
{
15+
"condition":{"typeReachable":"org.apache.shardingsphere.proxy.initializer.BootstrapInitializer"},
16+
"interfaces":["org.apache.seata.config.Configuration"]
17+
},
1418
{
1519
"condition":{"typeReachable":"org.apache.shardingsphere.transaction.base.seata.at.SeataATShardingSphereTransactionManager"},
1620
"interfaces":["org.apache.seata.config.Configuration"]

infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/reflect-config.json

Lines changed: 267 additions & 269 deletions
Large diffs are not rendered by default.

infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/resource-config.json

Lines changed: 103 additions & 85 deletions
Large diffs are not rendered by default.

jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,12 @@ private List<Connection> createConnections(final String databaseName, final Stri
340340
final ConnectionMode connectionMode) throws SQLException {
341341
if (1 == connectionSize) {
342342
Connection connection = createConnection(databaseName, dataSourceName, dataSource, connectionContext.getTransactionContext());
343-
methodInvocationRecorder.replay(connection);
343+
try {
344+
methodInvocationRecorder.replay(connection);
345+
} catch (final SQLException ex) {
346+
connection.close();
347+
throw ex;
348+
}
344349
return Collections.singletonList(connection);
345350
}
346351
if (ConnectionMode.CONNECTION_STRICTLY == connectionMode) {

test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/base/SeataTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
2626
import org.apache.shardingsphere.mode.manager.ContextManager;
2727
import org.apache.shardingsphere.test.natived.commons.TestShardingService;
28+
import org.awaitility.Awaitility;
2829
import org.junit.jupiter.api.AfterEach;
2930
import org.junit.jupiter.api.BeforeEach;
3031
import org.junit.jupiter.api.Test;
@@ -38,6 +39,7 @@
3839
import javax.sql.DataSource;
3940
import java.sql.Connection;
4041
import java.sql.SQLException;
42+
import java.util.concurrent.TimeUnit;
4143

4244
import static org.hamcrest.MatcherAssert.assertThat;
4345
import static org.hamcrest.Matchers.is;
@@ -67,8 +69,15 @@ void beforeEach() {
6769
assertThat(System.getProperty(serviceDefaultGroupListKey), is(nullValue()));
6870
}
6971

72+
/**
73+
* TODO Apparently there is a real connection leak on Seata Client 2.2.0.
74+
* Waiting for <a href="https://github.com/apache/incubator-seata/pull/7044">apache/incubator-seata#7044</a>.
75+
*
76+
* @throws SQLException SQL exception
77+
*/
7078
@AfterEach
7179
void afterEach() throws SQLException {
80+
Awaitility.await().pollDelay(5L, TimeUnit.SECONDS).until(() -> true);
7281
try (Connection connection = logicDataSource.getConnection()) {
7382
ContextManager contextManager = connection.unwrap(ShardingSphereConnection.class).getContextManager();
7483
for (StorageUnit each : contextManager.getStorageUnits(DefaultDatabase.LOGIC_NAME).values()) {

test/native/src/test/java/org/apache/shardingsphere/test/natived/proxy/transactions/base/SeataTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.awaitility.Awaitility;
2929
import org.junit.jupiter.api.AfterEach;
3030
import org.junit.jupiter.api.BeforeEach;
31-
import org.junit.jupiter.api.Disabled;
3231
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.api.condition.EnabledInNativeImage;
3333
import org.testcontainers.containers.GenericContainer;
3434
import org.testcontainers.containers.PostgreSQLContainer;
3535
import org.testcontainers.containers.wait.strategy.Wait;
@@ -45,18 +45,14 @@
4545
import java.sql.Statement;
4646
import java.time.Duration;
4747
import java.util.Properties;
48+
import java.util.concurrent.TimeUnit;
4849

4950
import static org.hamcrest.MatcherAssert.assertThat;
5051
import static org.hamcrest.Matchers.is;
5152
import static org.hamcrest.Matchers.nullValue;
5253

53-
/**
54-
* TODO Executing this unit test in the GraalVM Native Image of the Github Actions device results in a connection leak
55-
* in {@code org.apache.shardingsphere.test.natived.jdbc.databases.FirebirdTest}.
56-
* This requires further investigation.
57-
*/
5854
@SuppressWarnings({"SqlNoDataSourceInspection", "resource"})
59-
@Disabled
55+
@EnabledInNativeImage
6056
@Testcontainers
6157
class SeataTest {
6258

@@ -94,8 +90,13 @@ void beforeEach() {
9490
});
9591
}
9692

93+
/**
94+
* TODO Apparently there is a real connection leak on Seata Client 2.2.0.
95+
* Waiting for <a href="https://github.com/apache/incubator-seata/pull/7044">apache/incubator-seata#7044</a>.
96+
*/
9797
@AfterEach
9898
void afterEach() {
99+
Awaitility.await().pollDelay(5L, TimeUnit.SECONDS).until(() -> true);
99100
proxyTestingServer.close();
100101
TmNettyRemotingClient.getInstance().destroy();
101102
RmNettyRemotingClient.getInstance().destroy();

0 commit comments

Comments
 (0)