Search in sources :

Example 1 with BeforeExecuteDmlEvent

use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplate method executePartitionedDmlStatement.

@Override
public long executePartitionedDmlStatement(Statement statement) {
    Assert.notNull(statement, "A non-null statement is required.");
    maybeEmitEvent(new BeforeExecuteDmlEvent(statement));
    long rowsAffected = doWithOrWithoutTransactionContext(x -> {
        throw new SpannerDataException("Cannot execute partitioned DML in a transaction.");
    }, () -> this.databaseClientProvider.get().executePartitionedUpdate(statement));
    maybeEmitEvent(new AfterExecuteDmlEvent(statement, rowsAffected));
    return rowsAffected;
}
Also used : BeforeExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) AfterExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterExecuteDmlEvent)

Example 2 with BeforeExecuteDmlEvent

use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method executeDmlTest.

@Test
public void executeDmlTest() {
    TransactionContext context = mock(TransactionContext.class);
    TransactionRunner transactionRunner = mock(TransactionRunner.class);
    when(this.databaseClient.readWriteTransaction()).thenReturn(transactionRunner);
    when(transactionRunner.run(any())).thenAnswer((invocation) -> {
        TransactionCallable transactionCallable = invocation.getArgument(0);
        return transactionCallable.run(context);
    });
    when(context.executeUpdate(eq(DML))).thenReturn(333L);
    verifyBeforeAndAfterEvents(new BeforeExecuteDmlEvent(DML), new AfterExecuteDmlEvent(DML, 333L), () -> this.spannerTemplate.executeDmlStatement(DML), x -> {
    });
    verify(context, times(1)).executeUpdate(eq(DML));
}
Also used : BeforeExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent) TransactionContext(com.google.cloud.spanner.TransactionContext) TransactionRunner(com.google.cloud.spanner.TransactionRunner) TransactionCallable(com.google.cloud.spanner.TransactionRunner.TransactionCallable) AfterExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterExecuteDmlEvent) Test(org.junit.Test)

Example 3 with BeforeExecuteDmlEvent

use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method executePartitionedDmlTest.

@Test
public void executePartitionedDmlTest() {
    when(this.databaseClient.executePartitionedUpdate(eq(DML))).thenReturn(333L);
    verifyBeforeAndAfterEvents(new BeforeExecuteDmlEvent(DML), new AfterExecuteDmlEvent(DML, 333L), () -> this.spannerTemplate.executePartitionedDmlStatement(DML), x -> x.verify(this.databaseClient, times(1)).executePartitionedUpdate(eq(DML)));
}
Also used : BeforeExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent) AfterExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterExecuteDmlEvent) Test(org.junit.Test)

Example 4 with BeforeExecuteDmlEvent

use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplate method executeDmlStatement.

@Override
public long executeDmlStatement(Statement statement) {
    Assert.notNull(statement, "A non-null statement is required.");
    maybeEmitEvent(new BeforeExecuteDmlEvent(statement));
    long rowsAffected = doWithOrWithoutTransactionContext((x) -> x.executeUpdate(statement), () -> this.databaseClientProvider.get().readWriteTransaction().run(transactionContext -> transactionContext.executeUpdate(statement)));
    maybeEmitEvent(new AfterExecuteDmlEvent(statement, rowsAffected));
    return rowsAffected;
}
Also used : TransactionCallable(com.google.cloud.spanner.TransactionRunner.TransactionCallable) Arrays(java.util.Arrays) BeforeSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeSaveEvent) BeforeDeleteEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeDeleteEvent) DatabaseClient(com.google.cloud.spanner.DatabaseClient) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ConversionUtils(org.springframework.cloud.gcp.data.spanner.core.convert.ConversionUtils) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) ResultSet(com.google.cloud.spanner.ResultSet) Key(com.google.cloud.spanner.Key) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) AfterExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterExecuteDmlEvent) TransactionContext(com.google.cloud.spanner.TransactionContext) Collection(java.util.Collection) Set(java.util.Set) Mutation(com.google.cloud.spanner.Mutation) Collectors(java.util.stream.Collectors) QueryOption(com.google.cloud.spanner.Options.QueryOption) List(java.util.List) Struct(com.google.cloud.spanner.Struct) AfterDeleteEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterDeleteEvent) LogFactory(org.apache.commons.logging.LogFactory) AfterSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterSaveEvent) ReadContext(com.google.cloud.spanner.ReadContext) SpannerEntityProcessor(org.springframework.cloud.gcp.data.spanner.core.convert.SpannerEntityProcessor) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) BeforeExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StreamSupport(java.util.stream.StreamSupport) Nullable(javax.annotation.Nullable) SpannerSchemaUtils(org.springframework.cloud.gcp.data.spanner.core.admin.SpannerSchemaUtils) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) TimestampBound(com.google.cloud.spanner.TimestampBound) KeySet(com.google.cloud.spanner.KeySet) AfterReadEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent) Statement(com.google.cloud.spanner.Statement) ApplicationEvent(org.springframework.context.ApplicationEvent) ReadOption(com.google.cloud.spanner.Options.ReadOption) SpannerStatementQueryExecutor(org.springframework.cloud.gcp.data.spanner.repository.query.SpannerStatementQueryExecutor) StringJoiner(java.util.StringJoiner) Log(org.apache.commons.logging.Log) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) AfterQueryEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent) Collections(java.util.Collections) Assert(org.springframework.util.Assert) BeforeExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent) AfterExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterExecuteDmlEvent)

Aggregations

AfterExecuteDmlEvent (org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterExecuteDmlEvent)4 BeforeExecuteDmlEvent (org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent)3 TransactionContext (com.google.cloud.spanner.TransactionContext)2 TransactionCallable (com.google.cloud.spanner.TransactionRunner.TransactionCallable)2 Test (org.junit.Test)2 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)2 DatabaseClient (com.google.cloud.spanner.DatabaseClient)1 Key (com.google.cloud.spanner.Key)1 KeySet (com.google.cloud.spanner.KeySet)1 Mutation (com.google.cloud.spanner.Mutation)1 QueryOption (com.google.cloud.spanner.Options.QueryOption)1 ReadOption (com.google.cloud.spanner.Options.ReadOption)1 ReadContext (com.google.cloud.spanner.ReadContext)1 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)1 ResultSet (com.google.cloud.spanner.ResultSet)1 Statement (com.google.cloud.spanner.Statement)1 Struct (com.google.cloud.spanner.Struct)1 TimestampBound (com.google.cloud.spanner.TimestampBound)1 TransactionRunner (com.google.cloud.spanner.TransactionRunner)1 ArrayList (java.util.ArrayList)1