File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes transaction question in SessionBean 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 "transaction question in SessionBean" Watch "transaction question in SessionBean" New topic
Author

transaction question in SessionBean

Liz Lee
Greenhorn

Joined: Feb 23, 2003
Posts: 6
suppose there are two EntityBean A and B with one-one relationship. If A is deleted, B also should be delete.
There is a mothodA() in SessionBean.
public void methodA() throws Exception{
......
A a = home.findByPrimaryKey(aId);
B b = a.getB();
b.remove();
//... do something others
// (1)
a.remove();
}
The session bean is stateless, and methodA's transaction attribute is 'Required'.
According to the session facade pattern, I consider that there should be only one transaction in methodA, so if the exception is thrown, transaction should be rolled back. Both a and b should be still in DB.
But now in fact, b is deleted, a is still in DB. I don'w know what's wrong with my configuration.
Malar Sundar
Greenhorn

Joined: May 22, 2003
Posts: 7
Hi,
You should handle exceptions and mark the transaction to rollback, so that the container knows whether to commit or rollback.
// session bean method...
methodA() throws Exception
{
try {
// call b.remove();
// call a.remove();
}
catch(Throwable t)
{
getSessionContext).setRollbackOnly);
// throw new Exception(t.getMessage());
// or throw your application exception...
}
}
Liz Lee
Greenhorn

Joined: Feb 23, 2003
Posts: 6
It's OK now.
Thank you very much.
[ May 22, 2003: Message edited by: Liz Lee ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: transaction question in SessionBean
 
Similar Threads
interesting issue
Head First EJB Chapter 6, question 15
Is this a nested transaction?
setRollbackOnly() related question
can't define a variable