Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot @Transaction(timeout = 1)

I am using spring-boot-starter-data-jpa from Spring Boot 1.5.6

Everything works fine when the transaction timeout is greater than 1, for example @Transaction(timeout = 2), @Transaction(timeout = 5), or @Transaction(timeout = 10), but when i set it to @Transaction(timeout = 1) it throws an exception in less than one second. Here is a snapshot of my code:

@Transactional(readOnly=true)
public interface IUserRepository extends CrudRepository<UserEntity,Long>{
     @Transactional(timeout = 1)
     Iterable<UserEntity> findAll();

And the exception is:

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>)
... `
like image 326
DHARMENDRA SINGH Avatar asked Oct 29 '22 02:10

DHARMENDRA SINGH


1 Answers

I found that there is a problem with the logic that determines hibernate's transaction timeout. Therefore, the following issues have just been raised. https://hibernate.atlassian.net/browse/HHH-14062?atlOrigin=eyJpIjoiNTBhZTgzODBmYTFjNDU2MDhhYzM4NjU3ZjFiYzY5NjAiLCJwIjoiaiJ9

I am going to send a PR directly to fix it. I don't know when it will change, but if the Hibernate team determines that the bug is correct, I think it will be fixed soon.

like image 59
ssosso Avatar answered Nov 15 '22 11:11

ssosso