Announcement Announcement Module
Collapse
No announcement yet.
Question on using TransactionTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question on using TransactionTemplate

    Hi, I am using TransactionTemplate to programatically control database transaction. I kind of copied the code from reference.

    Object result = tt.execute(new TransactionCallback() {
    public Object doInTransaction(TransactionStatus status) {
    updateOperation1();
    return resultOfUpdateOperation2();
    }
    });

    My question is assuming updateOperation1 throws a typed exception, for example, java.io.IOException, how to handle it within doInTransaction() method? I guess I can catch it and throw an untyped exception such as java.lang.Error. But my goal is really to catch IOException around tt.execute().

    Thanks.

  • #2
    Re: Question on using TransactionTemplate

    Originally posted by song
    My question is assuming updateOperation1 throws a typed exception, for example, java.io.IOException, how to handle it within doInTransaction() method? I guess I can catch it and throw an untyped exception such as java.lang.Error. But my goal is really to catch IOException around tt.execute().
    Yes, transaction template is restricted to unchecked exceptions. For throwing checked exceptions you could use a transaction interceptor.
    From the docs,

    Code:
    As with HibernateInterceptor, TransactionInterceptor allows any checked application exception to be thrown with the callback code, while TransactionTemplate is restricted to unchecked exceptions within the callback.
    Not neat. But a work around with your current setup could be to throw a runtime exception with the IO exception as the message and catch (and/or convert) it around execute.

    Regards,

    Amit

    Comment

    Working...
    X