-
1. Re: Correct way to abort transaction in session bean?
petriwessman Oct 21, 2003 1:54 AM (in response to petriwessman)Hmm, nobody?
I'd have thought that this was close to a FAQ (though I didn't find the answer there :), since automatic transaction management is such a basic part of EJBs. Apparently it's not as common knowledge as I thought. Hmph. Back to RTFM... -
2. Re: Correct way to abort transaction in session bean?
petriwessman Oct 21, 2003 2:11 AM (in response to petriwessman)Addendum: Sun's j2ee blueprints has the following:
----
6. What are some tips for using container-managed transaction demarcation?
...
* Implement rollbacks by calling EJBContext, and then throwing an exception.
----
I assume that refers to calling setRollbackOnly() on the context(?).
So is the correct j2ee (and/or JBoss) way to do this the following?:
if (kaboom) {
context.setRollbackOnly();
throw new MyOwnException("boom!");
}
Or should I throw an instance of EJBException? -
3. Re: Correct way to abort transaction in session bean?
ioparra Oct 22, 2003 3:53 PM (in response to petriwessman)Generally, anyone correct me if I'm wrong, you have 2 types of exception. Runtime and application.
If a runtime exception is thrown and is observed across a app server boundary(proxies), then the transaction is automatically rollback.
If an application exception is thrown, the appserver will not doing anything to the transaction. It's up to the application to call context.setRollBackOnly().
EJBException is a standard runtime exception that can be extended to include more information. The transaction should also rollback if a NullPointerException or any other type of runtime exception is caught.
Answer your question?
-Ivan -
4. Re: Correct way to abort transaction in session bean?
petriwessman Oct 24, 2003 5:38 AM (in response to petriwessman)...
> EJBException is a standard runtime exception that can
> be extended to include more information. The
> transaction should also rollback if a
> NullPointerException or any other type of runtime
> exception is caught.
>
> Answer your question?
> -Ivan
Yes, thanks, it confirms what I've read elsewhere.
So apparently there are two ways of doing this:
a) throw EJBException or some other "system" exception, this will cause rollback automatically.
b) call context.setRollbackOnly(), then throw your own (application) exception.
Which one is more suitable for you depends on the application, and on whether you want to wrap eveything as EJBExceptions.