Skip to content

Commit

Permalink
Alleviate connection leaks caused by Seata Client throwing exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Jan 24, 2025
1 parent a7c137c commit 3695c21
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 362 deletions.
2 changes: 2 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

### Bug Fixes

1. Alleviate connection leaks caused by Seata Client throwing exceptions - [#34463](https://github.com/apache/shardingsphere/pull/34463)

### Change Logs

1. [MILESTONE](https://github.com/apache/shardingsphere/milestone/31)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource"},
"interfaces":["org.apache.hive.service.rpc.thrift.TCLIService$Iface"]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.proxy.initializer.BootstrapInitializer"},
"interfaces":["org.apache.seata.config.Configuration"]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.transaction.base.seata.at.SeataATShardingSphereTransactionManager"},
"interfaces":["org.apache.seata.config.Configuration"]
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,12 @@ private List<Connection> createConnections(final String databaseName, final Stri
final ConnectionMode connectionMode) throws SQLException {
if (1 == connectionSize) {
Connection connection = createConnection(databaseName, dataSourceName, dataSource, connectionContext.getTransactionContext());
methodInvocationRecorder.replay(connection);
try {
methodInvocationRecorder.replay(connection);
} catch (final SQLException ex) {
connection.close();
throw ex;
}
return Collections.singletonList(connection);
}
if (ConnectionMode.CONNECTION_STRICTLY == connectionMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.test.natived.commons.TestShardingService;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -38,6 +39,7 @@
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -67,8 +69,15 @@ void beforeEach() {
assertThat(System.getProperty(serviceDefaultGroupListKey), is(nullValue()));
}

/**
* TODO Apparently there is a real connection leak on Seata Client 2.2.0.
* Waiting for <a href="https://github.com/apache/incubator-seata/pull/7044">apache/incubator-seata#7044</a>.
*
* @throws SQLException SQL exception
*/
@AfterEach
void afterEach() throws SQLException {
Awaitility.await().pollDelay(5L, TimeUnit.SECONDS).until(() -> true);
try (Connection connection = logicDataSource.getConnection()) {
ContextManager contextManager = connection.unwrap(ShardingSphereConnection.class).getContextManager();
for (StorageUnit each : contextManager.getStorageUnits(DefaultDatabase.LOGIC_NAME).values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledInNativeImage;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.wait.strategy.Wait;
Expand All @@ -45,18 +45,14 @@
import java.sql.Statement;
import java.time.Duration;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

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

Expand Down Expand Up @@ -94,8 +90,13 @@ void beforeEach() {
});
}

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

0 comments on commit 3695c21

Please sign in to comment.