use of com.google.datastore.v1.BeginTransactionRequest in project pgadapter by GoogleCloudPlatform.
the class JdbcSimpleModeMockServerTest method testTwoQueries.
@Test
public void testTwoQueries() throws SQLException {
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (java.sql.Statement statement = connection.createStatement()) {
// Statement#execute(String) returns true if the result is a result set.
assertTrue(statement.execute("SELECT 1; SELECT 2;"));
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(1L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
// getMoreResults() returns true if the next result is a ResultSet.
assertTrue(statement.getMoreResults());
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(2L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
// getMoreResults() should now return false. We should also check getUpdateCount() as that
// method should return -1 to indicate that there is also no update count available.
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
}
}
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(2, requests.size());
assertEquals(SELECT1.getSql(), requests.get(0).getSql());
assertTrue(requests.get(0).getTransaction().hasId());
assertEquals(SELECT2.getSql(), requests.get(1).getSql());
assertTrue(requests.get(1).getTransaction().hasId());
// PGAdapter will use a read-only transaction if it sees that the implicit transaction will only
// read.
List<BeginTransactionRequest> beginRequests = mockSpanner.getRequestsOfType(BeginTransactionRequest.class);
assertEquals(1, beginRequests.size());
assertTrue(beginRequests.get(0).getOptions().hasReadOnly());
}
use of com.google.datastore.v1.BeginTransactionRequest in project spring-cloud-gcp by spring-cloud.
the class ReactiveFirestoreTransactionManager method startTransaction.
private Mono<ReactiveFirestoreResourceHolder> startTransaction(TransactionDefinition definition) {
TransactionOptions.Builder txOptions = definition.isReadOnly() ? TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.newBuilder().build()) : TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.newBuilder().build());
BeginTransactionRequest beginTransactionRequest = BeginTransactionRequest.newBuilder().setOptions(txOptions).setDatabase(this.databasePath).build();
return ObservableReactiveUtil.<BeginTransactionResponse>unaryCall(obs -> this.firestore.beginTransaction(beginTransactionRequest, obs)).map(beginTransactionResponse -> new ReactiveFirestoreResourceHolder(beginTransactionResponse.getTransaction()));
}
use of com.google.datastore.v1.BeginTransactionRequest in project pgadapter by GoogleCloudPlatform.
the class DdlTransactionModeAutocommitImplicitTest method testSelectAndDdlInBatch.
@Test
public void testSelectAndDdlInBatch() throws SQLException {
String sql = "SELECT 1; SELECT 2; CREATE TABLE foo (id bigint primary key);";
addDdlResponseToSpannerAdmin();
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute(sql));
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(1L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
assertTrue(statement.getMoreResults());
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(2L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
assertFalse(statement.getMoreResults());
assertEquals(0, statement.getUpdateCount());
// getMoreResults() should now return false. We should also check getUpdateCount() as that
// method should return -1 to indicate that there is also no update count available.
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
}
}
List<UpdateDatabaseDdlRequest> updateDatabaseDdlRequests = mockDatabaseAdmin.getRequests().stream().filter(request -> request instanceof UpdateDatabaseDdlRequest).map(UpdateDatabaseDdlRequest.class::cast).collect(Collectors.toList());
assertEquals(1, updateDatabaseDdlRequests.size());
assertEquals(1, updateDatabaseDdlRequests.get(0).getStatementsCount());
assertEquals("CREATE TABLE foo (id bigint primary key)", updateDatabaseDdlRequests.get(0).getStatements(0));
// The first SELECT statement should start a read-only implicit transaction.
assertEquals(1, mockSpanner.getRequestsOfType(BeginTransactionRequest.class).size());
BeginTransactionRequest beginRequest = mockSpanner.getRequestsOfType(BeginTransactionRequest.class).get(0);
assertTrue(beginRequest.getOptions().hasReadOnly());
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(2, requests.size());
assertEquals(SELECT1.getSql(), requests.get(0).getSql());
assertTrue(requests.get(0).getTransaction().hasId());
assertEquals(SELECT2.getSql(), requests.get(1).getSql());
assertTrue(requests.get(1).getTransaction().hasId());
}
use of com.google.datastore.v1.BeginTransactionRequest in project pgadapter by GoogleCloudPlatform.
the class DdlTransactionModeAutocommitImplicitTest method testDdlAndSelectInBatch.
@Test
public void testDdlAndSelectInBatch() throws SQLException {
String sql = "CREATE TABLE foo (id bigint primary key); SELECT 1; SELECT 2;";
addDdlResponseToSpannerAdmin();
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (Statement statement = connection.createStatement()) {
// Statement#execute(String) returns false if the result was either an update count or there
// was no result. Statement#getUpdateCount() returns 0 if there was no result.
assertFalse(statement.execute(sql));
assertEquals(0, statement.getUpdateCount());
assertTrue(statement.getMoreResults());
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(1L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
assertTrue(statement.getMoreResults());
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(2L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
// getMoreResults() should now return false. We should also check getUpdateCount() as that
// method should return -1 to indicate that there is also no update count available.
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
}
}
List<UpdateDatabaseDdlRequest> updateDatabaseDdlRequests = mockDatabaseAdmin.getRequests().stream().filter(request -> request instanceof UpdateDatabaseDdlRequest).map(UpdateDatabaseDdlRequest.class::cast).collect(Collectors.toList());
assertEquals(1, updateDatabaseDdlRequests.size());
assertEquals(1, updateDatabaseDdlRequests.get(0).getStatementsCount());
assertEquals("CREATE TABLE foo (id bigint primary key)", updateDatabaseDdlRequests.get(0).getStatements(0));
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(2, requests.size());
assertEquals(SELECT1.getSql(), requests.get(0).getSql());
// The first statement after the DDL statement should start an implicit transaction.
// That transaction will be a read-only transaction, and the begin of those are not inlined
// with the first statement.
assertTrue(requests.get(0).getTransaction().hasId());
assertEquals(SELECT2.getSql(), requests.get(1).getSql());
assertTrue(requests.get(1).getTransaction().hasId());
assertEquals(1, mockSpanner.getRequestsOfType(BeginTransactionRequest.class).size());
BeginTransactionRequest beginRequest = mockSpanner.getRequestsOfType(BeginTransactionRequest.class).get(0);
assertTrue(beginRequest.getOptions().hasReadOnly());
}
use of com.google.datastore.v1.BeginTransactionRequest in project spring-cloud-gcp by GoogleCloudPlatform.
the class ReactiveFirestoreTransactionManager method startTransaction.
private Mono<ReactiveFirestoreResourceHolder> startTransaction(TransactionDefinition definition) {
TransactionOptions.Builder txOptions = definition.isReadOnly() ? TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.newBuilder().build()) : TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.newBuilder().build());
BeginTransactionRequest beginTransactionRequest = BeginTransactionRequest.newBuilder().setOptions(txOptions).setDatabase(this.databasePath).build();
return ObservableReactiveUtil.<BeginTransactionResponse>unaryCall(obs -> this.firestore.beginTransaction(beginTransactionRequest, obs)).map(beginTransactionResponse -> new ReactiveFirestoreResourceHolder(beginTransactionResponse.getTransaction()));
}
Aggregations