提交hibernate事务有多昂贵? [英] How expensive is committing a hibernate transaction?

查看:114
本文介绍了提交hibernate事务有多昂贵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的用例,我通过JMS接收关于实体的消息,通过它的一个独特属性(而不是PK),它需要我更新实体的状态:

  HibernateUtil.beginSession(); 
HibernateUtil.beginTransaction();
尝试{
实体实体= dao.getEntityByUniqueProperty(propertyValue);
if(entity == null){
entity = dao.addEntityByUniqueProperty(propertyValue)
}
entity.setSomeProperty(otherPropertyValue);
HibernateUtil.commitTransaction();
} catch(ConstraintViolationException e){
HibernateUtil.rollbackTransaction();
//另外做其他事情
} catch(StaleStateObjectException e){
HibernateUtil.rollbackTransaction();
//另外做其他事情
} finally {
HibernateUtil.closeSession();





$ b

在这个用例中,我必须准备好以下事实:我试图更新尚未创建,所以我请求创建这样的实体(它的模板是与独特的属性精确),然后我改变它。
我的dillema如下:
一方面,我有两个截然不同的块,我应该在适当的时候使用不同的catch子句,但是当我查询时看到实体不在那里的最终情况,但是当我尝试创建它时(因此ConstraintViolationException)是不应该经常发生并插入的,因为在中间的额外提交/ beginTransaction看起来很糟糕。



我主要关心会话同步和JDBC连接在commit / begin发生时的额外性能。

我错了吗?我在哪里找不到优化?我是否错过了某些东西?

提前致谢

解决方案

我想我已经找到了具体的问题我的使用案例,这是只有在实际需要时才打开交易,所以我保存了过早的性能dillema:

  try { 
HibernateUtil.beginSession();
实体实体= dao.getEntityByUniqueProperty(propertyValue);
if(entity == null){
HibernateUtil.beginTransaction();
try {
entity = dao.addEntityByUniqueProperty(propertyValue)
HibernateUtil.commitTransaction();
} catch(ConstraintViolationException e){
HibernateUtil.rollbackTransaction();
HibernateUtil.closeSession();
HibernateUtil.beginSession();
entity = dao.getEntityByUniqueProperty(propertyValue);
//另外做其他事情
}
}
entity.setSomeProperty(otherPropertyValue);
HibernateUtil.commitTransaction();
} catch(StaleStateObjectException e){
HibernateUtil.rollbackTransaction();
//另外做其他事情
} finally {
HibernateUtil.closeSession();
}

这将使我能够定位每个交易中的特定风险并避免提交并在不需要时开启交易。
感谢您的意见。


I have a the following use case where I'm receiving a message via JMS regarding an entity, via a unique property of it (not PK) and it requires me to update the state of the entity:

HibernateUtil.beginSession();  
HibernateUtil.beginTransaction();  
try{  
  Entity entity = dao.getEntityByUniqueProperty(propertyValue);  
  if (entity==null){  
    entity = dao.addEntityByUniqueProperty(propertyValue)  
  }  
  entity.setSomeProperty(otherPropertyValue);
  HibernateUtil.commitTransaction();
} catch (ConstraintViolationException e){  
  HibernateUtil.rollbackTransaction();  
  //Do other things additionally  
} catch (StaleStateObjectException e){  
  HibernateUtil.rollbackTransaction();  
  //Do other things additionally  
} finally {  
  HibernateUtil.closeSession();  
}

In this use case I have to be prepared for the fact that the entity which I'm trying to update is not yet created and so I request that such entity be created (a template of it to be precise with the unique property) and then I change it. My dillema is as follows: On the one hand I have two clearly different blocks and I should use the different catch clauses where appropriate BUT seeing as the end case where the entity is not there when I query but is there a ms later when I try to create it (hence ConstraintViolationException) is something that shouldn't happen too often and to insert because of that an additional commit/beginTransaction at the middle just seems waistfull.

I'm mainly concerned about the additional performance hit of the session synchronization and the JDBC connection which are done when the commit/begin occur.
Am I wrong? Am I looking for optimization where I shouldn't? Am I missing something?
Thanks in advance

解决方案

I think I've actually found the specific question to my use case and that is to open the transaction only when actually needed and so I save the premature performance dillema:

try {
    HibernateUtil.beginSession();
    Entity entity = dao.getEntityByUniqueProperty(propertyValue);
    if (entity==null){
        HibernateUtil.beginTransaction();
        try {
            entity = dao.addEntityByUniqueProperty(propertyValue)
            HibernateUtil.commitTransaction();
        } catch (ConstraintViolationException e){
            HibernateUtil.rollbackTransaction();
            HibernateUtil.closeSession();
            HibernateUtil.beginSession();
            entity = dao.getEntityByUniqueProperty(propertyValue);
            //Do other things additionally
        }
    }
    entity.setSomeProperty(otherPropertyValue);
    HibernateUtil.commitTransaction();
} catch (StaleStateObjectException e){
    HibernateUtil.rollbackTransaction();
    //Do other things additionally
} finally {
    HibernateUtil.closeSession();
}

This will enable me to localize the specific risks in each Transaction and also avoid commiting and opening a transaction when it is unneeded. Thanks for your comments.

这篇关于提交hibernate事务有多昂贵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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