• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

setRollbackOnly() related question

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have methodA() and methodB() in bean1.

methodA(){

..
methodB();
}

methodB is called recursively in methodA and methodB is the one that calls the EJB's (CMP) methods to update some data.

If the first call to methodB() goes through fine and the second call fails, I want to commit the first call and rollback only the second call.

I am thinking this can be done by setting the transaction attribute to RequiresNew and calling the setRollbackOnly() method in methodB().

When I tested this is not how it works. It is rolling back all the calls.

Why is this happening? How can I get the behaviour I need from the transaction?
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy kavitha ,

Use of CMT is a pre requisite ??? could you use BMT instead ???

It's only a possibility ...

cheers ,

Paulo Nunes
MCP OCP IBM285 SCJP 1.4 SCWCD 1.4 SCBCD 1.3
 
kavitha yalamanchili
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paulo,

I would like to see how this can be acheived using CMT :-).

Thanks,
Kavitha
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the transaction attribute for methodA()?
 
Vaithiya Sundaram
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make methodB() as public method and call methodB() using its component interface from methodA().
 
Vaithiya Sundaram
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i.e.

It depends on how you call methodB from methodA. If you just call it
like you would any other method, It is executed as a normal method. If
you want a new transaction to be started call the method through the
local or remote interface of that bean.
 
kavitha yalamanchili
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vaithiya,

The transaction attribute for methodA() is Requires and the one for methodB() is RequiresNew.

So do you mean to say
If there are two methods in a bean, methodA() and methodB()
and if the transaction attribute for methodA() is Requires and the transaction attribute for methodB() is RequiresNew, when we call methodB() inside methodA() like this
methodA(){
methodB();
}

methodB() will get the transaction of methodA() instead of a new one if methodA() is called using a remote or local interface of that bean??

I was under the impression methodB() will have a new transaction no matter how it is called.

Thanks,
Kavitha
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think u cannot achieve u'r requierment using a CMP bean.
In your scenario the transaction is not suspended nor is commited so if the setRollbackOnly() method returns true in methodB()
the entire transaction rollsback.

You have to use a BMP bean, where u can have a new transaction
for method A and commit the transaction before propogating to method B


Please correct me if i am wrong...
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Vaithiya Sundaram
If you want a new transaction to be started call the method through the
local or remote interface of that bean.



I've just tested it with JBoss. It works exactly as you say! A new transaction is started by calling through the remote interface.

But I can't find any reference, which describe that transactions might be started only in this way. Should I look better in spec ?

It's hard to imagine that the behaviour is up to the container provider.
 
kavitha yalamanchili
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. I tested it the same way and it works. I must have not understood the spec properly :-(
In my test I am doing something like this
bean1{
methodA(){

bean1.methodB();

}

methodB();
}

Is this a right way of coding? calling the method on the same beans local interface? I am calling myself thru my local interface..

But this works :-)
 
Vaithiya Sundaram
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kavitha, your code is exactly right.

The bottom line is...The container is responsible for managing transactions
depending on the value of transaction attributes ONLY IF the method is called via the bean's home or component interface.

(spec. 17.6.2 page 357, second paragraph: The following.......when the method is invoked via the enterprise bean's home or component interface.)
 
kavitha yalamanchili
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaithiya,

Thanks a lot.

Kavitha
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

methodB is called recursively in methodA and methodB is the one that calls the EJB's (CMP) methods to update some data.

If the first call to methodB() goes through fine and the second call fails, I want to commit the first call and rollback only the second call.



I understand the scenario to be:



And you want the first call to commit because it went through fine and the second one to rollback because it failed. That suggest there's some sort of condition in methodB. If there isn't and you call setRollbackOnly no matter what, then both calls will rollback.



You would probably throw an app exception when things go bad or just a sys exception and forget about setRollbaclOnly but let's not get into that. The point is with something like this you would have the tx commit in some cases and rolback in other, which is what you want isn't it??

I'm probably missing something here cause I don't understand what the discussion about going through the home interface has to do with anything.
If methodB has an att of RequiresNew it will run in a new tx, regardless of who's calling it. If that's not how it works I have to cancel my exam for thursday!!!
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!