• Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB3 RollBack Txn Error  RSS feed

 
sivaprasad pasupulathi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a SessionBean(EJB3),which I call from a Web service.The method in the session bean had Txn Attribute set to REQUIRES_NEW.In side this method i make some data Base calls.
In my catch block I have another data base call which will record the exceptions.I have Txn Attribute set to REQUIRES_NEW for my error Method.
In the same catch block I call context.setRollbackOnly();

But I see the error txn rollbacked and all the DB statements in the try block does not rollback.
Is there anything which I am missing here?



Thanks

 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am not sure if I understood your problem quite well.
But one thing you need to keep in mind is that the Tx attributes are enforced and orchestrated by the remote interface stub implementation by app. server. As you know we use the declarative transaction attribute's support to handle the transaction, but someone has to ensure that this method call to be wrapped in a existing or new transaction or no transaction at all. This is the code that ejb compile will generate for you via implementation of remote interface. Now in your case as you are calling the method "storeerror" as a plain method call from within your try block, it is not wrapped/decorated with the code that will start a new transaction for "storeerror" method.

Hope this helps,
amit
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

and all the DB statements in the try block does not rollback.


Could you tell me what do you mean by the above ??
 
sivaprasad pasupulathi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

let me get this a bit clear.



if stmt1 executed successfully and when stmt2 failed,i call ejb 2 and when i return back to ejb 1 ,Do I need to throw an appException for stmt1 to rollback? or do I need to throw an runtimeException?Do i need to specify any annotation tags at my method level for stmt 1 to rollback?
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think your unit of work is stmt1,stmt2 only and not the transactions in the EJB2 then you will need to throw RuntimeException or EJBException that would rollback Tx1 started in EJB1. In addition to this if you want to throw ApplicationException to the end client then you need to set the annotation attribute @rollback=true for the application exception you would design. This holds true if your unit of work is as mentioned above.
It wasn't clear from your reply if my earlier reply helped you or not.

regds,
Amit
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you give a try with calling EJBContext.setRollbackOnly() within your Catch block ??

 
sivaprasad pasupulathi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply guys..

The annotation @ApplicationException(rollback=true) for my ApplicationException helped.I still have few questions..

I have two methods inside my class.The class level transaction is set to REQUIRED and method2 has transaction set to REQUIRES_NEW.

The method1 rollbacks successfully for any errors within it but method2 does not?

I am not sure why?Is it because I have REQUIRES_NEW ? Both the methods throw my ApplicationException and this exception has the necessary Annotation.

when i have context.setRollback(); in my catch block in method2 it works fine...?I am not sure why I have to do this when I have rollback == true at my AppplicationException class level.

Thanks for the info guys.
 
sivaprasad pasupulathi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried to call the same Ejb's LocalIterface within the EJB and had Require_New annotation to my method.And works as expected.Thanks for all the info.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!