2012-10-31 50 views
0

我有哪裏的消息被放入隊列,然後一個方法調用,以更新DB這是 通過Hibernate做了要求。我想使這個作爲一個交易的一部分,因此,如果在數據庫中更新調用失敗 的JMS回滾和隊列中不要放消息。 我試過配置它,但看起來像我失去了一些東西。 任何幫助將不勝感激。兩階段提交+ JTA + JMS + Hibernate的

和方法,其中我打電話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");  
    } 

} 

的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> 
+0

你爲什麼不乾脆把消息傳遞到隊列_after_成功的數據庫更新? – muratgu

+0

不行,我得先放消息,然後去數據庫更新數據,如果成功,則提交整個事務。如果有數據庫故障,那麼我需要滾動消息。 – Guest

回答

0

的主要問題是您正在使用不XA awared事務管理。

什麼容器中您將應用程序部署到? Spring提供交易經理與市場上的主要容器一起工作。

如果它是一個獨立的應用程序,或者是在不支持JTA容器運行,您可能需要使用一些其他的事務管理器在您的應用程序,像Bitronix或Atomikos公司

+0

它在網絡領域的運行,關鍵是我沒有這是需要支持的配置的完整理解。 – Guest

+0

在Spring應用上下文中只保留一個事務管理器,並使用org.springframework.transaction.jta.WebSphereUowTransactionManager作爲事務管理器的類。使用「hibernate spring websphere」來使用Google,您應該能夠找到大量關於如何設置的信息 –