关于此异常有很多问题,但经过研究后我认为这并不重复,因为它与已经运行的代码上的版本迁移后的问题有关。
我试图从Spring 4.3.9.RELEASE / Hibernate 4.3.6.Final迁移到Spring 5.0.4.RELEASE / Hibernate 5.2.14.Final。除了根据需要更改依赖关系并根据需要将包名称更改为hibernate5之外,代码不会更改。我一直收到异常 javax.persistence.TransactionRequiredException:没有正在进行的交易
@Transactional属性应用于适当的位置。我做了很多研究,以找出原因,特别是当它适用于从一个版本迁移到另一个版本时。以下日志实际显示事务已启动,但仍然抛出异常
14:39:28,719 DEBUG [main] org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:55) - begin
14:39:28,719 TRACE [main] org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:66) - Preparing to begin transaction via JDBC Connection.setAutoCommit(false)
14:39:28,719 TRACE [main] org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:68) - Transaction begun via JDBC Connection.setAutoCommit(false)
14:39:28,719 TRACE [main] org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.afterBeginCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:150) - ResourceLocalTransactionCoordinatorImpl#afterBeginCallback
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3466) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) [spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) [spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
14:39:28,812 DEBUG [main] org.hibernate.engine.transaction.internal.TransactionImpl.rollback(TransactionImpl.java:98) - rolling back
14:39:28,812 TRACE [main] org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.rollback(AbstractLogicalConnectionImplementor.java:115) - Preparing to rollback transaction via JDBC Connection.rollback()
很多调试让我跟随函数org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.isJoined(),其中physicalTransactionDelegate为null。因为isJoined函数返回false,所以抛出异常。
@Override
public boolean isJoined() {
return physicalTransactionDelegate != null && getTransactionDriverControl().isActive( true );
}
[基于编辑的评论]
它的java配置。代码崩溃非常简单。有人已经自动装配一个dao并调用以下方法。它基本上检查记录是否存在于数据库中,如果它存在,更新它或创建一个新记录。
@Transactional(propagation = Propagation.REQUIRED)
public void registerApplication(final ApplicationType type, final Class<?> versionedClass, final String extraData) throws Exception {
final Properties pr = getVersionInformation(versionedClass);
final String commit = pr.getProperty("git.commit.id");
if (commit == null) {
throw new NullPointerException("Could not find property git.commit.id");
}
VersionInfo info = extraData == null ? findBy(type, commit) : findBy(type, commit, extraData);
if (info != null) {
log.info("Found entry for the version, just updating usage count");
info.incrementUsageCount();
} else {
info = new VersionInfo();
info.setApplicationType(type);
info.setCommitId(commit);
info.incrementUsageCount();
info.setExtraData(extraData);
}
save(info);
log.info("Saved version information for {}:{}:{}", type, commit, extraData);
}
我应该在代码中更改哪些内容才能让它再次运行?