Skip to content

Commit a01f710

Browse files
committed
DATAJPA-1575 - Conditionally register SurroundingTransactionDetectorMethodInterceptor.
We now register SurroundingTransactionDetectorMethodInterceptor only for interfaces that return a Stream to detect a surrounding transaction.
1 parent 74ff5b3 commit a01f710

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import lombok.extern.slf4j.Slf4j;
2121

2222
import java.io.Serializable;
23+
import java.lang.reflect.Method;
2324
import java.util.Optional;
25+
import java.util.stream.Stream;
2426

2527
import javax.persistence.EntityManager;
2628
import javax.persistence.Tuple;
@@ -46,12 +48,14 @@
4648
import org.springframework.data.repository.core.support.RepositoryComposition;
4749
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
4850
import org.springframework.data.repository.core.support.RepositoryFragment;
51+
import org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor;
4952
import org.springframework.data.repository.query.QueryLookupStrategy;
5053
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
5154
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
5255
import org.springframework.data.repository.query.ReturnedType;
5356
import org.springframework.lang.Nullable;
5457
import org.springframework.util.Assert;
58+
import org.springframework.util.ReflectionUtils;
5559

5660
/**
5761
* JPA specific generic repository factory.
@@ -86,6 +90,12 @@ public JpaRepositoryFactory(EntityManager entityManager) {
8690
this.entityPathResolver = SimpleEntityPathResolver.INSTANCE;
8791

8892
addRepositoryProxyPostProcessor(crudMethodMetadataPostProcessor);
93+
addRepositoryProxyPostProcessor((factory, repositoryInformation) -> {
94+
95+
if (hasMethodReturningStream(repositoryInformation.getRepositoryInterface())) {
96+
factory.addAdvice(SurroundingTransactionDetectorMethodInterceptor.INSTANCE);
97+
}
98+
});
8999

90100
if (extractor.equals(PersistenceProvider.ECLIPSELINK)) {
91101
addQueryCreationListener(new EclipseLinkProjectionQueryCreationListener(entityManager));
@@ -231,6 +241,19 @@ protected RepositoryComposition.RepositoryFragments getRepositoryFragments(Repos
231241
return fragments;
232242
}
233243

244+
private static boolean hasMethodReturningStream(Class<?> repositoryClass) {
245+
246+
Method[] methods = ReflectionUtils.getAllDeclaredMethods(repositoryClass);
247+
248+
for (Method method : methods) {
249+
if (Stream.class.isAssignableFrom(method.getReturnType())) {
250+
return true;
251+
}
252+
}
253+
254+
return false;
255+
}
256+
234257
/**
235258
* Query creation listener that informs EclipseLink users that they have to be extra careful when defining repository
236259
* query methods using projections as we have to rely on the declaration order of the accessors in projection

0 commit comments

Comments
 (0)