Trying to rollback to savepoint when transaction has status MARKED_ROLLBACK

This was possible prior to v5.0.
What happened in 5.0 is that transaction marked for rollback in no longer treated as active in https://github.com/hibernate/hibernate-orm/commit/b476094d43e24db36d970e6c0c21607fe5d42832#diff-49cee7a0fb9d479f53cd7c2c12db6dbe

My code flow goes like this:
start transaction
get from persistant queue
mark queue element
set savepoint
do work
-catch exception
–rollback to savepoint
–update queue element for retry
commit

The problem is now my rollback to savepoint fails and the whole transaction is rolled back sending me into an endless loop.
Any ideas how I can get around this?

Hibernate does not support SAVEPOINTS. In fact, most JDBC Driver don’t support nested transactions either.

In your case, it’s unclear why exactly you need to use a SAVEPOINT because from START TRANSACTION to SET SAVEPOINT, it does not look like you are doing any DB operation.

More, once you get an Exception, the Session should be discarded and you need to try with a new Session.

We use this mechanism: java.sql.Connection#setSavepoint()
The code works well for most runtime exceptions because they don’t mark transaction for rollback.
What I need is:
either

  • some way to prevent transaction to be marked for rollback for any runtime exception
    or
  • the pre 5.0 behaviour where a transaction marked for rollback is treated as active in org.hibernate.context.internal.ThreadLocalSessionContext.TransactionProtectionWrapper

I don’t think this use case is supported by Hibernate. Even before 5.x, it just happened to work for you, although it was never intended to work like that.

You need to create a replicating test case and open a Jira issue where you describe the use case. We’ll analyse it afterward and see if it can be implemented.

Looks at this point like my only option is to make a patch fixing the bug in ThreadLocalSessionContext.TransactionProtectionWrapper. I’ll submit a pullrequest later.