10 Replies Latest reply on Sep 17, 2012 8:02 AM by paulohbmelo

    Transaction Propagation with JBoss - Case 4

    paulohbmelo

      Hi folks!

       

      Here in my job, I'm with a case that have 2 web java applications needing to make transactional (atomic and real time) integration. Soon I thought in distributed EJB transactions. I started researching and found the following article: https://community.jboss.org/wiki/TransactionPropagationWithJBoss that, by the way it's very good but, unfortunately, the case that I need don't was exemplified. The case 4!

      In the article, the author say that "the case 4 is covered at length in the JTS programmers guide" but, the samples in the guide not yet clear to me.

       

      In my case, there's an app1 that lookup a connection to make some atomic operations and need to make some operations in app2 too but, all the operations may be in the same transaction. Soon we created an EJB session bean  for the app2 and we configured the same (like change the deployment descriptor to support calls by IIOP transport).

       

      The app2 use Spring and we used the SpringBeanAutowiringInterceptor to integrate with EJB 3 and it's running very well but, I don't know how (and where to make the "bind") to integrate the local connection in app1 with the EJB connection in the same transaction.

       

      The server code is like this:

       

      @Stateless

      @Remote(TestService.class)

      @RemoteBinding(jndiBinding="TestServiceEJB")

      @Interceptors(SpringBeanAutowiringInterceptor.class)

      public class TestServiceEJB implements TestService {

         

          @Autowired

          private TestBusiness testBusiness;

       

          @Override

          public void includeTest(TestTO testTO) throws RemoteException {

              try {

                  Test test = new Test();

                 

                  test.setId(testTO.getId());

                  test.setDesc(testTO.getDesc());

                  test.setSigl(testTO.getSigl());

                  testBusiness.incluir(test);

              } catch (Exception ex) {

                  ex.printStackTrace();

                  throw new RemoteException(ex.getMessage());

              }

          }   

      }

       

      And the client source is like this:

       

      ...

      try {

                  InitialContext ctxLocal = new InitialContext();

                  ctxRemoto = getRemoteContext(); //context to lookup EJB with IIOP support

       

                  orb = new ORBWrapper(); // like in the article

                  orb.start();

       

                  OTSManager.get_current().begin();

       

                  DataSource dataSource = (DataSource) ctxLocal.lookup("jdbc/test-xa");

                  Connection connection = dataSource.getConnection();

                  connection.setAutoCommit(false);

       

                  Statement statement = connection.createStatement();

                  statement.executeUpdate("INSERT INTO TEST (TEST_DESC, TEST_SIGL, TEST_STAT) VALUES ('TESTE_APP1', 'TST', 'A')");

                  statement.close();

       

                  TestService testService = (TestService) ctxRemoto.lookup("TestServiceEJB");

                  TestTO testTO = new TestTO();

                  testTO.setDesc("TEST_APP2");

                  testTO.setSigl("TT");

       

                  testService.includeTest(testTO);

       

                  OTSManager.get_current().commit(true);

                  orb.stop();

              } catch (Exception ex) { //many catch's omitted to simplify.

                  rollbackTransaction();

                  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

              }

      ...

       

      Then I do the question: where (is) put the integration/bind between the connections (app1 local and EJB).

       

      BTW, I'm using JBoss 4.2.3 with EJB3 in app2, Spring 3.1.1 and JPA 1.0 (Hibernate 3) in app2, Java 6 in both and "pure" JDBC in app1.

       

      Sorry for my English and for don't have put the code in quotes (where is? ou how to do?)

       

      Thank you!

        • 1. Re: Transaction Propagation with JBoss - Case 4
          tomjenkinson

          Is it possible to change the code to be more like case 1?

           

          i.e. Wrap the transactional work to be done in app1 in an EJB method and get the client to call this, then call the EJB in app2?

           

          Tom

          • 2. Re: Transaction Propagation with JBoss - Case 4
            paulohbmelo

            Hi Tom, thank you for the response!

             

            It's a good "workaround" but, today the business logic (that is very complex and that will call the remote methods) are in many business objects (in app1). i.e. I would have to transform largely of business loginc in app1 to EJB. Would require much change in app1.

             

            The case 4 would be the best approach however, seems much more complex and less clean. Do you already used the Case 4?

             

            Again, thank you!

            • 3. Re: Transaction Propagation with JBoss - Case 4
              tomjenkinson

              No problem, but I do think my idea could be easier to implement by placing a simple EJB adapter which can perform the transaction wrapping.

               

              So lets say you have all these business logic components which you are now trying to coordinate into a single transaction, I would just add an EJB that does this in front of the existing code.

               

              If this is not going to work, we can wait for Mike (the author of the document you quoted) to come back on line to point out the parts of the documentation suite that are applicable to "case 4".

               

              Tom

              • 4. Re: Transaction Propagation with JBoss - Case 4
                paulohbmelo

                In your idea you don't pass the business logic to the app1 EJBs? Would use the same like a Delegate?

                 

                Coz, by what is written in the article:

                 

                The ClientUserTransactionService MBean publishes a

                UserTransaction implementation under the JNDIUserTransaction. When the

                UserTransaction is obtained with a JNDI lookup from a external client,

                a very simple UserTransaction suitable for thin clients is returned.

                This UserTransaction implementation only controls the transactions on

                the server the UserTransaction object was obtained from. Local

                transactional work done in the client is not done within the

                transactions started by this UserTransaction object.

                 

                i.e. I would have to control manually the transactions (local and remote from app2) in app1. That could bring me many problems. If I understand correctly, I don't need an extra EJB (like a Delegate) to make this.

                 

                Again, thank you very much!!

                • 5. Re: Transaction Propagation with JBoss - Case 4
                  tomjenkinson

                  Hi Paulo,

                   

                  What I am thinking is:

                   

                  0. Client using BMT or CMT

                  begin transaction

                  all it does is call new EJB in app1

                  complete transaction

                   

                  1. new EJB in app1 (TransactionAttribute.REQUIRED)

                  Wrapper to put stuff in your database, call your existing business logic pojos, call app2

                   

                  2. EJB in app2 (TransactionAttribute.REQUIRED)

                  same again

                   

                  Hope that is clearer,

                  Tom

                  • 6. Re: Transaction Propagation with JBoss - Case 4
                    paulohbmelo

                    Hi Tom, thank you!!

                     

                    I understand you! But, how I said above, this will demand to many refactoring in app1 (that is legacy, fully Transactions Script). So, the Case 4 is the better approach.

                     

                    Thank you very much!

                    • 7. Re: Transaction Propagation with JBoss - Case 4
                      tomjenkinson

                      The thing is, whatever you do you are going to have to heavily refactor I think.

                       

                      For example, in your proposed approach (client) you will have to register recovery modules so that if app1 crashes, the database updates can be completed even in the presence of the failure.

                       

                      Also, you need to make sure you are using JTS on both sides as that is required for transaction propagation.

                      • 8. Re: Transaction Propagation with JBoss - Case 4
                        paulohbmelo

                        Hi Tom!

                         

                        At least the transactional code of app1 is isolated and, my idea is (to try) to abstract (at maximum) these treatments. At least in my mind. What do you think?

                         

                        Thank you!

                        • 9. Re: Transaction Propagation with JBoss - Case 4
                          tomjenkinson

                          Hi Paulo,

                           

                          I would suggest looking up the datasource in JCA on app 1 to ensure it is properly enlisted with the transaction. Unfortunately I am not altogether certain how to do that for JBoss AS4 sorry! You will also need to make sure that the datasource has been properly configured for recovery.


                          Also, you will definitely need to have JTS configured, again I would check the manuals for that as this version of JBoss Transactions is somewhat before my time.

                           

                          Assuming all of those statements are satisfied, I don't see a problem with what you are proposing.

                           

                          Hopefully Mike (as the original author of the doc) will have a suggestion. To get his attention you could put a link to this discussion on the document you linked to?

                           

                          Good luck!

                          Tom

                          • 10. Re: Transaction Propagation with JBoss - Case 4
                            paulohbmelo

                            Hi Tom, thank you!!

                             

                            With JTA I can to control the transaction with UserTransaction (including multiple connections) but don't remotely. In other words, I would have one local transaction (for app1) and a UserTransaction to control the EJB transaction (for app2).

                             

                            I posted the link on the comments of the document.

                             

                            Thank you very much!