spring-data-commons icon indicating copy to clipboard operation
spring-data-commons copied to clipboard

Issue with @DomainEvents with a spring data repository having @cacheable decorated methods [DATACMNS-1247]

Open spring-projects-issues opened this issue 8 years ago • 3 comments

Gaurav Rawat opened DATACMNS-1247 and commented

Have any one had issues with using @DomainEvents with a Spring Data repository having @Cacheable decorated methods. It seems the domainEvents object don't get initialized when data comes from cache and hence give a NPE . This happens cause the transient variable not getting initialized when the data is coming from cache.

public class AbstractAggregateRoot<A extends AbstractAggregateRoot<A>> {
	private transient final @Transient List<Object> domainEvents = new ArrayList<>();

My caching repository is pretty simple looks like this, just the cache implementation I am using is based on Apache ignite. As a workaround currently I am not using domain events for cacheable methods, but I hope we can resolve this to get over the boilerplate and NPE issues


Affects: 1.13.8 (Ingalls SR8), 2.0.2 (Kay SR2)

spring-projects-issues avatar Jan 20 '18 19:01 spring-projects-issues

Oliver Drotbohm commented

The caching interceptor is ordered before all other one, including the one publishing domain events. The entire scenario looks pretty weird as caching mutable objects is a bad idea in the first place as now all of a sudden, multiple threads will have access to instances that are usually only accessible in a single thread

spring-projects-issues avatar Jan 20 '18 22:01 spring-projects-issues

Gaurav Rawat commented

Agree with your point about caching mutable entities .

But there could be a case where we do need to cache a search method or api in the repository , and this does break when the same data is say retrieved from cache ,modified and persisted.And subsequently domain events are fired . This could be handled if I seperate the command and query as I have done now but we have a case where we do some intensive processing in memory and don't want to go every time to our persistence store .Also we have reverted to simple spring events right now . Another approach we have done is using write behind in our apache ignite repositories and not using the @cacheable abstraction for our use case.

spring-projects-issues avatar Jan 28 '18 16:01 spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues avatar Jan 06 '21 10:01 spring-projects-issues

We do not intend to provide a general utility to make domain events work with cached object instances as we consider that approach invalid usage. With Spring's extension points, you can provide your application-specific interceptor to materialize domain events and decouple object instances to avoid shared mutable state.

mp911de avatar Feb 24 '23 09:02 mp911de