[英]Getting “javax.persistence.TransactionRequiredException: no transaction is in progress” in Spring Boot Application
I am using JPA+Hibernate in my Spring Boot Application.我在我的 Spring 引导应用程序中使用 JPA+Hibernate。 My Service Method is as follows:-
我的服务方法如下:-
@Override
public Employee createEmployee(Employee employeeModel) {
logger.debug("entered the createEmployee method");
Optional<Employee> optionalEmployee = repository.findByEmpShortNm(employeeModel.getEmpShortNm());
logger.debug("Fetched the optional");
if(optionalEmployee.isPresent())
return optionalEmployee.get();
else {
logger.debug("Persisting employee.....");
return repository.save(employeeModel);
}
}
EmployeeRepository.java has following method:- EmployeeRepository.java有以下方法:-
@Lock(LockModeType.PESSIMISTIC_WRITE)
Optional<Employee> findByEmpShortNm(String uuid);
My Application was working fine until I applied Lock annotation
on my repository method.在我在存储库方法上应用
Lock annotation
之前,我的应用程序运行良好。 After applying Lock
createEmployee method is throwing following exception:-应用
Lock
createEmployee 方法后抛出以下异常: -
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1560)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533)
at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1581)
at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getSingleResult(CriteriaQueryTypeQueryAdapter.java:111)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:196)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:88)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:154)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:142)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor$QueryMethodInvoker.invoke(QueryExecutorMethodInterceptor.java:195)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
Why it's not able to get a transaction while it was working fine when I was not using @Lock
Annoation.为什么当我不使用
@Lock
Annoation 时它工作正常时无法获得交易。 What's the relation b/w @Lock and Transaction. b/w @Lock 和 Transaction 有什么关系。
I would appreciate if any reference can be provided.如果可以提供任何参考,我将不胜感激。
Solution: you should annotate your service with @Transactional
解决方案:您应该使用
@Transactional
注释您的服务
When the lock is explicitly enabled and there is no active transaction, the underlying JPA implementation will throw a TransactionRequiredException.
当显式启用锁且没有活动事务时,底层 JPA 实现将抛出 TransactionRequiredException。
It is somehow logical when you don't have your own transaction, why do you want to lock the entity object?当您没有自己的事务时,这在某种程度上是合乎逻辑的,为什么要锁定实体 object? You should have a transaction outside repository method during which, you want to lock entity object.
您应该有一个存储库之外的事务方法,在此期间您要锁定实体 object。
Read this article:阅读这篇文章:
https://www.baeldung.com/java-jpa-transaction-locks https://www.baeldung.com/java-jpa-transaction-locks
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.