3 Replies Latest reply on Jan 24, 2009 6:57 AM by Michael Musgrove

    JBoss never commits using UserTransaction

    Mauricio Martinez Newbie

      Hi All,
      I built a simple stand-alone client who invokes an EJB3 stateless bean deployed on JbossAS 4.3.0.GA. The ejb updates de DB. And this client works fine:
      try {
      Context ctx = new InitialContext();
      BeanServiceRemote beanService = (BeanServiceRemote)ctx.lookup("jndi/BeanService");
      beanService.create();


      later I changed the client to:

      try {
      Context ctx = new InitialContext();
      BeanServiceRemote beanService = (BeanServiceRemote)ctx.lookup("jndi/BeanService");
      UserTransaction tx = (UserTransaction) ctx.lookup("UserTransaction");
      tx.begin();
      beanService.create();


      this client runs and finished withouth errors but the updates on the DB are never commited. I know that I can t use tx.commit() from client side because it is a different JVM, but I expected the server commits.

      What is wrong with this client?

        • 1. Re: JBoss never commits using UserTransaction
          Michael Musgrove Master

          The context lookup for UserTransaction actually returns a proxy for the transaction running in the server so you can call tx.commit() on it.

          • 2. Re: JBoss never commits using UserTransaction
            Mauricio Martinez Newbie

             

            "mmusgrov" wrote:
            The context lookup for UserTransaction actually returns a proxy for the transaction running in the server so you can call tx.commit() on it.


            Thanks mmusgrov, that works fine.
            Now I advanced one more step obtaining the datasource by JNDI and updating the database from the client:

            try {
            Context ctx = new InitialContext();
            BeanServiceRemote beanService = (BeanServiceRemote)ctx.lookup("jndi/BeanService");
            UserTransaction tx = (UserTransaction) ctx.lookup("UserTransaction");
            tx.begin();

            DataSource ds = (DataSource) ctx.lookup("my.DataSource");
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement("insert into MyTable (ID, NAME) values (1, 'name')");
            stmt.execute();


            beanService.create();

            tx.commit();

            The problem is "tx.commit();" only commits the ejb updates, not the client updates.
            I tried adding:

            conn.setAutoCommit(false);

            conn.commit();


            and It works but If I commit the connection, then I can 't rollback it, in case the ejb or the tx.commit() fails.
            It seems, the TransactionManager doesnt manage client updates.
            BTW, Im using JbossAS 4.3 default installation, and an Oracle xa-datasource.

            • 3. Re: JBoss never commits using UserTransaction
              Michael Musgrove Master

              Ah that is not a trivial task you are trying to execute. You will need to install the JTS version of the transaction service on both the client and server.

              There is a link on the WIKI that goes through an example of how to do it:

              https://www.jboss.org/community/docs/DOC-13179

              Good look with it.