Search in sources :

Example 11 with BeginTransactionRequest

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());
}
Also used : ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Connection(java.sql.Connection) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 12 with BeginTransactionRequest

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()));
}
Also used : CommitResponse(com.google.firestore.v1.CommitResponse) FirestoreGrpc(com.google.firestore.v1.FirestoreGrpc) Util(org.springframework.cloud.gcp.data.firestore.util.Util) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Mono(reactor.core.publisher.Mono) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest) ObservableReactiveUtil(org.springframework.cloud.gcp.data.firestore.util.ObservableReactiveUtil) AbstractReactiveTransactionManager(org.springframework.transaction.reactive.AbstractReactiveTransactionManager) ByteString(com.google.protobuf.ByteString) TransactionSynchronizationManager(org.springframework.transaction.reactive.TransactionSynchronizationManager) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) Empty(com.google.protobuf.Empty) SmartTransactionObject(org.springframework.transaction.support.SmartTransactionObject) TransactionSystemException(org.springframework.transaction.TransactionSystemException) GenericReactiveTransaction(org.springframework.transaction.reactive.GenericReactiveTransaction) CommitRequest(com.google.firestore.v1.CommitRequest) TransactionOptions(com.google.firestore.v1.TransactionOptions) RollbackRequest(com.google.firestore.v1.RollbackRequest) TransactionException(org.springframework.transaction.TransactionException) Nullable(org.springframework.lang.Nullable) Assert(org.springframework.util.Assert) TransactionOptions(com.google.firestore.v1.TransactionOptions) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest)

Example 13 with BeginTransactionRequest

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());
}
Also used : ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Statement(java.sql.Statement) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.Test)

Example 14 with BeginTransactionRequest

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());
}
Also used : ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Statement(java.sql.Statement) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.Test)

Example 15 with BeginTransactionRequest

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()));
}
Also used : CommitResponse(com.google.firestore.v1.CommitResponse) FirestoreGrpc(com.google.firestore.v1.FirestoreGrpc) Util(com.google.cloud.spring.data.firestore.util.Util) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Mono(reactor.core.publisher.Mono) Timestamp(com.google.cloud.Timestamp) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest) AbstractReactiveTransactionManager(org.springframework.transaction.reactive.AbstractReactiveTransactionManager) ByteString(com.google.protobuf.ByteString) TransactionSynchronizationManager(org.springframework.transaction.reactive.TransactionSynchronizationManager) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) Empty(com.google.protobuf.Empty) SmartTransactionObject(org.springframework.transaction.support.SmartTransactionObject) TransactionSystemException(org.springframework.transaction.TransactionSystemException) GenericReactiveTransaction(org.springframework.transaction.reactive.GenericReactiveTransaction) CommitRequest(com.google.firestore.v1.CommitRequest) TransactionOptions(com.google.firestore.v1.TransactionOptions) RollbackRequest(com.google.firestore.v1.RollbackRequest) TransactionException(org.springframework.transaction.TransactionException) FirestoreStub(com.google.firestore.v1.FirestoreGrpc.FirestoreStub) Nullable(org.springframework.lang.Nullable) FirestoreClassMapper(com.google.cloud.spring.data.firestore.mapping.FirestoreClassMapper) ObservableReactiveUtil(com.google.cloud.spring.data.firestore.util.ObservableReactiveUtil) Assert(org.springframework.util.Assert) TransactionOptions(com.google.firestore.v1.TransactionOptions) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest)

Aggregations

BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)11 Test (org.junit.Test)10 Transaction (com.google.spanner.v1.Transaction)7 BeginTransactionRequest (com.google.datastore.v1.BeginTransactionRequest)5 ByteString (com.google.protobuf.ByteString)5 ExecutionException (java.util.concurrent.ExecutionException)5 DatastoreException (com.google.datastore.v1.client.DatastoreException)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 Connection (java.sql.Connection)4 BeginTransactionRequest (com.google.firestore.v1.BeginTransactionRequest)3 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)3 UpdateDatabaseDdlRequest (com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest)3 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)3 Session (com.google.spanner.v1.Session)3 TransactionOptions (com.google.spanner.v1.TransactionOptions)3 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)2 CommitRequest (com.google.firestore.v1.CommitRequest)2 CommitResponse (com.google.firestore.v1.CommitResponse)2