EJB programming & troubleshooting: EJB Transactions

  1. EJB Transactions (1 messages)


    Iam facing a problem in case of BMP, transaction set to REQUIRED for all methods in the bean.Iam using MSAccess for testing this problem.

    **----Code given below-----------**

    <some code here--1>
    insert into tbl_master_book values(.....)
    <some code here--2>
    insert into tbl_master_cust values(.....)
    <some code here--3>

    **------Code ends here---------**

    In above code iam using two insert queries to database.But after first insert , the code block 2 raises an exception and my program terminates gracefully.But my problem is that the first insert stmt inserts data into my database even when an exception is raise and nothing is rollback.
    What is way out to tackle this situation.

    Iam confused at the point wheather the Bean transactions are in synchronization with database??

    Do help me in this situation


  2. EJB Transactions[ Go to top ]

    If your code throws an application exception, then the transaction does not automatically rollback.
    Transaction rolls back only for system exceptions(RuntimeExceptions,RemoteException and its subclasses like EJBException).
    You could roll back application exception by any of the following methods:
       -Catch your application exception (even SQLException ) and throw it back as EJBException
      - EJBContext::setRollbackOnly() to mark the transaction for rollback whenever an exception is thrown.

    If the code already throws only System Exception, make sure that you have not set setAutoCommit(true) for the database connection. Connection's commit, setAutoCommit should not be used for Container managed transaction demarcation.