Spring Boot @Transaction(timeout = 1)

6
我正在使用来自Spring Boot 1.5.6的spring-boot-starter-data-jpa。当事务超时时间大于1时,例如@Transaction(timeout = 2)@Transaction(timeout = 5)@Transaction(timeout = 10),一切正常,但是当我将其设置为@Transaction(timeout = 1)时,在不到一秒钟内就会抛出异常。以下是我的代码快照:
@Transactional(readOnly=true)
public interface IUserRepository extends CrudRepository<UserEntity,Long>{
     @Transactional(timeout = 1)
     Iterable<UserEntity> findAll();

异常信息如下:

15:18:11.078 [http-nio-9999-exec-2] ERROR [o.a.c.c.C.[.[.[.[dispatcherServlet]:181] - Servlet.service() for servlet [dispatcherServlet] in context with path [/springJPA-LOCAL] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: nested exception is javax.persistence.PersistenceException] with root cause
javax.persistence.PersistenceException: null
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:492)
at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:50)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:329)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy198.findAll(Unknown Source)
at com.dharam.springJPA.service.impl.UserServiceImpl.doFindAll(UserServiceImpl.java:29)
at com.dharam.springJPA.Controller.SpringJPAController.startFindAll(SpringJPAController.java:39)
at com.dharam.springJPA.Controller.SpringJPAController$$FastClassBySpringCGLIB$$1b008118.invoke(<generated>)
... `

那么,问题到底是什么。调用可能不会在1秒钟内完成,因此会发生超时并且您可能已经收到异常。 - pvpkiran
请正确格式化代码。 - PRATHAP S
如何处理超时后发生的异常? - DHARMENDRA SINGH
我处于类似的情况下,当 timeout=1 时,超时时间少于1秒,但当timeout>1时,超时却永远不会发生!如此奇怪。 - CDT
@CDT 你在那里找到任何线索了吗? - DHARMENDRA SINGH
1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接