3 Replies Latest reply on Sep 9, 2008 9:59 AM by rohit.macherla

    Transactions in JBoss

    rohit.macherla

      I am using a JBoss 4.2.2.GA server using WebServices. In my WebService, I am required to connect to a relational Database (Oracle 9i), get some data, change some of the existing data in the Database and then invoke another WebService. A failure to do any of these things implies that the entire thing should be rolled back i.e., this whole thing is one atomic transaction.

      Since I am a newbie to the server management, I have used JDBC to connect to a database using :

      Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
       connection=DriverManager.getConnection("jdbc:oracle:thin:@STD43S28:1521:cdot1","user","pwd");
      

      and I am 'COMMIT'ing the transaction only when I get a successful response from the WebService I invoke.
      This approach works but I would like to delegate the responsibility of getting a connection to the JBoss server and also would like to know if my transaction can be demarcated by using any JBoss specific feature (like JCA or something) or actually any server level thing that I have to activate.

      I've just found the use of Connection pooling and started to read about the JCA but was confused when I read that
      JCA is a resource manager integration API whose goal is to standardize access to non-relational resources

      Please inform me of what all I need to know i.e., if JCA will actually help me out in my quest or is there something else, some MBean that I need to know about. It would be great if you can suggest a possible framework for my WebService.

      Thank you.

        • 1. Re: Transactions in JBoss

          Hello,
          oh no, don't use Class.forName. Use JBoss Connection pool at first

          Context ctx = new InitialContext();
           javax.sql.DataSource ds
           = (javax.sql.DataSource) ctx.lookup ("myJtsDataSource");
           java.sql.Connection conn = ds.getConnection();


          You have to register a Datasource and place it in the deploy directory of jBoss. Of course you could write your own Conn Pool and delegate transaction management to the RDBMS but it's lots of work, and then why using an Application Server if you don't use its services ?

          Then from your Web Service you can start your own transactions yet I would advice you if you have experience at EJB, handle your business logic in an EJB either BMT or CMT. With EJB 3.0 it's pretty easier, no configuration files are needed to handle transactions.

          • 2. Re: Transactions in JBoss
            jhalliday

            > This approach works

            .. but only as long as your application can tolerate invocations of the web service without the corresponding database updates. The server may crash after invoking the web service but before committing the database updates. Or the commit on the database may fail.

            • 3. Re: Transactions in JBoss
              rohit.macherla

              @francis17101970
              Thanks for the information. We have put a jboss-ds.xml file in the Deploy structure of the JBoss. But the datasource is mentioned as

              <local-tx-datasource>
              
              Hope this doesn't destroy the purpose there are some XA datasources available as well (not in my jboss-ds.xml, but I found in the internet that XA datasources can also be configured) and I am unaware of their purpose.
              We'll also check if we can shift to EJB from the Servlets that we are using. But just one query here, in case we shift to EJB, does that mean that JCA need not be involved in my development ? I mean, because we'd be using CMP beans and as they'll take care of some part of the transactions, should I still use JCA somewhere because I've read that it deals with non-relational resources too?

              @jhalliday
              I've understood the problem you are referring to. One generic query here : Using EJB's here will not solve this problem, isnt' it ? I mean, if the server crashes even during the process or COMMITing, the method commit() should return an appropriate value I guess. Thanks.