Reputation: 263078
I am trying to use a @Transactional
method inside a CDI class, instead of an EJB:
@javax.inject.Named
// fails @javax.enterprise.context.ApplicationScoped
// fails @javax.enterprise.context.SessionScoped
// works @javax.ejb.Singleton
// works @javax.ejb.Stateless
public class SomeClass {
@javax.persistence.PersistenceContext
private EntityManager em;
@javax.annotation.PostConstruct
@javax.transaction.Transactional
public void someMethod() {
em.persist(someEntity);
}
}
When I annotate SomeClass
with @Singleton
or @Stateless
, everything works.
When I annotate SomeClass
with @ApplicationScoped
or @SessionScoped
, WildFly 13 displays the following error message:
Transaction is required to perform this operation (either use a transaction or extended persistence context)
I was under the impression that @Transactional
works with CDI since Java EE 7. Have I been mistaken? Or am I simply missing some additional configuration?
Upvotes: 2
Views: 1505
Reputation: 40296
I'll try to give a short list of things to look when trying to make @Transactional
work with CDI, so as to give the answer a bit more value than the comment:
javax.transaction.Transactional
, not javax.ejb.TransactionAttribute
, which works for EJBs!javax.transaction.Transactional
exists e.g. in WEB-INF/lib, when running in a full JEE application server. If you want to utilize it in a non-full-JEE environment, you will need to have it in the classpath.@Transactional
is implemented as a CDI interceptor by the latest JTA specifications. As such:
@PostConstruct
[THIS WAS THE PROBLEM IN THIS QUESTION], and it is NOT activated when invoking methods of this
object, BEWARE!!!Upvotes: 6