Results 1 to 4 of 4

Thread: jsm transaction

  1. #1
    Join Date
    May 2011
    Posts
    7

    Default jsm transaction

    Hello,
    I am using declarative mq jms transaction. I put message on the queue and throw an exception. I expect message to rollback but it does not rollback.

    Why my message does not get rolled back. What am I missing?

    <bean id="txManager" class="org.springframework.jms.connection.JmsTrans actionManager102">
    <property name="connectionFactory">
    <ref bean="jmsSingleQueueConnectionFactory" />
    </property>
    <property name="pubSubDomain">
    <value>false</value>
    </property>
    </bean>
    <tx:annotation-driven transaction-manager="txManager"/>

    @Transactional(rollbackFor=Exception.class)
    public void executeTransactionalMethod() throws Exception {
    jmsTemplate.send(new MessageCreator() {
    public Message createMessage(Session session) throws JMSException {
    Message m = session.createTextMessage("Hello world1");
    return m;
    }
    });
    if (true) throw new Exception("error");
    System.out.println("done ");
    }

    thanks
    Last edited by bmakhlin; Feb 28th, 2012 at 04:02 PM.

  2. #2
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,033

    Default

    Please use [ code]...[/code ] tags (no spaces) around code and config.

    I was going to point out your <tx:annotation-driven/> element points to a different transaction manager, but I see you edited that.

    How are you calling execute? For this to work, the class that contains the execute() method must be declared as a bean in the context, and invoked via the context either via injection, or getBean().

    Spring transactions wrap the bean in a proxy; that's how the behavior is added; you can't call an @Transactional method directly from within the class (well, you can, but it won't get a transaction).
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  3. #3
    Join Date
    May 2011
    Posts
    7

    Default

    Thanks your your help.

    Here is a bean class I am using to test jms transaction and a bean declaration. Please, point out what am I missing. thanks

    Code:
    public class MQBackEnd2 {
    	private JmsTemplate102 jmsTemplate;
    
    	public JmsTemplate102 getJmsTemplate() {
    		return jmsTemplate;
    	}
    
    	public void setJmsTemplate(JmsTemplate102 jmsTemplate) {
    		this.jmsTemplate = jmsTemplate;
    	}
    	
    
    	public void send(final String message, final String headerName, final String headerValue) {	    
    	    jmsTemplate.send(new MessageCreator() {
                public Message createMessage(Session session) throws JMSException {
                  Message m = session.createTextMessage(message);
                  m.setStringProperty(headerName, headerValue);
                  return m;
                }
            });		
    	}
    	
            @Transactional(rollbackFor=Exception.class)
            public void executeTransactionalMethod() throws Exception {
    	    jmsTemplate.send(new MessageCreator() {
                public Message createMessage(Session session) throws JMSException {
                  Message m = session.createTextMessage("Hello world1");
                  return m;
                }
            });		
    	    if (true) throw new Exception("error");
    	    System.out.println("done ");
    	}
    
    	public static void main(String[] args) throws Exception {
    		ClassPathResource res = new ClassPathResource("queueConfig.xml");
    	    XmlBeanFactory factory = new XmlBeanFactory(res);  
    	    final MQBackEnd2 mqBackEnd = MQBackEnd2)factory.getBean("mqBackEnd2");
    	    
    	    mqBackEnd.executeTransactionalMethod();
    	    System.out.println("done ");
    
    	}
    }
    
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate102">
            <property name="connectionFactory">
                <ref bean="jmsSingleQueueConnectionFactory"/>
             </property>
             <property name="defaultDestination">
          	<ref bean="destination"/>
             </property>
    
            <property name="pubSubDomain">
                <value>false</value>
            </property>
            <property name="receiveTimeout">
                <value>20000</value>
            </property>
            <property name="sessionTransacted">
                <value>true</value>
            </property>
            
            <property name="sessionAcknowledgeMode">
    	 <value>2</value>
            </property>
        </bean>
        
    
        <bean id="mqBackEnd2" class="com.thomson.ecom.core.client.queue.MQBackEnd2">
            <property name="jmsTemplate">
                <ref bean="jmsQueueTemplate"/>
             </property>
    	
        </bean>

  4. #4
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,033

    Default

    Declarative @Transactions (and all Spring AOP behavior) only work with ApplicationContexts, not raw BeanFactories; you need

    Code:
    ApplicationContext ctx = new ClassPathXMLApplicationContext("queueConfig.xml");
    ...ctx.getBean(...
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •