4 Replies Latest reply on Aug 24, 2012 6:01 PM by Remus Vitan

    ejb Interceptor outside the transaction  how to?

    Remus Vitan Newbie

      hello,

      i would like to create an interceptor that should analyze the constraints violated in the db operations... i have already done something like this :

      @AroundInvoke

                public Object catchSomeDBExceptionAsBusiness(InvocationContext context) throws Exception{

                          try {

                                              Object result = context.proceed();

                                              return result;

                          } catch (Exception  e) {

                                    //examine e.getMessage and throw some special type of exception ...

                                    throw e;

                          }

                }

       

      Still, it does work when my hibernate operation is an insert (i believe because it is imediatelly flushed)

      And it does not catch any exception when an update operation is made (even though it still violates the some constraint).

      If after the update operation i explicitly flush the transaction, then an exception is thrown and caught inside the interceptor just like in the case of insertion.

       

      How can i make my interceptor to be above (what i believe it is ) the transaction manager interceptor (i refer to the code that takes care of commiting the transaction).

      and hopefully catch the exception thrown when the flush is done accordingly to the triggered commit...

       

      And one more thing... inside an ejb method call, how can i find out the transaction attribute at runtime?

       

       

      Thanks a lot in advance

        • 1. Re: ejb Interceptor outside the transaction  how to?
          Nicklas Karlsson Master

          Can you inject a UserTransaction (or look it up) to get the transaction attribute?

          Not sure but I would think ejb-jar.xml could be used for setting interceptor precedence.

          Are there any Hibernate callbacks/interceptors you could use for the case?

          • 2. Re: ejb Interceptor outside the transaction  how to?
            Wolf-Dieter Fink Master

            I don't know a way to do surround the Tx in EJB3 with an interceptor, it will always inside the Tx and all other container related stuff.

            The problem is that the Tx might be rolled back in case of DB issues (like constraints, optimistic locking, etc) this will happen during the commit phase and not visible inside the bean method because at this state the Tx is still active and valid.

             

            You might do it with a BeanManagedTransaction - lookup UserTransaction and manage it

            Or you surround each Bean with another one which is annotated as TX=Never and call your original one (solution from anothe r forum member).

             

            But in both cases you have the drawback that you have to do this for every method where you want this behaviour.

            • 3. Re: ejb Interceptor outside the transaction  how to?
              Remus Vitan Newbie

              @Nicklas Karlsson

               

              Actually i can inject the UserTransaction event though i believe i am not supposed to do it in CMT bean... but how can i obtain the transaction attribute with it ?

               

              You are right about interceptors precedence in ejb-jar.xml but i believe that i can specify there only my custom interceptors, if not please give me clue ... and i'll search in depth

               

              Actually hibernate interceptors sound like a very good idea,

              still ... i want to put the whole invocation (of ejb method) inside a try-catch block so i can catch the exceptions and alter some of them (resend as ApplicationException).

              for the moment i have no idea how i might do this in the callbacks (and i haven't managed to make a hibernate callback to work yet ).

              • 4. Re: ejb Interceptor outside the transaction  how to?
                Remus Vitan Newbie

                @ Wolf-Dieter Fink

                 

                What you have stated seems very true, and very discouraging .

                 

                My only purpose is to catch the sent exception... so the rollback phase should be passed by that time.

                Of course , as you have probably guessed the BMT is not an option in my case.