aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes setRollbackOnly() related question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "setRollbackOnly() related question" Watch "setRollbackOnly() related question" New topic
Author

setRollbackOnly() related question

kavitha yalamanchili
Greenhorn

Joined: Oct 10, 2002
Posts: 25
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?
Paulo Asterio Nunes
Ranch Hand

Joined: Jul 06, 2004
Posts: 63
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


Regards,<br /> <br />Paulo Nunes (Brazil)<br />MCP OCP IBM285 SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCEA (I)
kavitha yalamanchili
Greenhorn

Joined: Oct 10, 2002
Posts: 25
Paulo,

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

Thanks,
Kavitha
Vaithiya Sundaram
Ranch Hand

Joined: Apr 22, 2004
Posts: 98
What is the transaction attribute for methodA()?


Vaithiyanathan Sundaram<br />SCJP, SCBCD, SCEA part1
Vaithiya Sundaram
Ranch Hand

Joined: Apr 22, 2004
Posts: 98
make methodB() as public method and call methodB() using its component interface from methodA().
Vaithiya Sundaram
Ranch Hand

Joined: Apr 22, 2004
Posts: 98
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

Joined: Oct 10, 2002
Posts: 25
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
VP Jain
Ranch Hand

Joined: Feb 11, 2004
Posts: 81
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...


SCJP SCWCD SCBCD <br /> <br />what u do in life echos in eternity!
Wladimir Babitzki
Greenhorn

Joined: Apr 27, 2004
Posts: 19
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

Joined: Oct 10, 2002
Posts: 25
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

Joined: Apr 22, 2004
Posts: 98
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

Joined: Oct 10, 2002
Posts: 25
Vaithiya,

Thanks a lot.

Kavitha
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
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!!!
 
wood burning stoves
 
subject: setRollbackOnly() related question