Search in sources :

Example 26 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerTransactionManager method doBegin.

@Override
protected void doBegin(Object transactionObject, TransactionDefinition transactionDefinition) throws TransactionException {
    if (transactionDefinition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
        throw new IllegalStateException("SpannerTransactionManager supports only isolation level TransactionDefinition.ISOLATION_DEFAULT");
    }
    if (transactionDefinition.getPropagationBehavior() != TransactionDefinition.PROPAGATION_REQUIRED) {
        throw new IllegalStateException("SpannerTransactionManager supports only propagation behavior " + "TransactionDefinition.PROPAGATION_REQUIRED");
    }
    Tx tx = (Tx) transactionObject;
    if (transactionDefinition.isReadOnly()) {
        final ReadContext targetTransactionContext = this.databaseClientProvider.get().readOnlyTransaction();
        tx.isReadOnly = true;
        tx.transactionManager = null;
        tx.transactionContext = new TransactionContext() {

            @Override
            public void buffer(Mutation mutation) {
                throw new IllegalStateException("Spanner transaction cannot apply" + " mutation because it is in readonly mode");
            }

            @Override
            public void buffer(Iterable<Mutation> iterable) {
                throw new IllegalStateException("Spanner transaction cannot apply" + " mutations because it is in readonly mode");
            }

            @Override
            public long executeUpdate(Statement statement) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public ApiFuture<Long> executeUpdateAsync(Statement statement) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public long[] batchUpdate(Iterable<Statement> iterable) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public ApiFuture<long[]> batchUpdateAsync(Iterable<Statement> iterable) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public ResultSet read(String s, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
                return targetTransactionContext.read(s, keySet, iterable, readOptions);
            }

            @Override
            public AsyncResultSet readAsync(String s, KeySet keySet, Iterable<String> iterable, ReadOption... readOptions) {
                return targetTransactionContext.readAsync(s, keySet, iterable, readOptions);
            }

            @Override
            public ResultSet readUsingIndex(String s, String s1, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
                return targetTransactionContext.readUsingIndex(s, s1, keySet, iterable, readOptions);
            }

            @Override
            public AsyncResultSet readUsingIndexAsync(String s, String s1, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
                return targetTransactionContext.readUsingIndexAsync(s, s1, keySet, iterable, readOptions);
            }

            @Nullable
            @Override
            public Struct readRow(String s, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRow(s, key, iterable);
            }

            @Override
            public ApiFuture<Struct> readRowAsync(String s, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRowAsync(s, key, iterable);
            }

            @Nullable
            @Override
            public Struct readRowUsingIndex(String s, String s1, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRowUsingIndex(s, s1, key, iterable);
            }

            @Override
            public ApiFuture<Struct> readRowUsingIndexAsync(String s, String s1, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRowUsingIndexAsync(s, s1, key, iterable);
            }

            @Override
            public ResultSet executeQuery(Statement statement, Options.QueryOption... queryOptions) {
                return targetTransactionContext.executeQuery(statement, queryOptions);
            }

            @Override
            public AsyncResultSet executeQueryAsync(Statement statement, QueryOption... queryOptions) {
                return targetTransactionContext.executeQueryAsync(statement, queryOptions);
            }

            @Override
            public ResultSet analyzeQuery(Statement statement, QueryAnalyzeMode queryAnalyzeMode) {
                return targetTransactionContext.analyzeQuery(statement, queryAnalyzeMode);
            }

            @Override
            public void close() {
                targetTransactionContext.close();
            }
        };
    } else {
        tx.transactionManager = tx.databaseClient.transactionManager();
        tx.transactionContext = tx.getTransactionManager().begin();
        tx.isReadOnly = false;
    }
    TransactionSynchronizationManager.bindResource(tx.getDatabaseClient(), tx);
}
Also used : KeySet(com.google.cloud.spanner.KeySet) Options(com.google.cloud.spanner.Options) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) Statement(com.google.cloud.spanner.Statement) QueryOption(com.google.cloud.spanner.Options.QueryOption) Struct(com.google.cloud.spanner.Struct) ApiFuture(com.google.api.core.ApiFuture) QueryOption(com.google.cloud.spanner.Options.QueryOption) TransactionContext(com.google.cloud.spanner.TransactionContext) ReadContext(com.google.cloud.spanner.ReadContext) ResultSet(com.google.cloud.spanner.ResultSet) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) Mutation(com.google.cloud.spanner.Mutation) ReadOption(com.google.cloud.spanner.Options.ReadOption) ReadOption(com.google.cloud.spanner.Options.ReadOption) Nullable(javax.annotation.Nullable) Key(com.google.cloud.spanner.Key)

Aggregations

Key (com.google.cloud.spanner.Key)26 Test (org.junit.Test)16 KeySet (com.google.cloud.spanner.KeySet)8 PrimaryKey (org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey)7 ArrayList (java.util.ArrayList)4 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)4 Mutation (com.google.cloud.spanner.Mutation)3 ResultSet (com.google.cloud.spanner.ResultSet)3 SpannerOperations (org.springframework.cloud.gcp.data.spanner.core.SpannerOperations)3 SpannerPageableQueryOptions (org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions)3 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)3 Sort (org.springframework.data.domain.Sort)3 PersistentProperty (org.springframework.data.mapping.PersistentProperty)3 Statement (com.google.cloud.spanner.Statement)2 Struct (com.google.cloud.spanner.Struct)2 List (java.util.List)2 StringJoiner (java.util.StringJoiner)2 Pageable (org.springframework.data.domain.Pageable)2 ApiFuture (com.google.api.core.ApiFuture)1 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)1