Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

java - javax.persistence.TransactionRequiredException: no transaction is in progress after migrating spring/hibernate version

There are lot questions on this exception, but after research I believe this is not duplicate as it relates to issues after version migration on already working code.

I'm trying to migrate from Spring 4.3.9.RELEASE/ Hibernate 4.3.6.Final to Spring 5.0.4.RELEASE/ Hibernate 5.2.14.Final. The code is not changed except changing the dependencies as needed and changing package names to hibernate5 as needed. I keep getting the exception javax.persistence.TransactionRequiredException: no transaction is in progress

@Transactional attribute is applied at appropriate places. I have done lot of research to find out why especially when it applies to migration from one version to another. Following log actually shows that transaction is started, but still exception is being thrown

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()

Lot of debugging has led me to following function org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.isJoined() where physicalTransactionDelegate is null. Because isJoined function returns false, exception is thrown.

@Override
public boolean isJoined() {
    return physicalTransactionDelegate != null && getTransactionDriverControl().isActive( true );
}

[Edit based op comments]

It's java configuration. The code crashing is very simple. Someone has autowired a dao and calls following method. It's basically checking if the record exists in database, if it exists, update it, or create a new one.

@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);
}

What should I change in my code to make it work again?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...