Hello all, I'm trying to write my own interceptor, and have a question or two...

I'd like to write a simple interceptor that will manage hibernate sessions for 
me. I know that there are more sophisticated ways to do this using Spring, etc. 
but this is mainly a learning experience.

So, given the simple code below, my questions are:


1)      Does it appear to be thread-safe?

2)      Is there any condition, other than an unhandled exception elsewhere in 
the app, that would cause the transaction not to be committed?

3)      Are there any better places to stash the session and transaction 
references other than the Action? I tried putting it on the value stack, but 
that wreaked havoc with the Params interceptor.

Thanks!

David

public class HibernateInterceptor implements Interceptor {

                public String intercept(ActionInvocation actionInvocation) 
throws Exception {

                                Session sess = HibernateUtil.getSession();
                                Transaction tx = sess.beginTransaction();

                                // Put sess, tx in Action
                                Action action = (Action) 
actionInvocation.getAction();
                                if(action instanceof MyAction ) {
                                                
((MyAction)action).setHibSession(sess);
                                                
((MyAction)action).setHibTransaction(tx);
                                }

                                String rslt = actionInvocation.invoke();

                                try {
                                                // Try to commit:
                                                tx.commit();

                                } catch (Exception ex) {
                                                // Try to rollback and do other 
stuff here ...
                                }
                                return rslt;
                }
                public void destroy() {}
                public void init() {}
}

Reply via email to