Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Commit 1909fe1

Browse files
authored
Fix auditing when running through DatastoreTemplate.performTransaction (#2604)
The new `DatastoreTemplate` instance should have the same `ApplicationEventPublisher` as the original one, so that `AuditingHandler` can be called. Fixes: #2603.
1 parent 7231b7d commit 1909fe1

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

spring-cloud-gcp-data-datastore/src/main/java/org/springframework/cloud/gcp/data/datastore/core/DatastoreTemplate.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,14 @@ public <A> A performTransaction(Function<DatastoreOperations, A> operations) {
434434
}
435435
return ((Datastore) getDatastoreReadWriter())
436436
.runInTransaction(
437-
(DatastoreReaderWriter readerWriter) -> operations.apply(new DatastoreTemplate(() -> readerWriter,
438-
DatastoreTemplate.this.datastoreEntityConverter,
439-
DatastoreTemplate.this.datastoreMappingContext,
440-
DatastoreTemplate.this.objectToKeyFactory)));
437+
(DatastoreReaderWriter readerWriter) -> {
438+
DatastoreTemplate template = new DatastoreTemplate(() -> readerWriter,
439+
DatastoreTemplate.this.datastoreEntityConverter,
440+
DatastoreTemplate.this.datastoreMappingContext,
441+
DatastoreTemplate.this.objectToKeyFactory);
442+
template.setApplicationEventPublisher(DatastoreTemplate.this.eventPublisher);
443+
return operations.apply(template);
444+
});
441445
}
442446

443447
@Override

spring-cloud-gcp-data-datastore/src/test/java/org/springframework/cloud/gcp/data/datastore/core/DatastoreTemplateAuditingTests.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,7 @@
5353
* Tests the auditing features of the template.
5454
*
5555
* @author Chengyuan Zhao
56+
* @author Frank Pavageau
5657
*/
5758
@RunWith(SpringRunner.class)
5859
@ContextConfiguration
@@ -62,6 +63,8 @@ public class DatastoreTemplateAuditingTests {
6263

6364
@Autowired
6465
DatastoreTemplate datastoreTemplate;
66+
@Autowired
67+
Datastore datastore;
6568

6669
@Test
6770
public void testModifiedNullProperties() {
@@ -82,6 +85,20 @@ public void testModifiedPrevProperties() {
8285
this.datastoreTemplate.saveAll(Collections.singletonList(testEntity));
8386
}
8487

88+
@Test
89+
public void testInTransaction() {
90+
when(datastore.runInTransaction(any()))
91+
.thenAnswer(invocation -> {
92+
Datastore.TransactionCallable<?> callable = invocation.getArgument(0);
93+
return callable.run(datastore);
94+
});
95+
96+
TestEntity testEntity = new TestEntity();
97+
testEntity.id = "a";
98+
99+
this.datastoreTemplate.performTransaction(operations -> operations.save(testEntity));
100+
}
101+
85102
/**
86103
* Spring config for the tests.
87104
*/

0 commit comments

Comments
 (0)