Discussions

EJB programming & troubleshooting: Transaction Rollback using Statefull Session Bean

  1. Hi,
    I am building a statefull session bean which inside using a JTA --> UserTransaction.
    I'm using MySQL 4.1.8 for the database, but it is always failed when the bean calls rollback method.
    I'm also using JBoss 3.2.5 for the EJB container. Can anyone please help me on this problem?
    Thanks.
  2. Hi, sorry i forgot to post my source code. Below is the code:

    public class BOCustomerTransactionBean implements SessionBean{

         public void createCustomer(Vector customerData) throws EJBException{
              System.out.println("ejbBOCustomerTransaction createCustomer called...");
              UserTransaction transaction = sessionContext.getUserTransaction( );
              CreateID createID = new CreateID();
              custID = createID.getCustomerID();
              custName = String.valueOf(customerData.get(0));
              try{
                   transaction.begin();
              }
              catch(Exception e){
               throw new EJBException(e);
              }
              try{
                   System.out.println("" + transaction.getStatus());
                   
                   statementOneInsert.setString(1, custID);
                   statementOneInsert.setString(2, custName);
                   statementOneInsert.executeUpdate();
                   System.out.println("1");

                   statementTwoInsert.setString(1, custID);
                   statementTwoInsert.setString(2, custName);
                   statementTwoInsert.executeUpdate();
                   System.out.println("2");

                   transaction.commit();
                   System.out.println("Transaction committed...");
              }
              catch(Exception e){
                   try{
                        transaction.rollback();
                        System.out.println("Transaction rolled back...");
                   }
                   catch(Exception re){
                    throw new EJBException(re);
                   }
              }
         }
    }

    Thanks
  3. It's unclear what you mean by "it is always failed". For sure you don't need all those try/catch. If an exception happen and it's not an application exception the transaction will be rollback anyway.

    You probably want to use container managed transaction to not need to call commit. Then the code will be simpler and less buggy.
  4. the case is that i drop(delete) the table in DS2, the data that has already been inserted into DS1 has to be rolledback, but it is always failed. The record is already there, eventhough the transaction.rollback() has been executed.
    Yes, I know I can use CMT, but in this case I want to try to use BMT, and find out if it can handle this kind of transaction as it said so.
    All the try{...}catch{...} is only to make sure I catch all posibbility of exception during the transaction execution.
    Do you have any suggestion on this?
    Thanks a lot.
  5. Sound like your two transactions are not in the same JTA transactions. You can enable JTA loggin and see the transaction number in both DS to see if they are the same. To work with JTA the DB driver need to support it and be configured for it.
  6. Can you tell me how to enable the JTA log and see the transaction number within the two DS?
    Do you have any experience in configuring a MySql JDBC Driver so it can support JTA?
    Thanks
  7. You will have to dig in:

    http://docs.jboss.org/jbossas/getting_started/v4/html/tour.html#sect.logging

    and

    http://docs.jboss.org/jbossas/getting_started/v4/html/db.html#d0e2091
  8. A lot of reading...[ Go to top ]

    Seems that's a lot of reading to do...
    thanks a lot for your help, I will update it when I found something usefull and can solve the problem from the documents.
  9. You can check whether the transaction isolation levels specified for these different Datasources are the same
    and are according to the requirement.
  10. Drop means is DDL??[ Go to top ]

    If you are trying to drop a table in transaction context then my friend you are in trouble unless I misinterpreted the problem.

    If the statements are simple insert and update then make sure you get a XAConnection object before obtaining the statements.

    If everything is already in place. I dont see any code problems other than those redundant try/catch.
  11. question on CMR[ Go to top ]

    hi all i have a question on CMR.........i have a problem...i need to get CMRs for a field of an EJB without defining that field in my ejb-jar.xml as a cmp-field.......

    any one have the sol. plz let me know....

    Hemant