Hibernate 将对象保存到多个会话 [英] Hibernate Save Object to Multiple Sessions

查看:28
本文介绍了Hibernate 将对象保存到多个会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用休眠写入多个数据库.我已经将写和读/写会话封装在一个会话对象中.但是,当我去保存时,我收到很多错误,这些对象已经与另一个会话相关联:非法尝试将一个集合与两个打开的会话相关联"

I am attempting to write to multiple databases using hibernate. I have encapsulated write and read/write sessions within a single session object. However, when I go to save I get a lot of errors that the objects are already associated with another session: "Illegal attempt to associate a collection with two open sessions"

这是我的代码:

public class MultiSessionObject implements Session {

       private Session writeOnlySession;
       private Session readWriteSession;

       @Override
       public void saveOrUpdate(Object arg0) throws HibernateException {
              readWriteSession.saveOrUpdate(arg0);
              writeOnlySession.saveOrUpdate(arg0);
       }
}

我尝试过驱逐对象并刷新;然而,这会导致行被另一个事务更新或删除"的问题......即使两个会话都指向不同的数据库.

I have tried evicting the object and flushing; however, that causes problems with "Row was updated or deleted by another transaction"... even though both sessions point to different databases.

public class MultiSessionObject implements Session {

       private Session writeOnlySession;
       private Session readWriteSession;

       @Override
       public void saveOrUpdate(Object arg0) throws HibernateException {
              readWriteSession.saveOrUpdate(arg0);
              readWriteSession.flush();
              readWriteSession.evict(arg0);

              writeOnlySession.saveOrUpdate(arg0);
              writeOnlySession.flush();
              writeOnlySession.evict(arg0);
       }
}

除上述之外,我还尝试使用休眠的复制功能.这也是没有错误的不成功.

In addition to the above, I have also attempted using the replicate functionality of hibernate. This was also unsuccessful without errors.

有没有人成功地将对象保存到具有相同架构的两个数据库中?

Has anyone successfully saved an object to two databases that have the same schema?

推荐答案

saveOrUpdate 尝试将给定的实体重新附加到当前正在运行的 Session,因此代理(LAZY 关联)绑定到 Hibernate Session.尝试使用 merge 而不是 saveOrUpdate,因为 merge 只是将分离的实体状态复制到新检索的托管实体.这样,提供的参数永远不会附加到会话中.

The saveOrUpdate tries to reattach a given Entity to the current running Session, so Proxies (LAZY associations) are bound to the Hibernate Session. Try using merge instead of saveOrUpdate, because merge simply copies a detached entity state to a newly retrieved managed entity. This way, the supplied arguments never gets attached to a Session.

另一个问题是事务管理.如果使用线程绑定事务,那么如果要从同一个线程更新两个数据源,则需要两个显式事务.

Another problem is Transaction Management. If you use Thread-bound Transaction, then you need two explicit transactions if you want to update two DataSources from the same Thread.

也尝试明确设置事务边界:

Try to set the transaction boundaries explicitly too:

public class MultiSessionObject implements Session {

   private Session writeOnlySession;
   private Session readWriteSession;

   @Override
   public void saveOrUpdate(Object arg0) throws HibernateException {

        Transaction readWriteSessionTx = null;
        try {
            readWriteSessionTx = readWriteSession.beginTransaction();
            readWriteSession.merge(arg0);
            readWriteSessionTx.commit();
        } catch (RuntimeException e) {
            if ( readWriteSessionTx != null && readWriteSessionTx.isActive() ) 
                readWriteSessionTx.rollback();
            throw e;
        }

        Transaction writeOnlySessionTx = null;
        try {
            writeOnlySessionTx = writeOnlySession.beginTransaction();
            writeOnlySession.merge(arg0);
            writeOnlySessionTx.commit();
        } catch (RuntimeException e) {
            if ( writeOnlySessionTx != null && writeOnlySessionTx.isActive() ) 
                writeOnlySessionTx.rollback();
            throw e;
        }
   }
}

这篇关于Hibernate 将对象保存到多个会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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