Skip to content

Commit 8aa0edb

Browse files
authored
feat: auto_batch_dml connection property (#1787)
* 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
1 parent db48aa5 commit 8aa0edb

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ these can also be supplied in a Properties instance that is passed to the
118118
- maxSessions (int): Sets the maximum number of sessions in the backing session pool. Defaults to 400.
119119
- numChannels (int): Sets the number of gRPC channels to use. Defaults to 4.
120120
- 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.
121+
- auto_batch_dml (boolean): Automatically buffer DML statements and execute them as one batch,
122+
instead of executing them on Spanner directly. The buffered DML statements are executed on Spanner
123+
in one batch when a query is executed, or when the transaction is committed. This option can for
124+
example be used in combination with Hibernate to automatically group more (small) DML statements
125+
into one batch.
121126
- 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.
122127
- 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.
123128

src/test/java/com/google/cloud/spanner/jdbc/ExecuteMockServerTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import com.google.protobuf.Value;
3737
import com.google.rpc.Code;
3838
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata;
39+
import com.google.spanner.v1.CommitRequest;
40+
import com.google.spanner.v1.ExecuteBatchDmlRequest;
3941
import com.google.spanner.v1.ResultSetMetadata;
4042
import com.google.spanner.v1.ResultSetStats;
4143
import com.google.spanner.v1.StructType;
@@ -844,4 +846,28 @@ public void testInvalidExecuteUpdate_shouldNotLeakSession() throws SQLException
844846
}
845847
}
846848
}
849+
850+
private String getExtension() {
851+
return dialect == Dialect.POSTGRESQL ? "spanner." : "";
852+
}
853+
854+
@Test
855+
public void testExecuteAutoBatchDml() throws SQLException {
856+
try (Connection connection = createJdbcConnection();
857+
Statement statement = connection.createStatement()) {
858+
connection.setAutoCommit(false);
859+
860+
assertFalse(statement.execute(String.format("set %sauto_batch_dml = true", getExtension())));
861+
for (int i = 0; i < 3; i++) {
862+
assertFalse(statement.execute(dml));
863+
assertEquals(1, statement.getUpdateCount());
864+
}
865+
connection.commit();
866+
}
867+
assertEquals(1, mockSpanner.countRequestsOfType(ExecuteBatchDmlRequest.class));
868+
ExecuteBatchDmlRequest request =
869+
mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class).get(0);
870+
assertEquals(3, request.getStatementsCount());
871+
assertEquals(1, mockSpanner.countRequestsOfType(CommitRequest.class));
872+
}
847873
}

0 commit comments

Comments
 (0)