Skip to content

Commit

Permalink
feat: auto_batch_dml connection property (#1787)
Browse files Browse the repository at this point in the history
* feat: auto_batch_dml connection property

The `auto_batch_dml` connection property enables automatic batching of
DML statements, for example when using Hibernate. When this connection
variable has been enabled, a JDBC connection will automatically buffer
DML statements in memory, and send all buffered DML statements to
Spanner as one batch when a query is executed or when the transaction
is committed. This can significantly reduce the number of round-trips
to Spanner, especially when using an ORM like Hibernate that generates
many small single-row DML statements.

* test: add test for auto_batch_dml
  • Loading branch information
olavloite authored Oct 12, 2024
1 parent db48aa5 commit 8aa0edb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ these can also be supplied in a Properties instance that is passed to the
- maxSessions (int): Sets the maximum number of sessions in the backing session pool. Defaults to 400.
- numChannels (int): Sets the number of gRPC channels to use. Defaults to 4.
- retryAbortsInternally (boolean): The JDBC driver will by default automatically retry aborted transactions internally. This is done by keeping track of all statements and the results of these during a transaction, and if the transaction is aborted by Cloud Spanner, it will replay the statements on a new transaction and compare the results with the initial attempt. Disable this option if you want to handle aborted transactions in your own application.
- auto_batch_dml (boolean): Automatically buffer DML statements and execute them as one batch,
instead of executing them on Spanner directly. The buffered DML statements are executed on Spanner
in one batch when a query is executed, or when the transaction is committed. This option can for
example be used in combination with Hibernate to automatically group more (small) DML statements
into one batch.
- oauthToken (string): A valid pre-existing OAuth token to use for authentication for this connection. Setting this property will take precedence over any value set for a credentials file.
- lenient (boolean): Enable this to force the JDBC driver to ignore unknown properties in the connection URL. Some applications automatically add additional properties to the URL that are not recognized by the JDBC driver. Normally, the JDBC driver will reject this, unless `lenient` mode is enabled.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import com.google.protobuf.Value;
import com.google.rpc.Code;
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata;
import com.google.spanner.v1.CommitRequest;
import com.google.spanner.v1.ExecuteBatchDmlRequest;
import com.google.spanner.v1.ResultSetMetadata;
import com.google.spanner.v1.ResultSetStats;
import com.google.spanner.v1.StructType;
Expand Down Expand Up @@ -844,4 +846,28 @@ public void testInvalidExecuteUpdate_shouldNotLeakSession() throws SQLException
}
}
}

private String getExtension() {
return dialect == Dialect.POSTGRESQL ? "spanner." : "";
}

@Test
public void testExecuteAutoBatchDml() throws SQLException {
try (Connection connection = createJdbcConnection();
Statement statement = connection.createStatement()) {
connection.setAutoCommit(false);

assertFalse(statement.execute(String.format("set %sauto_batch_dml = true", getExtension())));
for (int i = 0; i < 3; i++) {
assertFalse(statement.execute(dml));
assertEquals(1, statement.getUpdateCount());
}
connection.commit();
}
assertEquals(1, mockSpanner.countRequestsOfType(ExecuteBatchDmlRequest.class));
ExecuteBatchDmlRequest request =
mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class).get(0);
assertEquals(3, request.getStatementsCount());
assertEquals(1, mockSpanner.countRequestsOfType(CommitRequest.class));
}
}

0 comments on commit 8aa0edb

Please sign in to comment.