[rules-users] Some Code Problem about SingleSessionCommandService

Rui Tang tangrui at zyeeda.com
Sun Nov 14 08:39:10 EST 2010


Hi list,

During my application development, I encountered some problems about the
exception handling within SingleSessionCommandService class.

In this constructor

    public SingleSessionCommandService(KnowledgeBase kbase,
                                       KnowledgeSessionConfiguration conf,
                                       Environment env) {

the exceptions are handled like this

try {
    ....
}  catch ( Exception t1 ) {
    try {
        this.txm.rollback(); // line 1
    } catch ( Throwable t2 ) {
        throw new RuntimeException( "Could not commit session or rollback",
t2 ); // line 2
    }
    throw new RuntimeException( "Could not commit session", t1 ); // line 3
}

In the first try block, if some exception occurred, exception t1 will be
caught, then this.txm (TransactionManager) will be rolled back. But during
this piece of code, if exception occurred again, throwable t2 will be
caught, then t2 will be wrapped into another runtime exception and re-throw.
In this case the wrapped t1 (in line 3) will never be throw out. But in my
situation, the wrapped t1 exception will be more meaningful than the wrapped
t2 exception.

So I think this code should be changed like this:

try {
    ...
} catch (Exception t1) {
    try {
        this.txm.rollback();
    } catch (Throwable t2) {
        logger.error(t2.getMessage(), t2); // or some other logs, but do NOT
rethrow
    }
    throw new RuntimeException( "Could not commit session", t1
}

Is this right?
-- 
唐睿
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20101114/28df91af/attachment.html 


More information about the rules-users mailing list