[jboss-dev-forums] [EJB3 Development] - Re: Distributed transaction

Michael Musgrove do-not-reply at jboss.com
Mon Dec 19 11:31:50 EST 2011


Michael Musgrove [http://community.jboss.org/people/mmusgrov] created the discussion

"Re: Distributed transaction"

To view the discussion, visit: http://community.jboss.org/message/642280#642280

--------------------------------------------------------------
I don't have a ready made example but to get you started you will need something along the following lines:

        <persistence-unit name="pctx1">
                <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
                <properties>
                  <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
                  <property name="hibernate.connection.url" value="jdbc:h2:mem:test1" />
                  <property name="hibernate.connection.username" value="sa" />
                  <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
                  <property name="hibernate.hbm2ddl.auto" value="create" />
                  ....
                </properties>
        </persistence-unit>
        <persistence-unit name="pctx2">
                <jta-data-source>java:jboss/datasources/ExampleDS2</jta-data-source>
                <properties>
                ... etc

in your persistence.xml where you have configured java:jboss/datasources/ExampleDS and java:jboss/datasources/ExampleDS2 as xa datasources in your XML config

followed by 

public class XAService {
   @PersistenceContext(unitName = "pctx1")
   EntityManager em1;
   @PersistenceContext(unitName = "pctx2")
   EntityManager em2;

   public void doXA() {
      UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
      ut.begin();
      em1.persist(new Entity1("E1"));
      em2.persist(new Entity2("E2"));
      ut.commit();
   }
--------------------------------------------------------------

Reply to this message by going to Community
[http://community.jboss.org/message/642280#642280]

Start a new discussion in EJB3 Development at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2030]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-dev-forums/attachments/20111219/5d9d09f3/attachment.html 


More information about the jboss-dev-forums mailing list