5 Replies Latest reply on Oct 31, 2002 4:28 AM by vickyk

    Transaction Problem!!!

    vickyk

      Hi,
      This has alreasy been posted in other section but did not get any response there so I am putting it here now.
      I hava a Jsp where I wanted the JTA,to handle the Transaction.I have created the XADataSource and I am Calling the executeUpdate after starting the Transaction as shown below:

      <%@ page import="javax.naming.*,javax.sql.*,java.sql.*,project.session.login.*,javax.transaction.*" %>
      <%
      Context ocontext=new InitialContext();
      //DataSource ods=(DataSource)ocontext.lookup("java:/XAOracleDS");
      DataSource ods=(DataSource)ocontext.lookup("java:/OracleDSI");
      System.out.println("Data Source is "+ods);
      Connection ocon=ods.getConnection();
      ocon.setAutoCommit(true);
      PreparedStatement ostmt=ocon.prepareStatement("update gangsterejb set name=? where id = ? ");
      ostmt.setString(1,"VK1");
      ostmt.setString(2,"3");
      //ostmt.executeUpdate();
      System.out.println("Before getting UserTransaction");
      UserTransaction trans=(UserTransaction)ocontext.lookup("UserTransaction");
      //ResultSet ors=ostmt.executeQuery("select * from tab");
      int i=10;
      trans.setTransactionTimeout(10);
      System.out.println("After Timeout");
      out.println("Value Of i before begin:"+i+"");
      trans.begin();
      System.out.println("After Begin");
      //trans.setRollbackOnly();
      System.out.println("STATUS_ACTIVE "+Status.STATUS_ACTIVE);
      System.out.println("Get Status "+trans.getStatus());
      //ostmt.executeUpdate("update gangsterejb set name='1' where id = 3 ");
      System.out.println("After update");
      i++;
      //trans.commit();
      System.out.println("Before Commit "+ocon.getAutoCommit());
      //trans.rollback();
      ostmt.executeUpdate();
      //trans.commit();
      System.out.println("After Commit");
      ocon.close();
      out.println("Value Of i:"+i+"");
      out.println(trans+"");

      %>



      The Output which I get from this follows

      17:37:18,464 INFO [STDOUT] Data Source is org.jboss.resource.adapter.jdbc.local
      .LocalDataSource@47098a
      17:37:18,995 INFO [STDOUT] Before getting UserTransaction
      17:37:18,995 INFO [STDOUT] After Timeout
      17:37:19,005 INFO [STDOUT] After Begin
      17:37:19,005 INFO [STDOUT] STATUS_ACTIVE 0
      17:37:19,005 INFO [STDOUT] Get Status 0
      17:37:19,005 INFO [STDOUT] After update
      17:37:19,005 INFO [STDOUT] Before Commit true
      17:37:29,111 WARN [TxCapsule] Transaction XidImpl [FormatId=257, GlobalId=VIDYA
      DHAR-D//2, BranchQual=] timed out. status=STATUS_ACTIVE


      The "After Commit" statement does not get displayed on the console.I have tried various options and found if I comment ostmt.executeUpdate(); the code goes to completion.I am not able to solve the problem /I would appericiate if you could try this.
      regards
      Vicky

        • 1. Re: Transaction Problem!!!

          Try removing the commented out lines it would be
          easier to follow. :-(

          Your code does the following.

          1) Gets a an Oracle connection from the pool
          2) Sets auto-commit on the connection
          3) Creates and begins a user transaction
          4) Runs an SQL update

          The connection is not enlisted in the transaction
          because you haven't begun it yet.
          Simple ordering problem.

          Second setting auto-commit on an XA transaction is bad.
          I don't know what Oracles does with this, but XA expects a
          transaction manager to commit transactions. Which
          will happen when you do
          trans.commit();
          See point above, the transaction manager never knew
          about the connection.

          The warning is because the transaction manager
          expires the transaction. It never did anything anyway.

          Regards,
          Adrian

          • 2. Re: Transaction Problem!!!
            vickyk

            Hi,
            Now my code is properly ordered , infact last time it was ordered but then due to many trial changes it was in that order .

            UserTransaction trans=(UserTransaction)ocontext.lookup("UserTransaction");
            trans.begin();
            DataSource ods=(DataSource)ocontext.lookup("java:/XAOracleDS");
            System.out.println("After getting DataSource");
            Connection ocon=ods.getConnection();
            System.out.println("After getting Connection"+ocon.getAutoCommit());
            PreparedStatement ostmt=ocon.prepareStatement("update gangsterejb set name=? where id = ? ");
            ostmt.setString(1,"VK1");
            ostmt.setInt(2,3);
            int i=10;
            out.println("Value Of i before begin:"+i+"");
            System.out.println("After update");
            i++;
            System.out.println("Before Starting Commit");
            trans.commit();
            System.out.println("After Committing");
            out.println("Value Of i:"+i+"");
            out.println(trans+"");

            Now the code gets executed fully but does not make any changes in the DataBase.Why is this happenning?
            regards
            Vicky

            • 3. Re: Transaction Problem!!!
              vickyk

              Hi,
              Now my code is properly ordered , infact last time it was ordered but then due to many trial changes it was in that order .

              UserTransaction trans=(UserTransaction)ocontext.lookup("UserTransaction");
              trans.begin();
              DataSource ods=(DataSource)ocontext.lookup("java:/XAOracleDS");
              System.out.println("After getting DataSource");
              Connection ocon=ods.getConnection();
              System.out.println("After getting Connection"+ocon.getAutoCommit());
              PreparedStatement ostmt=ocon.prepareStatement("update gangsterejb set name=? where id = ? ");
              ostmt.setString(1,"VK1");
              ostmt.setInt(2,3);
              int i=10;
              out.println("Value Of i before begin:"+i+"");
              System.out.println("After update");
              i++;
              System.out.println("Before Starting Commit");
              trans.commit();
              System.out.println("After Committing");
              out.println("Value Of i:"+i+"");
              out.println(trans+"");

              Now the code gets executed fully but does not make any changes in the DataBase.Why is this happenning?
              regards
              Vicky

              • 4. Re: Transaction Problem!!!
                vickyk

                Hi,
                Now my code is properly ordered , infact last time it was ordered but then due to many trial changes it was in that order .

                UserTransaction trans=(UserTransaction)ocontext.lookup("UserTransaction");
                trans.begin();
                DataSource ods=(DataSource)ocontext.lookup("java:/XAOracleDS");
                System.out.println("After getting DataSource");
                Connection ocon=ods.getConnection();
                System.out.println("After getting Connection"+ocon.getAutoCommit());
                PreparedStatement ostmt=ocon.prepareStatement("update gangsterejb set name=? where id = ? ");
                ostmt.setString(1,"VK1");
                ostmt.setInt(2,3);
                int i=10;
                out.println("Value Of i before begin:"+i+"");
                System.out.println("After update");
                i++;
                System.out.println("Before Starting Commit");
                trans.commit();
                System.out.println("After Committing");
                out.println("Value Of i:"+i+"");
                out.println(trans+"");

                Now the code gets executed fully but does not make any changes in the DataBase.Why is this happenning?
                regards
                Vicky

                • 5. Re: Transaction Problem!!!
                  vickyk

                  Hi,
                  Now my code is properly ordered , infact last time it was ordered but then due to many trial changes it was in that order .

                  UserTransaction trans=(UserTransaction)ocontext.lookup("UserTransaction");
                  trans.begin();
                  DataSource ods=(DataSource)ocontext.lookup("java:/XAOracleDS");
                  System.out.println("After getting DataSource");
                  Connection ocon=ods.getConnection();
                  System.out.println("After getting Connection"+ocon.getAutoCommit());
                  PreparedStatement ostmt=ocon.prepareStatement("update gangsterejb set name=? where id = ? ");
                  ostmt.setString(1,"VK1");
                  ostmt.setInt(2,3);
                  int i=10;
                  out.println("Value Of i before begin:"+i+"");
                  System.out.println("After update");
                  i++;
                  System.out.println("Before Starting Commit");
                  trans.commit();
                  System.out.println("After Committing");
                  out.println("Value Of i:"+i+"");
                  out.println(trans+"");

                  Now the code gets executed fully but does not make any changes in the DataBase.Why is this happenning?
                  regards
                  Vicky