Hibernate 两阶段提交&x2B;JTA&x2B;JMS+;冬眠

Hibernate 两阶段提交&x2B;JTA&x2B;JMS+;冬眠,hibernate,transactions,jms,jta,Hibernate,Transactions,Jms,Jta,我有一个要求,消息被放入队列中,然后进行方法调用以更新数据库,该数据库是 通过休眠完成。我想将其作为单个事务的一部分,以便在db中的更新调用失败时 JMS回滚,不将消息放入队列。 我试着配置它,但看起来我遗漏了什么。 任何帮助都将不胜感激 我调用JMS和DAO的方法是 @Transactional(rollbackFor = Exception.class) public boolean retrieveBatchOrders(BatchPullCriteria b

我有一个要求,消息被放入队列中,然后进行方法调用以更新数据库,该数据库是 通过休眠完成。我想将其作为单个事务的一部分,以便在db中的更新调用失败时 JMS回滚,不将消息放入队列。 我试着配置它,但看起来我遗漏了什么。 任何帮助都将不胜感激

我调用JMS和DAO的方法是

@Transactional(rollbackFor = Exception.class)               
public boolean retrieveBatchOrders(BatchPullCriteria batchPullCriteria) throws SQLException, IOException{

    List<IntrmStrg> IntrmStrgOrderLst  = iStorageDAO.retrieveByStoreId(batchPullCriteria);
    List<IntrmStrg> IntrmStrgFinalLst  = iStorageDAO.retrieveOrdersByStoreIdLst(batchPullCriteria,IntrmStrgOrderLst);
    sendMsgToQueue.sendMessageToTarget(IntrmStrgFinalLst);

    iStorageDAO.updateOrdersStatus(IntrmStrgFinalLst,batchPullCriteria.getRetrievedBy(),BATCHRTRVD);
    return true;
}

@Transactional

public void updateOrdersStatus(List<IntrmStrg> IntrmStrgLst , String retrivedBy, String status ){
        Date retriveDate = new Date();
        Timestamp retriveDTimestamp = new Timestamp(retriveDate.getTime());
    try{
        for (Iterator<IntrmStrg> iterator = IntrmStrgLst.iterator(); iterator.hasNext();) {
            IntrmStrg intrmStrg = (IntrmStrg) iterator.next();
            intrmStrg.setRtrvdBy(retrivedBy);
            intrmStrg.setRtrvdDt(retriveDTimestamp);
            intrmStrg.setStts(status);
            LOGGER.info("Updating Order Status with ID" + intrmStrg.getIntrmStrgId());
            getHibernateTemplate().getSessionFactory().getCurrentSession().beginTransaction();
            getHibernateTemplate().update(intrmStrg);
            LOGGER.info("Updated Order Status with ID" + intrmStrg.getIntrmStrgId());
            getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().commit();
        }
    }
    catch(HibernateException he)
    {
        getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().rollback();
    }


    if (LOGGER.isDebugEnabled()) {
        LOGGER.info("Order Status Updated");    
    }

}
@Transactional(rollboor=Exception.class)
公共布尔检索BatchOrders(BatchPullCriteria BatchPullCriteria)引发SQLException、IOException{
List IntrmStrgOrderLst=iStorageDAO.retrieveByStoreId(batchPullCriteria);
List IntrmStrgFinalLst=iStorageDAO.retrieveOrdersByStoreIdLst(batchPullCriteria,IntrmStrgOrderLst);
sendMsgToQueue.sendMessageToTarget(IntrmStrgFinalLst);
updateOrderStatus(IntrmStrgFinalLst,batchPullCriteria.getRetrievedBy(),BATCHRTRVD);
返回true;
}
@交易的
public void updateOrderStatus(列表IntrmStrgLst、字符串检索者、字符串状态){
日期检索日期=新日期();
Timestamp RetrievedTimestamp=新的时间戳(retrieveDate.getTime());
试一试{
for(Iterator Iterator=IntrmStrgLst.Iterator();Iterator.hasNext();){
IntrmStrg IntrmStrg=(IntrmStrg)迭代器;
intrmStrg.setRtrvdBy(检索者);
intrmStrg.setRtrvdDt(检索时间戳);
intrmStrg.setStts(状态);
info(“使用ID“+intrmStrg.getIntrmStrgId()”更新订单状态);
getHibernateTemplate().getSessionFactory().getCurrentSession().beginTransaction();
getHibernateTemplate().update(intrmStrg);
info(“ID为“+intrmStrg.getIntrmStrgId()”的更新订单状态);
getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().commit();
}
}
捕获(冬眠异常he)
{
getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().rollback();
}
if(LOGGER.isDebugEnabled()){
LOGGER.info(“订单状态更新”);
}
}
application.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    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-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


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


   <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">


   </bean>





    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url"
            value="" />
        <property name="username" value="" />
        <property name="password" value="" />
    </bean>


    <bean id="iStorageSessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:org/entity</value>
            </list>

        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.default_schema">DEV1</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>

    </bean>


    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        lazy-init="true">
        <property name="dataSource" ref="dataSource" />
    </bean>

        <!-- -DAO Beans -->

    <bean id="storageDAO" class="org.kp.oppr.iStorage.dao.IStorageDAOImpl" >
    <property name="sessionFactory">
            <ref local="iStorageSessionFactory" />
        </property>
    </bean>

    <!--  Service Beans  -->
    <bean id="iStorageService" class="org.kp.oppr.iStorage.services.IStorageServiceImpl" >
    </bean>


        <!-- BROKER CONNECTION SETTING FOR TARXHB00 -->
    <bean id="jmsFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
        <property name="hostName" value="${mq.conn.hostName}" />
        <property name="port" value="${mq.conn.hostPort}" />
        <property name="queueManager" value="${mq.conn.queueManager}" />
        <property name="channel" value="${mq.conn.channel}" />
        <property name="transportType" value="${mq.conn.transportType}" />
    </bean>

    <bean id="jmsFactorySecurity"
        class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
        <property name="targetConnectionFactory" ref="jmsFactory" />
        <property name="username" value=" " />
        <property name="password" value=" " />
    </bean>

    <bean id="jmsQueueSender" class="org.kp.oppr.storage.jms.JmsQueueSender">
        <property name="connectionFactory" ref="jmsFactorySecurity"></property>
    </bean>


    <bean id="SendMsgToQueue" class="org.kp.oppr.storage.jms.SendMsgToQueue">
    <property name="queueNamePrfix" value="${QUEUE_PREFIX}"></property>
    <property name="queueNameSufix" value="${QUEUE_SUFFIX}"></property>
    </bean>

    <bean id="iStorageConfig" class="org.kp.oppr.storage.config.storageConfig">
        <property name="maxNumberofOrders" value="${MAX_ORDER_COUNT}"></property>
    </bean>




</beans>

类路径:组织/实体
org.hibernate.dialen.oracle10galent
DEV1
真的

主要问题是您使用的事务管理器未激活

您将应用程序部署到哪个容器?Spring提供了与市场中的主要容器一起工作的事务管理器


如果它是一个独立的应用程序,或者它运行在不支持JTA的容器上,您可能希望在应用程序中使用其他事务管理器,如Bitronix或Atomikos

为什么不在成功更新数据库后将消息放入队列?不,我必须先放入消息,然后转到数据库更新数据,如果成功,则提交整个事务。如果DB出现故障,那么我需要滚动消息。它在web sphere中运行,问题是我不完全了解支持该功能所需的配置。在Spring应用程序上下文中只保留一个事务管理器,并使用org.springframework.transaction.jta.WebSphereUowTransactionManager作为事务管理器的类。使用“hibernate spring websphere”访问Google,您应该能够找到大量关于如何设置的信息