在Hibernate 4.x及更高版本中是否有替换Isolater(Hibernate 3.6)? [英] Is there a replacement for Isolater (Hibernate 3.6) in Hibernate 4.x and above?

查看:147
本文介绍了在Hibernate 4.x及更高版本中是否有替换Isolater(Hibernate 3.6)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Hibernate 3.6的Isolater(org.hibernate.engine.transaction.Isolater),以便能够在事务之外工作。



我必须升级到Hibernate 4.3或更高版本,并且此版本的hibernate中不再有Isolater。在Hibernate中发生这种变化后,是否有任何替代方案可以执行独立的工作?

解决方案

看起来它并不是真的打算在Hibernate内部使用。



您需要访问 SessionImplementor 所以你必须把你的Session放到那里。我找不到一种方法来获取不涉及getCurrentSession()的结果的访问。然后,您可以使用TransactionCoordinator创建一个 IsolationDelegate ,其工作方式与Isolater类似:

  SessionImplementor session =(SessionImplementor)sessionFactory.getCurrentSession(); 
IsolationDelegate isolationDelegate = session.getTransactionCoordinator()。createIsolationDelegate();
isolationDelegate.delegateWork(new AbstractWork(){
@Override
public void execute(Connection connection)throws SQLException {
//这将在一个单独的连接上运行,当前
//暂停交易(如果需要)
}
},false);


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

这篇关于在Hibernate 4.x及更高版本中是否有替换Isolater(Hibernate 3.6)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