Mihail Stoynov's blog!
Surrender your ego
Friday, May 02, 2008
EJB Exception wrapping, commiting broken transactions, resource handling in finally statement - overridding thrown exceptions
In this piece of code the task is to override system exceptions (JPA, JTA exceptions) with application exceptions:
try {
transaction.begin();
bm(); //business method
} catch (PersistenceException) {
//rewrite persistence exception and throw it (1)
} finally {
try {
transaction.commit();
} catch (RollbackException re) {
if( re.getCause() instanceof PersistenceException ) rewrite
else // THROW SOME DEFAULT EXCEPTION (2)
}
}
The rewriting of PersistenceException (PE) is straightforward.
It could come from the business method (
bm()
) and from
commit()
.
When it comes from
commit()
it is wrapped in
RollbackException
. Thus the
if
.
Unfortunately if PE is raised from the
bm()
, it is also raised from the
commit()
.
So if I throw an exception at
(1)
, it gets overridden in
(2)
. As simple as that. Because of the
finally
's ability to override results (OOP got messy here).
So what should be fixed?
First, at
(2)
I shouldn't throw any exception.
Second, I'm thinking of checking the result of
bm()
- if an exception, don't call
commit()
, but
rollback()
.
Sounds right.
Comments [0]
|
Trackback
OpenID
Please login with either your
OpenID
above, or your details below.
Name
E-mail
(will show your
gravatar
icon)
Home page
Remember Me
Comment (Some html is allowed:
a@href@title, blockquote@cite, strike
) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.
Enter the code shown (prevents robots):
Live Comment Preview
dasBlog theme by
Mads Kristensen
RSS feed
Search
Archives
November, 2008 (15)
October, 2008 (16)
September, 2008 (30)
August, 2008 (15)
July, 2008 (14)
June, 2008 (26)
May, 2008 (6)
April, 2008 (21)
March, 2008 (14)
February, 2008 (28)
November, 2007 (5)
October, 2007 (7)
September, 2007 (1)
August, 2007 (7)
July, 2007 (3)
June, 2007 (1)
Blog Stats
Total Posts: 203
This Year: 179
This Month: 0
This Week: 0
Comments: 80
Categories
Did you know
Java
rulez
Sucks
БГ
Blogroll
Michael Moore (no rss)
Links
BG-JUG
Copyright policy
No rights reserved.
(You are going to
copy stuff anyway :)
If you mention my
name, thank you.
2008, Mihail Stoynov