Skip to content

Commit b9f884f

Browse files
committed
Allow enabling autocommit without an active transaction
Resolves: #3473
1 parent 04494b5 commit b9f884f

File tree

9 files changed

+35
-14
lines changed

9 files changed

+35
-14
lines changed

fdb-relational-api/src/main/java/com/apple/foundationdb/relational/api/RelationalConnection.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ default CallableStatement prepareCall(String sql, int resultSetType, int resultS
157157
throw new SQLFeatureNotSupportedException("Not implemented in the relational layer", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
158158
}
159159

160+
@Override
161+
default void commit() throws SQLException {
162+
commit(false);
163+
}
164+
165+
void commit(boolean onlyIfActive) throws SQLException;
166+
160167
@Override
161168
@ExcludeFromJacocoGeneratedReport
162169
default String nativeSQL(String sql) throws SQLException {

fdb-relational-core/src/main/java/com/apple/foundationdb/relational/recordlayer/EmbeddedRelationalConnection.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,17 @@ boolean canCommit() throws SQLException {
191191
}
192192

193193
@Override
194-
public void commit() throws SQLException {
194+
public void commit(boolean onlyIfActive) throws SQLException {
195195
checkOpen();
196196
if (getAutoCommit()) {
197197
throw new RelationalException("commit called when the Connection is in auto-commit mode!", ErrorCode.CANNOT_COMMIT_ROLLBACK_WITH_AUTOCOMMIT).toSqlException();
198198
}
199199
if (!inActiveTransaction()) {
200-
throw new RelationalException("No transaction to commit", ErrorCode.TRANSACTION_INACTIVE).toSqlException();
200+
if (onlyIfActive) {
201+
return;
202+
} else {
203+
throw new RelationalException("No transaction to commit", ErrorCode.TRANSACTION_INACTIVE).toSqlException();
204+
}
201205
}
202206
commitInternal();
203207
}

fdb-relational-core/src/test/java/com/apple/foundationdb/relational/memory/InMemoryRelationalConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public boolean getAutoCommit() throws SQLException {
7878
}
7979

8080
@Override
81-
public void commit() throws SQLException {
81+
public void commit(final boolean onlyIfActive) throws SQLException {
8282

8383
}
8484

fdb-relational-core/src/test/java/com/apple/foundationdb/relational/recordlayer/AutoCommitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ public void changeAutoCommitBetweenTransactions() throws SQLException {
520520
// since the resultSet is closed but autoCommit is off, transaction will remain open.
521521
Assertions.assertTrue(conn.inActiveTransaction());
522522
}
523-
Assertions.assertDoesNotThrow(conn::commit);
523+
Assertions.assertDoesNotThrow(() -> conn.commit());
524524
}
525525

526526
@Test
@@ -670,7 +670,7 @@ private static void checkAutoCommitAndCommitIfRequired(@Nonnull EmbeddedRelation
670670
} else {
671671
Assertions.assertFalse(connection.getAutoCommit());
672672
if (transactionType == TransactionType.AUTO_COMMIT_OFF_WITH_EXPLICIT_COMMIT) {
673-
Assertions.assertDoesNotThrow(connection::commit);
673+
Assertions.assertDoesNotThrow(() -> connection.commit());
674674
}
675675
}
676676
}

fdb-relational-core/src/test/java/com/apple/foundationdb/relational/recordlayer/RelationalConnectionRule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ public void commit() throws SQLException {
101101
connection.commit();
102102
}
103103

104+
@Override
105+
public void commit(final boolean onlyIfActive) throws SQLException {
106+
connection.commit(onlyIfActive);
107+
}
108+
104109
@Override
105110
public void rollback() throws SQLException {
106111
connection.rollback();

fdb-relational-grpc/src/main/proto/grpc/relational/jdbc/v1/jdbc.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ message StatementResponse {
151151
}
152152

153153
message CommitRequest {
154+
// If true, this will only commit if there is an active transaction
155+
bool only_if_active = 1;
154156
}
155157

156158
message CommitResponse {

fdb-relational-jdbc/src/main/java/com/apple/foundationdb/relational/jdbc/JDBCRelationalConnection.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,15 @@ public InsertResponse insert(@Nonnull String tableName, @Nonnull List<Relational
257257
}
258258

259259
@Override
260-
@ExcludeFromJacocoGeneratedReport
261-
public void commit() throws SQLException {
260+
public void commit(final boolean onlyIfActive) throws SQLException {
262261
try {
263262
if (getAutoCommit()) {
264263
throw new SQLException("Commit cannot be called when auto commit is ON");
265264
} else {
266265
TransactionalRequest.Builder transactionRequest = TransactionalRequest.newBuilder()
267-
.setCommitRequest(CommitRequest.newBuilder().build());
268-
// wait here for response
266+
.setCommitRequest(CommitRequest.newBuilder()
267+
.setOnlyIfActive(onlyIfActive).build());
268+
// wait here for responsea
269269
final TransactionalResponse response = serverConnection.sendRequest(transactionRequest.build());
270270
checkForResponseError(response);
271271
if (!response.hasCommitResponse()) {
@@ -345,7 +345,7 @@ public void setAutoCommit(boolean autoCommit) throws SQLException {
345345
this.autoCommit = false;
346346
} else {
347347
// commit any remaining work
348-
commit();
348+
commit(true);
349349
this.autoCommit = true;
350350
serverConnection.close();
351351
serverConnection = null;

fdb-relational-server/src/main/java/com/apple/foundationdb/relational/server/FRL.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,11 @@ public int transactionalInsert(TransactionalToken token, String tableName, List<
361361
}
362362
}
363363

364-
public void transactionalCommit(TransactionalToken token) throws SQLException {
365-
assertValidToken(token);
366-
token.getConnection().commit();
364+
public void transactionalCommit(TransactionalToken token, final boolean onlyIfActive) throws SQLException {
365+
if (!(onlyIfActive && token == null)) {
366+
assertValidToken(token);
367+
token.getConnection().commit(onlyIfActive);
368+
}
367369
}
368370

369371
public void transactionalRollback(TransactionalToken token) throws SQLException {

fdb-relational-server/src/main/java/com/apple/foundationdb/relational/server/jdbc/v1/TransactionRequestHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ public void onNext(final TransactionalRequest transactionRequest) {
103103
} else if (transactionRequest.hasCommitRequest()) {
104104
// handle commit
105105
logger.info("Handling commit request");
106-
frl.transactionalCommit(transactionalToken);
106+
frl.transactionalCommit(transactionalToken,
107+
transactionRequest.getCommitRequest().getOnlyIfActive());
107108
responseBuilder.setCommitResponse(CommitResponse.newBuilder().build());
108109
} else if (transactionRequest.hasRollbackRequest()) {
109110
// handle rollback

0 commit comments

Comments
 (0)