2012-02-13 37 views
0

我們在我當前的項目中使用hibernateTemplate作爲我們的dao。我有,我想,如果事務中任何操作未能有回滾事務的情況下(不是火箭科學,對吧?)..所以基本上,這裏是方法:HibernateTemplate,bulkUpdate和flush模式

// Reserve a deal and add the 'pending order' all in one transaction 
@Transactional(propagation = Propagation.REQUIRES_NEW) 
private MyUserOrder reserveQuantity(MyUserOrder userOrder, Date updatedOn) throws MyAPIException{ 

    userOrder.setOrderStatus(OrderStatus.PENDING); 
    int orderReserved = orderDao.reserveQuantity(userOrder); 

    //for some reason hibernate is flushing on the above line??? 

    if (dealsReserved < 1) 
      throw new MyAPIException(ExceptionCode.INSUFFICIENT_QUANTITY); 

    userOrder = userOrderDao.save(userOrder); 

    //hibernate flushes again! 

    return userOrder; 
} 

所以,基本上我們從數據庫訂單可用性表中保留X訂單...然後將掛單訂單保存到數據庫。我希望兩個語句在同一個事務中執行,所以如果一個語句失敗,它們都會回滾。不幸的是,即使整個方法應該在自己的事務中運行,看起來.reserveQuantity的聲明也會立即提交。

這裏是日誌..

13:09:28.007 [[email protected]] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 13291529680 
13:09:28.008 [[email protected]] DEBUG o.s.o.hibernate3.SessionFactoryUtils - Registering Spring transaction synchronization for new Hibernate Session 
13:09:28.009 [[email protected]] DEBUG o.s.orm.hibernate3.HibernateTemplate - Found thread-bound Session for HibernateTemplate 
13:09:28.011 [[email protected]] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 
13:09:28.011 [[email protected]] DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection 
13:09:28.012 [[email protected]] DEBUG o.s.j.d.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:db2://172.26.10.144:60000/devdb] 
13:09:28.189 [[email protected]] DEBUG org.hibernate.SQL - update deal set remaining_quantity=remaining_quantity-?, updated_on=?, updated_by=? where id=? and remaining_quantity>=? 
13:09:28.242 [[email protected]] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 
13:09:28.243 [[email protected]] DEBUG org.hibernate.jdbc.ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources! 
13:09:28.244 [[email protected]] DEBUG o.s.orm.hibernate3.HibernateTemplate - Not closing pre-bound Hibernate Session after HibernateTemplate 
13:09:28.245 [[email protected]] DEBUG o.s.o.hibernate3.SessionFactoryUtils - Flushing Hibernate Session on transaction synchronization 
13:09:28.246 [[email protected]] DEBUG o.s.o.hibernate3.SessionFactoryUtils - Registering Hibernate Session for deferred close 
13:09:30.524 [[email protected]] DEBUG o.s.o.hibernate3.SessionFactoryUtils - Opening Hibernate Session 
13:09:30.525 [[email protected]] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 13291529705 
13:09:30.534 [[email protected]] DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress 
13:09:30.537 [[email protected]] DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress 
13:09:30.540 [[email protected]] DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress 
13:09:30.543 [[email protected]] DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress 
13:09:30.549 [[email protected]] DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress 
13:09:30.551 [[email protected]] DEBUG o.s.orm.hibernate3.HibernateTemplate - Eagerly flushing Hibernate session 
13:09:30.552 [[email protected]] DEBUG o.h.e.d.AbstractFlushingEventListener - processing flush-time cascades 
13:09:30.554 [[email protected]] DEBUG o.h.e.d.AbstractFlushingEventListener - dirty checking collections 
13:09:30.556 [[email protected]] DEBUG org.hibernate.engine.Collections - Collection found: [<redacted>.UserOrder.dealOrderSet#<delayed:8>], was: [<unreferenced>] (initialized) 
13:09:30.558 [[email protected]] DEBUG o.h.e.d.AbstractFlushingEventListener - Flushed: 5 insertions, 0 updates, 0 deletions to 5 objects 
13:09:30.559 [[email protected]] DEBUG o.h.e.d.AbstractFlushingEventListener - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections 

正如你可以看到,兩個沖洗操作時發生。其中大約一半通過日誌(當我保留),另一個在月底的時候我嘗試保存訂單bean。這是我的數據源配置。

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dealsdbDataSource" /> 
    <property name="packagesToScan" value="<redacted>.common"/> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.archive.autodetection">class</prop> 
      <prop key="hibernate.show_sql">${hibernate-show-sql}</prop> 
      <prop key="hibernate.format_sql">false</prop> 
      <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
      <prop key="hibernate.hbm2ddl.import_files">${hibernate.hbm2ddl.import_files}</prop> 
     </props> 
    </property> 
</bean> 

<bean id="hibernateTemplate" name="hibernateTemplate" 
    class="org.springframework.orm.hibernate3.HibernateTemplate"> 
    <constructor-arg ref="sessionFactory" />    
</bean> 

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

<tx:annotation-driven transaction-manager="transactionManager"/> 

而且經調查,它看起來像我的flushmode是在AUTO的HibernateTemplate。

任何想法??

回答

0

如果您已經在orderDao類中聲明瞭@Transactional,您可以檢查一下嗎? 我想這可能是原因。

最佳做法是在服務層擁有@Transactional,並且不要在DAO層有任何東西。

+0

沒有@Transactional只在 – 2012-03-01 15:00:25