Hibernate - 想要将删除和存储放在一个“事务”中,如果发生pb,则所有事务都将回滚

发布于 2024-12-06 16:16:32 字数 767 浏览 0 评论 0原文

我有一种方法可以删除一些项目,然后插入一些其他项目。

public void refresh() {
    if (newitems != null) {
        toto.clear();
        for (totoDao p : newItems) {
            toto.store(p);
        }
    }
    newitems = null;
}

public void clear() {
    final Session session = this.getHibernateUtil().getSession();
    int n = session.createQuery(DELETE).executeUpdate();
    session.clear();
    session.flush();
}

public void store(TotoDao object) {         
    final Session session = this.getHibernateUtil().getSession();
    session.saveOrUpdate(object);
    session.flush();
}

目前,我在clear()方法中有一个刷新,在store()方法中有另一个刷新。

我想将所有这些内容添加到一个“事务”中,如果出现某些情况,应用程序会在 toto.clear() 之后重新启动,例如,我希望该事务回滚所有块。

那么,实现性能和持久性的最佳解决方案是什么?

谢谢 !

I have a method which delete some items, and next insert some others items.

public void refresh() {
    if (newitems != null) {
        toto.clear();
        for (totoDao p : newItems) {
            toto.store(p);
        }
    }
    newitems = null;
}

public void clear() {
    final Session session = this.getHibernateUtil().getSession();
    int n = session.createQuery(DELETE).executeUpdate();
    session.clear();
    session.flush();
}

public void store(TotoDao object) {         
    final Session session = this.getHibernateUtil().getSession();
    session.saveOrUpdate(object);
    session.flush();
}

For the moment I have one flush in clear() method and other one in store() method.

I want to add all of theses stuffs in one "transaction", if somethings appears, a application restart just after the toto.clear(), for example, i want that transaction rollback all the block.

So what are the best solution for perfomances and persistances ?

Thx !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您的好友蓝忘机已上羡 2024-12-13 16:16:33
    session = sessionFactory.openSession();  
    Transaction tx = null;

try{
     tx = session.beginTransaction();  

    ... your add/store/delete/.... 
    tx.commit();  
}catch(Throwable(or other type of Exception you like) ex){
    tx.rollback();
}
    session = sessionFactory.openSession();  
    Transaction tx = null;

try{
     tx = session.beginTransaction();  

    ... your add/store/delete/.... 
    tx.commit();  
}catch(Throwable(or other type of Exception you like) ex){
    tx.rollback();
}
憧憬巴黎街头的黎明 2024-12-13 16:16:33

只需将这两个方法调用包含在一个唯一的事务中即可。

冲洗与交易没有任何关系。它只是意味着“真正执行持久保存会话中所做的修改所需的所有 SQL 语句”。但提交只会在事务结束时完成。

手动刷新会话几乎总是不必要的。必要时让 Hibernate 来做。

另请注意,DAO 应该是一个允许查询和更新实体的服务对象。它不应该是一个持久的实体。

阅读 http://docs.jboss.org/hibernate /core/3.6/reference/en-US/html_single/#transactionshttp://docs.jboss.org/hibernate/ core/3.6/reference/en-US/html_single/#objectstate-flushing

Just enclose these two method calls inside a unique transaction.

Flushing doesn't have anything to do with transactions. It just means "really execute all the SQL statements needed to persist the modifications made in the session". But the commit will only be done at the end of the transaction.

Flushing the session manually is almost always unnecessary. Let Hibernate do it when it has to.

Also, note that a DAO is supposed to be a service object allowing to query and update entities. It's not supposed to be a persistent entity.

Read http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#transactions and http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#objectstate-flushing

与君绝 2024-12-13 16:16:33

Spring 为事务管理提供了很好的解决方案。
在此页面上,您将找到使用 XML 文件配置 spring/hibernate 的方法。

如果您需要一些解释,请询问,我会尽快帮助您。

一些例子:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

  <aop:config>
    <aop:pointcut id="pointcutId" expression="execution(* com.stackoverflow.service.FooService.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutId"/>
  </aop:config>

  <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
      <!-- all methods starting with 'get' are read-only -->
      <tx:method name="get*" read-only="true"/>
      <!-- other methods (By example Rollback for NullPointerException)-->
      <tx:method name="*" read-only="false" rollback-for="NullPointerException"/>
    </tx:attributes>
  </tx:advice>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
   <property name="sessionFactory" ref="sessionFactory" /> 
</bean>

 ...

</beans>

Spring has good solutions for transaction management.
On this page you'll find the way to configure spring/hibernate with XML files.

If you need some explanation, just ask and i'll try to help you asap.

Some exemple:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

  <aop:config>
    <aop:pointcut id="pointcutId" expression="execution(* com.stackoverflow.service.FooService.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutId"/>
  </aop:config>

  <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
      <!-- all methods starting with 'get' are read-only -->
      <tx:method name="get*" read-only="true"/>
      <!-- other methods (By example Rollback for NullPointerException)-->
      <tx:method name="*" read-only="false" rollback-for="NullPointerException"/>
    </tx:attributes>
  </tx:advice>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
   <property name="sessionFactory" ref="sessionFactory" /> 
</bean>

 ...

</beans>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文