为了能够在事务之外工作,我一直在使用Hibernate 3.6中的隔离器(org.hibernate.engine.transaction.Isolater)。
我必须升级到hibernate 4.3或更高版本,并且隔离器在这个版本的Hibernate中不再存在。在hibernate中发生这种更改之后,是否有任何替代执行隔离工作的方法?
发布于 2017-05-14 07:25:19
Hibernate 4和5中有一个替代品,但它似乎并不真正用于Hibernate内部的外部。
您需要访问一个SessionImplementor,所以您必须将会话转换到它。我找不到一种不需要转换getCurrentSession()结果的访问方式。然后可以使用TransactionCoordinator创建一个与隔离器类似的IsolationDelegate:
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);发布于 2017-04-08 14:59:50
您可以做的一件事是使用Hibernate操作队列注册一个特定的回调,该回调将在提交之前或根据用例立即触发。这些课程是:
org.hibernate.action.spi.BeforeTransactionCompletionProcess org.hibernate.action.spi.AfterTransactionCompletionProcess
对于您的用例,您似乎希望使用AfterTransactionCompletionProcess。为了向特定会话注册回调,您可以:
session.getActionQueue().registerProcess(
new AfterTransactionCompletionProcess() {
@Override
void doAfterTransactionCompletion(
boolean success,
SharedSessionContractImplementor session) {
// do your logic here
}
}
);https://stackoverflow.com/questions/43288391
复制相似问题