Distributed tx involving DB and JMS session
asookazian Aug 10, 2009 5:29 PMPlease reference this thread:
http://www.seamframework.org/Community/NewSeamExampleFeaturingDistributedTransactions
Using a EJB 3.0 SFSB with JSF 1.2 and Seam 2.1.2.GA running in JBoss AS 4.2.3.GA.
Need to understand why the tx mgr is (apparently?) not enlisting the JMS session as part of a distributed tx with the other resource mgr managing a DB (via EntityManager interface).
I am using <local-tx-datasource> to test this out in my *-ds.xml:
<datasources> <local-tx-datasource> <jndi-name>bookingDatasource</jndi-name> <connection-url>jdbc:mysql://localhost:3306/jboss</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>jboss</user-name> <password>password</password> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> <local-tx-datasource> <jndi-name>bookingDatasource2</jndi-name> <connection-url>jdbc:mysql://localhost:3306/jboss2</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>jboss</user-name> <password>password</password> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> </datasources>
Here's some relevant code snippets:
@Name("startConversation") @AutoCreate @Install(true) @Stateful public class TestStartConversation implements TestStartConversationLocal{ @Logger private Log log; @In private EntityManager em; private transient TopicPublisher topicPublisher; private transient TopicSession topicSession; private transient TopicConnection conn; private transient Topic topic; @Begin(join=true, flushMode=FlushModeType.MANUAL) public void startup(){ log.info("in startup()"); List<Hotel> list = em.createQuery("from Hotel").getResultList(); log.info("list.size() = "+list.size()); Hotel hotel = null; if (list != null && list.size() > 0) { hotel = list.get(0); hotel.setAddress("arbi's house1"); } em.flush(); try{ InitialContext iniCtx = new InitialContext(); Object tmp = iniCtx.lookup("ConnectionFactory"); TopicConnectionFactory tcf = (TopicConnectionFactory) tmp; conn = tcf.createTopicConnection(); topic = (Topic) iniCtx.lookup("topic/chatroomTopic"); topicSession = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE); topicPublisher = topicSession.createPublisher(topic); topicPublisher.publish( topicSession.createObjectMessage(new ChatroomEvent("connect", "arbime")) ); } catch(Exception e){ //noop } } @Destroy @Remove public void destroy(){ log.info("in destroy()"); } }
Please note that the distributed tx semantics are behaving as expected when I read from two different DBs in the startup() method above using two different EntityManager instances (i.e. I get tx enlist exceptions when I use local-tx-datasource instead of xa-datasource in *-ds.xml). And of course, the tx attribute type by default for the startup() method is REQUIRED as per JSR220.
thx.