deltaspike-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gerhard Petracek <gpetra...@apache.org>
Subject Re: Exception "Transaction not active" inside my own interceptor combined with @Transactional
Date Sat, 19 Sep 2015 16:16:54 GMT
hi greg,

you are very welcome to ask questions on this list.
(helping deltaspike-users is the main intention here...)

@your issue:
your code isn't valid with cdi. if you don't like to use both
interceptor-annotations in your service-classes, just create a
cdi-stereotype annotation (e.g. @Service) and annotate that one with both
interceptor-annotations.

regards,
gerhard



2015-09-19 17:56 GMT+02:00 Grzesiek <gregory.d3@gmail.com>:

> Hi all,
>
> It is my first post here, usually I would use StackOverflow, but there is
> almost none questions/ answers tagged with DeltaSpike.
>
> Sorry for wasting your precious time. But I'm struggling for a couple of
> hours with not working @Transactional interceptor from
> *deltaspike-jpa-module* inside my own interceptor.
>
> Details:
> My own interceptor: @DetectIntegrityConstraintViolationInterceptor needs to
> have injected an EntityManager instance, but unfortunately I'm always
> getting "*java.lang.IllegalStateException: Transaction not active*" inside
> this interceptor (injection of EntityManager works fine, but transaction is
> not started automatically).
>
> Do somebody know why?
>
> My use case is quite simple: one service, marked with @Transactional, and
> its one method is also annotated with
> @DetectIntegrityConstraintViolationInterceptor.
>
> I've been trying blindly a couple of combinations, but none works:
>   * I've marked also my interceptor with @Transactional
>   * I've marked also both my Interceptor and InterceptorBinding with
> @Transactional
>
> Simplified code:
>
> @ManagedBean // make it a CDI bean
> > @Interceptor
> > @DetectIntegrityConstraintViolation
> > public class DetectIntegrityConstraintViolationInterceptor {
> >
> >     @Inject
> >     private EntityManager em;
> >
> >     @AroundInvoke
> >
>     // @Transactional     // ALSO BLINDLY TRIED THIS, but no luck
> >
>     public Object processInvocation(InvocationContext ctx)
> >             throws Exception {
> >         Object o = null;
> >         try {
> >             o = ctx.proceed();
> >             em.flush();          // THIS CAUSES EXCEPTION, AS TRANSACTION
> > DOESN'T EXIST
> >         } catch (PersistenceException ex) {
> >             // ....
> >         }
> >         return o;
> >     }
> >
> }
> >
>
> Service class looks like:
>
> import org.apache.deltaspike.jpa.api.transaction.Transactional;
> >
>
> >
> @Transactional
> > public class MyServiceBean implements MyService {
> >
> >     @Inject
> >     EntityManager em;
> >
> >     @DetectIntegrityConstraintViolation
> >     public ResponseEntity<User> createUser(User user) {
> >         em.persist(user);
> >     }
> > }
> >
>
> Tip: EntityManager was tried both: @TransactionScoped and @RequestScoped -
> result similar, only message was a bit different ;-)
>
> Any clues?
> Ps. I'm sorry, if above code is not readable, bu I don't know how to format
> code inside an email.
>
> Libs versions used:
>  * deltaspike-jpa-module-api 1.4.1
>  * weld-servlet-core  2.2.0.Final
>  * JDK 1.7
>
> Kind regards
> Greg Demecki
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message