Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HibernateTemplate、批量更新和刷新模式_Java_Hibernate_Spring_Jakarta Ee_Transactions - Fatal编程技术网

Java HibernateTemplate、批量更新和刷新模式

Java HibernateTemplate、批量更新和刷新模式,java,hibernate,spring,jakarta-ee,transactions,Java,Hibernate,Spring,Jakarta Ee,Transactions,在我当前的项目中,我们正在为dao使用hibernateTemplate。我的情况是,如果事务中的任何一个操作失败,我希望有一个事务回滚(不是rocket science,对吧?)。。因此,基本上,方法如下: // Reserve a deal and add the 'pending order' all in one transaction @Transactional(propagation = Propagation.REQUIRES_NEW) private MyUserOrd

在我当前的项目中,我们正在为dao使用hibernateTemplate。我的情况是,如果事务中的任何一个操作失败,我希望有一个事务回滚(不是rocket science,对吧?)。。因此,基本上,方法如下:

    // 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;
}
//在一次交易中保留交易并添加“待定订单”
@事务性(传播=传播。需要\u新建)
私有MyUserOrder reserveQuantity(MyUserOrder userOrder,Date UpdateOn)引发MyAPIException{
setOrderStatus(OrderStatus.PENDING);
int orderReserved=orderDao.reserveQuantity(userOrder);
//由于某种原因,hibernate正在刷新上面的行???
如果(交易已送达<1)
抛出新的MyAPIException(ExceptionCode.数量不足);
userOrder=userOrderDao.save(userOrder);
//冬眠再次刷新!
返回用户订单;
}
所以,基本上我们从数据库订单可用性表中保留X个订单。。。然后将挂起的订单保存到数据库中。我希望两条语句都在同一事务中执行,因此如果其中一条语句失败,它们都会回滚。不幸的是,它看起来像是立即提交了.reserveQuantity的语句,即使整个方法应该在它自己的事务中运行

这是日志

    13:09:28.007 [5253145@qtp-25442933-1] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 13291529680
