I have been using Isolater (org.hibernate.engine.transaction.Isolater) from Hibernate 3.6 in order to be able to do work outside of a transaction.
I have to upgrade to Hibernate 4.3 or above and Isolater is no longer there in this version of hibernate. Is there any substitute to performing isolated work after this change in hibernate?
There is a replacement in Hibernate 4 and 5 but it seems like it is not really intended to be used outside of the Hibernate internals.
You need access to a SessionImplementor so you'll have to cast your Session to that. I can't find a way to get access that doesn't involve casting the result of getCurrentSession(). Then you can use the TransactionCoordinator to create an IsolationDelegate which works similarly to Isolater:
SessionImplementor session = (SessionImplementor) sessionFactory.getCurrentSession();
IsolationDelegate isolationDelegate = session.getTransactionCoordinator().createIsolationDelegate();
isolationDelegate.delegateWork(new AbstractWork() {
@Override
public void execute(Connection connection) throws SQLException {
// This will run on a separate connection with the current
// transaction suspended (if necessary)
}
}, false);
One thing you can do is use the Hibernate action queue to register a specific callback that fires right before the commit or immediately after depending on your use case. Those classes are:
org.hibernate.action.spi.BeforeTransactionCompletionProcess
org.hibernate.action.spi.AfterTransactionCompletionProcess
For your use case, it seems you would want to use an AfterTransactionCompletionProcess. In order to register the callback with a specific session you would:
session.getActionQueue().registerProcess(
new AfterTransactionCompletionProcess() {
@Override
void doAfterTransactionCompletion(
boolean success,
SharedSessionContractImplementor session) {
// do your logic here
}
}
);