Hi David,
I've written a couple of these so let me give you some advice:
Create an Interface "HibernateAware" that your actions will implement
if they want a Hibernate Session and Transaction injected.
public interface HibernateAware
{
}
On Wed, Nov 10, 2010 at 3:45 PM, Altenhof, David Aron
<[email protected]> wrote:
> 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() {}
> }
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]