fix(agent): execute transaction control SQL as statements
This commit is contained in:
1 parent
18710ce2ab
commit
0dc83f69b5
2 files changed
+42
-32
No files matched your search
@@ -67,24 +67,8 @@ public final class JdbcExecutor {
|
|||||||
) {
|
) {
|
||||||
return unchecked(() -> {
|
return unchecked(() -> {
|
||||||
String trimmedSql = trimSql(sql);
|
String trimmedSql = trimSql(sql);
|
||||||
String upperSql = trimmedSql.toUpperCase(Locale.ROOT).trim();
|
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
if ("BEGIN".equals(upperSql) || "BEGIN TRANSACTION".equals(upperSql)) {
|
|
||||||
conn.setAutoCommit(false);
|
|
||||||
return emptyQueryResult(start);
|
|
||||||
}
|
|
||||||
if ("COMMIT".equals(upperSql)) {
|
|
||||||
conn.commit();
|
|
||||||
conn.setAutoCommit(true);
|
|
||||||
return emptyQueryResult(start);
|
|
||||||
}
|
|
||||||
if ("ROLLBACK".equals(upperSql)) {
|
|
||||||
conn.rollback();
|
|
||||||
conn.setAutoCommit(true);
|
|
||||||
return emptyQueryResult(start);
|
|
||||||
}
|
|
||||||
|
|
||||||
applySchema(conn, schema, setSchemaSql);
|
applySchema(conn, schema, setSchemaSql);
|
||||||
|
|
||||||
try (Statement stmt = conn.createStatement()) {
|
try (Statement stmt = conn.createStatement()) {
|
||||||
@@ -94,6 +78,8 @@ public final class JdbcExecutor {
|
|||||||
if (fetchSize != null && fetchSize > 0) {
|
if (fetchSize != null && fetchSize > 0) {
|
||||||
stmt.setFetchSize(fetchSize);
|
stmt.setFetchSize(fetchSize);
|
||||||
}
|
}
|
||||||
|
// SQL dumps often contain BEGIN/COMMIT/ROLLBACK as executable statements.
|
||||||
|
// Do not translate them to Connection.commit(), which requires autoCommit=false.
|
||||||
boolean hasResultSet = stmt.execute(trimmedSql);
|
boolean hasResultSet = stmt.execute(trimmedSql);
|
||||||
long elapsed = System.currentTimeMillis() - start;
|
long elapsed = System.currentTimeMillis() - start;
|
||||||
if (hasResultSet) {
|
if (hasResultSet) {
|
||||||
@@ -204,24 +190,8 @@ public final class JdbcExecutor {
|
|||||||
return unchecked(() -> {
|
return unchecked(() -> {
|
||||||
expireIdleSessions(targetSessions, System.currentTimeMillis(), QUERY_SESSION_IDLE_TIMEOUT_MILLIS);
|
expireIdleSessions(targetSessions, System.currentTimeMillis(), QUERY_SESSION_IDLE_TIMEOUT_MILLIS);
|
||||||
String trimmedSql = trimSql(sql);
|
String trimmedSql = trimSql(sql);
|
||||||
String upperSql = trimmedSql.toUpperCase(Locale.ROOT).trim();
|
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
if ("BEGIN".equals(upperSql) || "BEGIN TRANSACTION".equals(upperSql)) {
|
|
||||||
conn.setAutoCommit(false);
|
|
||||||
return emptyQueryPageResult(start);
|
|
||||||
}
|
|
||||||
if ("COMMIT".equals(upperSql)) {
|
|
||||||
conn.commit();
|
|
||||||
conn.setAutoCommit(true);
|
|
||||||
return emptyQueryPageResult(start);
|
|
||||||
}
|
|
||||||
if ("ROLLBACK".equals(upperSql)) {
|
|
||||||
conn.rollback();
|
|
||||||
conn.setAutoCommit(true);
|
|
||||||
return emptyQueryPageResult(start);
|
|
||||||
}
|
|
||||||
|
|
||||||
applySchema(conn, schema, setSchemaSql);
|
applySchema(conn, schema, setSchemaSql);
|
||||||
|
|
||||||
Statement stmt = conn.createStatement();
|
Statement stmt = conn.createStatement();
|
||||||
@@ -230,6 +200,8 @@ public final class JdbcExecutor {
|
|||||||
if (options.getFetchSize() != null && options.getFetchSize() > 0) {
|
if (options.getFetchSize() != null && options.getFetchSize() > 0) {
|
||||||
stmt.setFetchSize(options.getFetchSize());
|
stmt.setFetchSize(options.getFetchSize());
|
||||||
}
|
}
|
||||||
|
// Keep script transaction-control statements in the SQL stream.
|
||||||
|
// JDBC transaction APIs are reserved for executeTransaction.
|
||||||
boolean hasResultSet = stmt.execute(trimmedSql);
|
boolean hasResultSet = stmt.execute(trimmedSql);
|
||||||
long elapsed = System.currentTimeMillis() - start;
|
long elapsed = System.currentTimeMillis() - start;
|
||||||
if (!hasResultSet) {
|
if (!hasResultSet) {
|
||||||
|
|||||||
@@ -103,6 +103,44 @@ class AbstractJdbcAgentTest {
|
|||||||
assertEquals(Arrays.asList("setMaxRows:10001", "execute:BEGIN\n NULL;\nEND;"), tracking.calls);
|
assertEquals(Arrays.asList("setMaxRows:10001", "execute:BEGIN\n NULL;\nEND;"), tracking.calls);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void executesTransactionControlSqlThroughStatements() {
|
||||||
|
TrackingConnection tracking = new TrackingConnection();
|
||||||
|
TestAgent agent = new TestAgent(tracking);
|
||||||
|
agent.connect(new ConnectParams());
|
||||||
|
|
||||||
|
// Dump files can contain explicit transaction SQL while the JDBC connection is in auto-commit mode.
|
||||||
|
agent.executeQuery("BEGIN;", null, new ExecuteQueryOptions());
|
||||||
|
agent.executeQuery("BEGIN TRANSACTION;", null, new ExecuteQueryOptions());
|
||||||
|
agent.executeQuery("COMMIT;", null, new ExecuteQueryOptions());
|
||||||
|
agent.executeQuery("ROLLBACK;", null, new ExecuteQueryOptions());
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
Arrays.asList(
|
||||||
|
"setMaxRows:10001",
|
||||||
|
"execute:BEGIN",
|
||||||
|
"setMaxRows:10001",
|
||||||
|
"execute:BEGIN TRANSACTION",
|
||||||
|
"setMaxRows:10001",
|
||||||
|
"execute:COMMIT",
|
||||||
|
"setMaxRows:10001",
|
||||||
|
"execute:ROLLBACK"
|
||||||
|
),
|
||||||
|
tracking.calls
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void executesPagedTransactionControlSqlThroughStatements() {
|
||||||
|
TrackingConnection tracking = new TrackingConnection();
|
||||||
|
TestAgent agent = new TestAgent(tracking);
|
||||||
|
agent.connect(new ConnectParams());
|
||||||
|
|
||||||
|
agent.executeQueryPage("COMMIT;", null, new QueryPageOptions());
|
||||||
|
|
||||||
|
assertEquals(Collections.singletonList("execute:COMMIT"), tracking.calls);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void delegatesTransactionsThroughSharedFoundation() {
|
void delegatesTransactionsThroughSharedFoundation() {
|
||||||
TrackingConnection tracking = new TrackingConnection();
|
TrackingConnection tracking = new TrackingConnection();
|
||||||
|
|||||||
Reference in new issue
Block a user