The moose likes EJB and other Java EE Technologies and the fly likes EJB transaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB transaction" Watch "EJB transaction" New topic

EJB transaction


Joined: Jul 18, 2002
Posts: 5

I am inserting the data into six tables under single transaction scope. Basically, if any errors occur it should role back the complete transaction.

My question is Can I use entity beans (CMP) and DAO based Stored Procedure calls from a session bean, both in a single transaction?

Right now even though there are problems with some of the procedure calls, the transaction scope is not working and didn�t role back.

I have tried many methods, including using java.sql.Connection based trasactions, CMT based trasactions (i.e. allowing the container to handle the trasactions) and finally using BMT in the session bean and CMT for the entity beans. All the same i am still faced with this problem.

Here is my code for the last of these options.

ut = ejbContext.getUserTransaction();

// Insert data into Course Table
// Insert data into CourseSession table
// Insert data into Course Request table
// Insert data into Manager Table
// Insert data into Employee table by Using CMP
insertEmployee(employeeDTO, courseRequestDTO);
//Insert data into CourseRequest Table


catch(SQLException se){
System.out.println("<<<< Transaction failed >>>>>");

<b>Can anyone help me on this ?</b>

Thanks and Regards

Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi KamranZaidi,

Please answer to the questions below:
  • Do you handle transactions in your stored procedures?
  • What do you mean the transaction is not rolled back? Some of sqls are rolled back and other are not or none of them is rolled back? Did you notice any pattern in this behavior?
  • What type of exceptions did you encountered?
  • Looking at your code I can see that you catch a SQL exception and rollback the transaction. How about if another type of exception is thrown? How about RuntimeExceptions, how do you handle those?

  • And by the way, are you sure the only exceptions you get are SQLExceptions?

    I think, therefore I exist -- Rene Descartes

    Joined: Jul 18, 2002
    Posts: 5
    Hi Valentin Tanase,

    Follwing are answeer of your questions...

    Ans 1) No I am handling the transaction in DAO.

    Ans 2) I have insert the data into six tables the error is occur in one table but it will insert the data into Manager table. According to my logic the complete transaction will roll back if error occur inserting a data into any table.

    Ans 3) The following run time exception.

    Start server side stack trace:
    java.rmi.RemoteException: EJB Exception: ; nested exception is:
    java.lang.IllegalStateException: Transaction does not exist
    java.lang.IllegalStateException: Transaction does not exist
    at weblogic.transaction.internal.TransactionManagerImpl.rollback(
    at coursedao.CourseDAOBean.insertMainCourseRequest(
    at coursedao.CourseDAOBean_dnar3d_EOImpl.insertMainCourseRequest(
    at coursedao.CourseDAOBean_dnar3d_EOImpl_WLSkel.invoke(Unknown Source)
    at weblogic.rmi.internal.BasicServerRef.invoke(
    at weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(
    at weblogic.rmi.internal.BasicServerRef$
    at weblogic.rmi.internal.BasicServerRef.handleRequest(
    at weblogic.rmi.internal.BasicExecuteRequest.execute(
    at weblogic.kernel.ExecuteThread.execute(
    End server side stack trace
    ; nested exception is:
    java.lang.IllegalStateException: Transaction does not exist

    Ans 4) Yes, I used SQLException but I changed in my code and using Exception.
    Roger Chung-Wee
    Ranch Hand

    Joined: Sep 29, 2002
    Posts: 1683
    It looks as if a method is invoked while the instance is not associated with a transaction. Post your DD which shows the trans-attribute elements.

    SCJP 1.4, SCWCD 1.3, SCBCD 1.3
    Valentin Tanase
    Ranch Hand

    Joined: Feb 17, 2005
    Posts: 704
    Hi KamranZaidi,

    Ans 1) No I am handling the transaction in DAO.

    This might be a problem though. If the DAO starts a transaction then accordingly with your code:

    You will face the next two issues:
  • You start the transaction at the beginning of the method above and you might start another (nested one) inside any of insert methods. Be aware that j2ee doesn�t support nested transactions. In your case I would expect them to be treated as flat transactions.
  • I also noticed that you use some CMP entity beans. You also must be aware that entity ejbs don�t support BMT. You might check whether you handle transactions in your entity beans.

  • .
    Finally you might follow Roger�s hint. A possible scenario (of course they could be many other) might be that your BMP mandates the use of transactions and you probably committed the current transaction after the insertManager runs. Since the BMP bean is not associated with any transaction, an exception will be thrown.

    Joined: Jul 18, 2002
    Posts: 5
    Hi Rogers & Valentin,

    Here is My DD (ejb-jar.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "">

    NOTE:- I am using CMT and BMT according to your comment J2EE doesn�t support the both transaction in single scope.
    I agree. Here's the link:
    subject: EJB transaction
    It's not a secret anymore!