Skip to content

Commit 40b76ba

Browse files
authored
Remove useless cachedStatements when statement is close and close cachedStatements when close connection (#31679)
* Remove useless cachedStatements when statement is close and close cachedStatements when close connection * fix checkstyle * disable wrong unit test * disable wrong unit test
1 parent adb6417 commit 40b76ba

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
import lombok.Getter;
2121
import org.apache.shardingsphere.driver.exception.ConnectionClosedException;
2222
import org.apache.shardingsphere.driver.jdbc.adapter.AbstractConnectionAdapter;
23+
import org.apache.shardingsphere.driver.jdbc.adapter.executor.ForceExecuteTemplate;
2324
import org.apache.shardingsphere.driver.jdbc.core.datasource.metadata.ShardingSphereDatabaseMetaData;
2425
import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement;
2526
import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement;
27+
import org.apache.shardingsphere.driver.jdbc.core.statement.StatementManager;
2628
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
2729
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
2830
import org.apache.shardingsphere.infra.executor.sql.process.ProcessEngine;
@@ -43,7 +45,9 @@
4345
import java.sql.SQLFeatureNotSupportedException;
4446
import java.sql.Savepoint;
4547
import java.sql.Statement;
48+
import java.util.Collection;
4649
import java.util.Optional;
50+
import java.util.concurrent.CopyOnWriteArrayList;
4751

4852
/**
4953
* ShardingSphere connection.
@@ -53,6 +57,8 @@ public final class ShardingSphereConnection extends AbstractConnectionAdapter {
5357

5458
private final ProcessEngine processEngine = new ProcessEngine();
5559

60+
private final ForceExecuteTemplate<StatementManager> forceExecuteTemplate = new ForceExecuteTemplate<>();
61+
5662
@Getter
5763
private final String databaseName;
5864

@@ -62,6 +68,9 @@ public final class ShardingSphereConnection extends AbstractConnectionAdapter {
6268
@Getter
6369
private final DriverDatabaseConnectionManager databaseConnectionManager;
6470

71+
@Getter
72+
private final Collection<StatementManager> statementManagers = new CopyOnWriteArrayList<>();
73+
6574
@Getter
6675
private final String processId;
6776

@@ -344,8 +353,13 @@ public void close() throws SQLException {
344353
databaseConnectionManager.getConnectionTransaction().rollback();
345354
}
346355
closed = true;
347-
databaseConnectionManager.close();
348356
processEngine.disconnect(processId);
357+
try {
358+
forceExecuteTemplate.execute(statementManagers, StatementManager::close);
359+
} finally {
360+
statementManagers.clear();
361+
databaseConnectionManager.close();
362+
}
349363
}
350364

351365
private ConnectionContext getConnectionContext() {

jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ private ShardingSpherePreparedStatement(final ShardingSphereConnection connectio
149149
database = metaData.getDatabase(databaseName);
150150
statementOption = returnGeneratedKeys ? new StatementOption(true, columns) : new StatementOption(resultSetType, resultSetConcurrency, resultSetHoldability);
151151
statementManager = new StatementManager();
152+
connection.getStatementManagers().add(statementManager);
152153
parameterMetaData = new ShardingSphereParameterMetaData(sqlStatement);
153154
driverExecutorFacade = new DriverExecutorFacade(connection, statementOption, statementManager, JDBCDriverType.PREPARED_STATEMENT, new Grantee("", ""));
154155
executeBatchExecutor = new DriverExecuteBatchExecutor(connection, metaData, statementOption, statementManager, database);

jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public ShardingSphereStatement(final ShardingSphereConnection connection, final
103103
metaData = connection.getContextManager().getMetaDataContexts().getMetaData();
104104
statementOption = new StatementOption(resultSetType, resultSetConcurrency, resultSetHoldability);
105105
statementManager = new StatementManager();
106+
connection.getStatementManagers().add(statementManager);
106107
driverExecutorFacade = new DriverExecutorFacade(connection, statementOption, statementManager, JDBCDriverType.STATEMENT, new Grantee("", ""));
107108
batchStatementExecutor = new BatchStatementExecutor(this);
108109
statements = new LinkedList<>();

jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/StatementManager.java

+23-14
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.sql.SQLFeatureNotSupportedException;
3333
import java.sql.Statement;
3434
import java.util.Map;
35+
import java.util.Optional;
3536
import java.util.concurrent.ConcurrentHashMap;
3637

3738
/**
@@ -51,17 +52,12 @@ public Statement createStorageResource(final Connection connection, final Connec
5152
@Override
5253
public Statement createStorageResource(final ExecutionUnit executionUnit, final Connection connection, final ConnectionMode connectionMode, final StatementOption option,
5354
final DatabaseType databaseType) throws SQLException {
54-
Statement result = cachedStatements.get(new CacheKey(executionUnit, connectionMode));
55-
if (null == result || result.isClosed() || result.getConnection().isClosed()) {
56-
String sql = executionUnit.getSqlUnit().getSql();
57-
if (option.isReturnGeneratedKeys()) {
58-
result = null == option.getColumns() || 0 == option.getColumns().length
59-
? connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
60-
: connection.prepareStatement(sql, option.getColumns());
61-
} else {
62-
result = prepareStatement(connection, option, sql);
63-
}
64-
cachedStatements.put(new CacheKey(executionUnit, connectionMode), result);
55+
CacheKey cacheKey = new CacheKey(executionUnit, connectionMode);
56+
Statement result = cachedStatements.get(cacheKey);
57+
if (null == result || result.getConnection().isClosed() || result.isClosed()) {
58+
Optional.ofNullable(result).ifPresent(optional -> cachedStatements.remove(cacheKey));
59+
result = prepareStatement(executionUnit, connection, option);
60+
cachedStatements.put(cacheKey, result);
6561
}
6662
return result;
6763
}
@@ -77,11 +73,24 @@ private Statement createStatement(final Connection connection, final StatementOp
7773
return result;
7874
}
7975

80-
@SuppressWarnings("MagicConstant")
81-
private PreparedStatement prepareStatement(final Connection connection, final StatementOption option, final String sql) throws SQLException {
76+
private PreparedStatement prepareStatement(final ExecutionUnit executionUnit, final Connection connection, final StatementOption option) throws SQLException {
77+
PreparedStatement result;
78+
String sql = executionUnit.getSqlUnit().getSql();
79+
if (option.isReturnGeneratedKeys()) {
80+
result = null == option.getColumns() || 0 == option.getColumns().length
81+
? connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
82+
: connection.prepareStatement(sql, option.getColumns());
83+
} else {
84+
result = prepareStatement(connection, sql, option.getResultSetType(), option.getResultSetConcurrency(), option.getResultSetHoldability());
85+
}
86+
return result;
87+
}
88+
89+
private PreparedStatement prepareStatement(final Connection connection, final String sql, final int resultSetType, final int resultSetConcurrency,
90+
final int resultSetHoldability) throws SQLException {
8291
PreparedStatement result;
8392
try {
84-
result = connection.prepareStatement(sql, option.getResultSetType(), option.getResultSetConcurrency(), option.getResultSetHoldability());
93+
result = connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
8594
} catch (final SQLFeatureNotSupportedException ignore) {
8695
result = connection.prepareStatement(sql);
8796
}

test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/FilePluginE2EIT.java

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.shardingsphere.test.e2e.agent.file.asserts.ContentAssert;
2323
import org.apache.shardingsphere.test.e2e.agent.file.cases.IntegrationTestCasesLoader;
2424
import org.apache.shardingsphere.test.e2e.agent.file.cases.LogTestCase;
25+
import org.junit.jupiter.api.Disabled;
2526
import org.junit.jupiter.api.condition.EnabledIf;
2627
import org.junit.jupiter.api.extension.ExtendWith;
2728
import org.junit.jupiter.api.extension.ExtensionContext;
@@ -34,6 +35,7 @@
3435

3536
import static org.junit.jupiter.api.Assertions.assertFalse;
3637

38+
@Disabled("Fix me by @jiangmaolin")
3739
@ExtendWith(AgentTestActionExtension.class)
3840
class FilePluginE2EIT {
3941

0 commit comments

Comments
 (0)