spring-boot - Spring Boot @Transaction(timeout = 1)

标签 spring-boot transactions spring-data-jpa

我正在使用 spring-boot-starter-data-jpa从 Spring Boot 1.5.6

例如,当事务超时大于 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();

异常(exception)是:

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>)
... `

最佳答案

我发现决定hibernate的事务超时的逻辑有问题。因此,刚刚提出了以下问题。
https://hibernate.atlassian.net/browse/HHH-14062?atlOrigin=eyJpIjoiNTBhZTgzODBmYTFjNDU2MDhhYzM4NjU3ZjFiYzY5NjAiLCJwIjoiaiJ9

我要直接发 PR 来修复它。我不知道它什么时候会改变,但如果 Hibernate 团队确定该错误是正确的,我认为它会很快修复。

关于spring-boot - Spring Boot @Transaction(timeout = 1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46216018/

相关文章:

java - hibernate + Spring + Tomcat

java - 在 Spring Boot 应用程序中对 @Value 注释字段实现约束

sql-server - SQL Server 嵌套事务

.net - 如何防止OleDbConnection尝试加入分布式事务?

java - JPA 软删除存储库 + 审核

java - 无法从 Spring Boot 连接到 postgresql 架构 - 'org.postgresql.util.PLSQLException: ERROR: schema "测试“不存在”

java - HSQLDB在Spring项目中的持久性

java - Spring AMQP 多线程事务

hibernate - 将 JPA 规范转换为 JPQL

spring-data-jpa - 如何将 Spring 数据查询记录到 Couchbase 数据库