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

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

问题描述

我正在尝试使用hibernate写入多个数据库。我在单个会话对象中封装了写入和读取/写入会话。然而,当我去保存时,我收到了很多对象已经与另一个会话关联的错误:非法尝试将集合与两个打开的会话相关联



这里是我的代码:

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

我尝试过驱逐物体并冲洗;然而,这会导致Row被另一个事务更新或删除的问题......即使这两个会话指向了不同的数据库。

  public class MultiSessionObject实现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);
}
}

除上述之外,我还试图使用休眠的复制功能。



有没有人成功地将一个对象保存到两个具有相同模式的数据库?

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



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



尝试设置事务边界:

  public class MultiSessionObject implements Session {

private Session writeOnlySession;
private Session readWriteSession;

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

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

事务writeOnlySessionTx = null;
尝试{
writeOnlySessionTx = writeOnlySession.beginTransaction();
writeOnlySession.merge(arg0);
writeOnlySessionTx.commit();
} catch(RuntimeException e){
if(writeOnlySessionTx!= null&&&& writeOnlySessionTx.isActive())
writeOnlySessionTx.rollback();
throw e;
}
}
}


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"

Here is my code:

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?

解决方案

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天全站免登陆