title: Two Phase commit + JTA + JMS + Hibernate
body: <p>I have a requirement where the messages are put into the queue and then a method call is made to update the DB which is 
  done through Hibernate. I want to make this as part of a single transaction, so that if the update call in the db fails
  the JMS roll back and do not put message in the queue. 
  I tried configuring it but looks like i am missing something.
  Any help will be appreciated.</p>
  
  <p>and method where i am calling JMS and DAO is </p>
  
  <pre><code>@Transactional(rollbackFor = Exception.class)               
  public boolean retrieveBatchOrders(BatchPullCriteria batchPullCriteria) throws SQLException, IOException{
  
      List&lt;IntrmStrg&gt; IntrmStrgOrderLst  = iStorageDAO.retrieveByStoreId(batchPullCriteria);
      List&lt;IntrmStrg&gt; IntrmStrgFinalLst  = iStorageDAO.retrieveOrdersByStoreIdLst(batchPullCriteria,IntrmStrgOrderLst);
      sendMsgToQueue.sendMessageToTarget(IntrmStrgFinalLst);
  
      iStorageDAO.updateOrdersStatus(IntrmStrgFinalLst,batchPullCriteria.getRetrievedBy(),BATCHRTRVD);
      return true;
  }
  
  @Transactional
  
  public void updateOrdersStatus(List&lt;IntrmStrg&gt; IntrmStrgLst , String retrivedBy, String status ){
          Date retriveDate = new Date();
          Timestamp retriveDTimestamp = new Timestamp(retriveDate.getTime());
      try{
          for (Iterator&lt;IntrmStrg&gt; 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");    
      }
  
  }
  </code></pre>
  
  <p>application.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
  
     &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
  
     &lt;bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;
  
  
     &lt;/bean&gt;
  
  
  
  
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;
          &lt;property name="driverClassName" value="oracle.jdbc.OracleDriver" /&gt;
          &lt;property name="url"
              value="" /&gt;
          &lt;property name="username" value="" /&gt;
          &lt;property name="password" value="" /&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="iStorageSessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
          &lt;property name="dataSource"&gt;
              &lt;ref local="dataSource" /&gt;
          &lt;/property&gt;
          &lt;property name="mappingDirectoryLocations"&gt;
              &lt;list&gt;
                  &lt;value&gt;classpath:org/entity&lt;/value&gt;
              &lt;/list&gt;
  
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.Oracle10gDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.default_schema"&gt;DEV1&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
  
      &lt;/bean&gt;
  
  
      &lt;bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          lazy-init="true"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
          &lt;!-- -DAO Beans --&gt;
  
      &lt;bean id="storageDAO" class="org.kp.oppr.iStorage.dao.IStorageDAOImpl" &gt;
      &lt;property name="sessionFactory"&gt;
              &lt;ref local="iStorageSessionFactory" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!--  Service Beans  --&gt;
      &lt;bean id="iStorageService" class="org.kp.oppr.iStorage.services.IStorageServiceImpl" &gt;
      &lt;/bean&gt;
  
  
          &lt;!-- BROKER CONNECTION SETTING FOR TARXHB00 --&gt;
      &lt;bean id="jmsFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory"&gt;
          &lt;property name="hostName" value="${mq.conn.hostName}" /&gt;
          &lt;property name="port" value="${mq.conn.hostPort}" /&gt;
          &lt;property name="queueManager" value="${mq.conn.queueManager}" /&gt;
          &lt;property name="channel" value="${mq.conn.channel}" /&gt;
          &lt;property name="transportType" value="${mq.conn.transportType}" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jmsFactorySecurity"
          class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter"&gt;
          &lt;property name="targetConnectionFactory" ref="jmsFactory" /&gt;
          &lt;property name="username" value=" " /&gt;
          &lt;property name="password" value=" " /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jmsQueueSender" class="org.kp.oppr.storage.jms.JmsQueueSender"&gt;
          &lt;property name="connectionFactory" ref="jmsFactorySecurity"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="SendMsgToQueue" class="org.kp.oppr.storage.jms.SendMsgToQueue"&gt;
      &lt;property name="queueNamePrfix" value="${QUEUE_PREFIX}"&gt;&lt;/property&gt;
      &lt;property name="queueNameSufix" value="${QUEUE_SUFFIX}"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="iStorageConfig" class="org.kp.oppr.storage.config.storageConfig"&gt;
          &lt;property name="maxNumberofOrders" value="${MAX_ORDER_COUNT}"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
  
  
  
  &lt;/beans&gt;
  </code></pre>