0

我有一个要求,将消息放入队列,然后进行方法调用以更新通过 Hibernate 完成的数据库。我想将此作为单个事务的一部分,以便如果数据库中的更新调用失败,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");    
    }

}

应用程序.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>
4

1 回答 1

0

主要问题是您使用的事务管理器不支持 XA。

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

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

于 2012-10-31T02:21:32.433 回答