wood burning stoves 2.0*
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
Author

EJB transaction

KamranZaidi
Greenhorn

Joined: Jul 18, 2002
Posts: 5
Hi,

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.

try{
ut = ejbContext.getUserTransaction();

ut.begin();
// Insert data into Course Table
insertCourse(courseRequestDTO);
// Insert data into CourseSession table
insertCourseSession(courseRequestDTO);
// Insert data into Course Request table
insertExternalInfo(courseRequestDTO);
// Insert data into Manager Table
insertManager(courseRequestDTO);
// Insert data into Employee table by Using CMP
insertEmployee(employeeDTO, courseRequestDTO);
//Insert data into CourseRequest Table
insertCourseRequest(courseRequestDTO);

ut.commit();

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

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

Thanks and Regards

<b>Kamran</b>
<email>Kamranzaidi@hotmail.com</email>
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?
    Regards.


    I think, therefore I exist -- Rene Descartes
    KamranZaidi
    Greenhorn

    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(TransactionManagerImpl.java:298)
    at coursedao.CourseDAOBean.insertMainCourseRequest(CourseDAOBean.java:316)
    at coursedao.CourseDAOBean_dnar3d_EOImpl.insertMainCourseRequest(CourseDAOBean_dnar3d_EOImpl.java:149)
    at coursedao.CourseDAOBean_dnar3d_EOImpl_WLSkel.invoke(Unknown Source)
    at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:441)
    at weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerRef.java:114)
    at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:382)
    at weblogic.security.service.SecurityServiceManager.runAs(SecurityServiceManager.java:726)
    at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:377)
    at weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:30)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:234)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:210)
    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.
    Regards.
    KamranZaidi
    Greenhorn

    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" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
    <ejb-jar>
    <display-name>CourseInformation</display-name>
    <enterprise-beans>
    <session>
    <display-name>CourseDAO</display-name>
    <ejb-name>CourseDAO</ejb-name>
    <home>coursedao.CourseDAOHome</home>
    <remote>coursedao.CourseDAO</remote>
    <ejb-class>coursedao.CourseDAOBean</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Bean</transaction-type>
    </session>
    </enterprise-beans>
    <assembly-descriptor>
    <container-transaction>
    <method>
    <ejb-name>CourseDAO</ejb-name>
    <method-name>*</method-name>
    </method>
    <trans-attribute>Required</trans-attribute>
    </container-transaction>
    </assembly-descriptor>
    </ejb-jar>



    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: http://aspose.com/file-tools
     
    subject: EJB transaction
     
    Similar Threads
    IBM 287 ICE TEST
    long post IBM.158
    EJB Notes I prepared during my preparation for SCBCD
    SessionSynchronization interface and stateless session bean
    My Study Notes