-
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
-
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).
-
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>
-
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(...