13:09:28.008 [5253145@qtp-25442933-1] DEBUG o.s.o.hibernate3.SessionFactoryUtils - Registering Spring transaction synchronization for new Hibernate Session
13:09:28.009 [5253145@qtp-25442933-1] DEBUG o.s.orm.hibernate3.HibernateTemplate - Found thread-bound Session for HibernateTemplate
13:09:28.011 [5253145@qtp-25442933-1] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
13:09:28.011 [5253145@qtp-25442933-1] DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
13:09:28.012 [5253145@qtp-25442933-1] DEBUG o.s.j.d.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:db2://172.26.10.144:60000/devdb]
13:09:28.189 [5253145@qtp-25442933-1] DEBUG org.hibernate.SQL - update deal set remaining_quantity=remaining_quantity-?, updated_on=?, updated_by=? where id=? and remaining_quantity>=?
13:09:28.242 [5253145@qtp-25442933-1] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
13:09:28.243 [5253145@qtp-25442933-1] 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 [5253145@qtp-25442933-1] DEBUG o.s.orm.hibernate3.HibernateTemplate - Not closing pre-bound Hibernate Session after HibernateTemplate
13:09:28.245 [5253145@qtp-25442933-1] DEBUG o.s.o.hibernate3.SessionFactoryUtils - Flushing Hibernate Session on transaction synchronization
13:09:28.246 [5253145@qtp-25442933-1] DEBUG o.s.o.hibernate3.SessionFactoryUtils - Registering Hibernate Session for deferred close
13:09:30.524 [5253145@qtp-25442933-1] DEBUG o.s.o.hibernate3.SessionFactoryUtils - Opening Hibernate Session
13:09:30.525 [5253145@qtp-25442933-1] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 13291529705
13:09:30.534 [5253145@qtp-25442933-1] DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress
13:09:30.537 [5253145@qtp-25442933-1] DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress
13:09:30.540 [5253145@qtp-25442933-1] DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress
13:09:30.543 [5253145@qtp-25442933-1] DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress
13:09:30.549 [5253145@qtp-25442933-1] DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress
13:09:30.551 [5253145@qtp-25442933-1] DEBUG o.s.orm.hibernate3.HibernateTemplate - Eagerly flushing Hibernate session
13:09:30.552 [5253145@qtp-25442933-1] DEBUG o.h.e.d.AbstractFlushingEventListener - processing flush-time cascades
13:09:30.554 [5253145@qtp-25442933-1] DEBUG o.h.e.d.AbstractFlushingEventListener - dirty checking collections
13:09:30.556 [5253145@qtp-25442933-1] DEBUG org.hibernate.engine.Collections - Collection found: [<redacted>.UserOrder.dealOrderSet#<delayed:8>], was: [<unreferenced>] (initialized)
13:09:30.558 [5253145@qtp-25442933-1] DEBUG o.h.e.d.AbstractFlushingEventListener - Flushed: 5 insertions, 0 updates, 0 deletions to 5 objects
13:09:30.559 [5253145@qtp-25442933-1] DEBUG o.h.e.d.AbstractFlushingEventListener - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
13:09:28.007[5253145@qtp-25442933-1]DEBUG org.hibernate.impl.SessionImpl-在时间戳13291529680处打开会话
13:09:28.008 [5253145@qtp-25442933-1]调试o.s.o.hibernate3.SessionFactoryUtils-为新Hibernate会话注册Spring事务同步
13:09:28.009 [5253145@qtp-25442933-1]调试o.s.orm.hibernate3.HibernateTemplate-找到HibernateTemplate的线程绑定会话
13:09:28.011 [5253145@qtp-25442933-1]DEBUG org.hibernate.jdbc.AbstractBatcher-即将打开PreparedStatement(打开PreparedStatements:0,全局:0)
13:09:28.011 [5253145@qtp-25442933-1]调试org.hibernate.jdbc.ConnectionManager-打开jdbc连接
13:09:28.012 [5253145@qtp-25442933-1]调试o.s.j.d.DriverManager数据源-创建到[JDBC:db2://172.26.10.144:60000/devdb]的新JDBC DriverManager连接
13:09:28.189 [5253145@qtp-25442933-1]DEBUG org.hibernate.SQL-更新交易集剩余数量=剩余数量-?,更新时间=?,更新时间=?其中id=?剩余数量>=?
13:09:28.242 [5253145@qtp-25442933-1]DEBUG org.hibernate.jdbc.AbstractBatcher-即将关闭PreparedStatement(打开PreparedStatements:1,全局:1)
13:09:28.243 [5253145@qtp-25442933-1]DEBUG org.hibernate.jdbc.ConnectionManager-使用on_关闭连接释放模式在会话上完成事务;确保关闭会话以释放JDBC资源!
13:09:28.244 [5253145@qtp-25442933-1]调试o.s.orm.hibernate3.HibernateTemplate-在HibernateTemplate之后不关闭预先绑定的Hibernate会话
13:09:28.245 [5253145@qtp-25442933-1]调试o.s.o.hibernate3.SessionFactoryUtils-刷新事务同步上的Hibernate会话
13:09:28.246 [5253145@qtp-25442933-1]调试o.s.o.hibernate3.SessionFactoryUtils-为延迟关闭注册Hibernate会话
13:09:30.524 [5253145@qtp-25442933-1]调试o.s.o.hibernate3.SessionFactoryUtils-打开Hibernate会话
13:09:30.525 [5253145@qtp-25442933-1]DEBUG org.hibernate.impl.SessionImpl-时间戳为13291529705的已打开会话
13:09:30.534 [5253145@qtp-25442933-1]调试o.h.e.def.AbstractSaveEventListener-由于没有正在进行的事务而延迟标识插入
13:09:30.537 [5253145@qtp-25442933-1]调试o.h.e.def.AbstractSaveEventListener-由于没有正在进行的事务而延迟标识插入
13:09:30.540 [5253145@qtp-25442933-1]调试o.h.e.def.AbstractSaveEventListener-由于没有正在进行的事务而延迟标识插入
13:09:30.543 [5253145@qtp-25442933-1]调试o.h.e.def.AbstractSaveEventListener-由于没有正在进行的事务而延迟标识插入
13:09:30.549 [5253145@qtp-25442933-1]调试o.h.e.def.AbstractSaveEventListener-由于没有正在进行的事务而延迟标识插入
13:09:30.551 [5253145@qtp-25442933-1]调试o.s.orm.hibernate3.HibernateTemplate-急切地刷新Hibernate会话
13:09:30.552 [5253145@qtp-25442933-1]调试o.h.e.d.AbstractFlushingEventListener-处理刷新时间级联
13:09:30.554 [5253145@qtp-25442933-1]调试o.h.e.d.AbstractFlushingEventListener-脏检查集合
13:09:30.556 [5253145@qtp-25442933-1]调试org.hibernate.engine.Collections-找到的集合:[.UserOrder.dealOrderSet#],was:[](已初始化)
13:09:30.558 [5253145@qtp-25442933-1]调试o.h.e.d.AbstractFlushingEventListener-刷新:对5个对象进行5次插入、0次更新、0次删除
13:09:30.559 [5253145@qtp-25442933-1]调试o.h.e.d.AbstractFlushingEventListener-刷新:1(重新)创建,0次更新,0次删除到1个集合
如您所见,发生了两次刷新操作。。一个在日志的一半(当我保留时),另一个在最后,当我试图保存orderbean时。这是我的数据源配置

    <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"/>

班
${hibernate show sql}
假的
${hibernate.dial}
${hibernate.hbm2ddl.auto}
${hibernate.hbm2ddl.import_files}
经过调查,我的flushmode在hibernateTemplate中似乎是自动的


有什么想法吗???

如果您在orderDao类中声明了@Transactional,请检查一下。 我想这可能是原因


最佳实践是在服务层使用@Transactional,而在DAO层不使用任何@Transactional。

No@Transactional仅在