File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB transaction

 
KamranZaidi
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 704
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    KamranZaidi
    Greenhorn
    Posts: 5
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1683
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Valentin Tanase
    Ranch Hand
    Posts: 704
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 5
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic