users@glassfish.java.net

Using _at_Transactional

From: Steven Van Impe <steven.vanimpe_at_icloud.com>
Date: Sun, 05 May 2013 10:26:32 +0200

I'm trying to rework some of my JAX-RS projects to use CDI instead of EJB. I started by trying out this little example (using a JPA entity class Message):

@Path("test")
@RequestScoped
public class TestService {
    
    @PersistenceContext
    private EntityManager em;

    @Transactional
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Message test() {
        return em.find(Message.class, 1);
    }
}

This deploys fine (using GlassFish 4 b87), but when I try a GET request, I get the following exception:

javax.transaction.TransactionRequiredException: Managed bean with Transactional annotation and TxType of MANDATORY called outside of a transaction context

I've tried changing @Transactional to @Transactional(Transactional.TxType.REQUIRED) -even though REQUIRED should be the default according to the JTA 1.2 JavaDoc- as well as annotating the class instead of the method, but I always get the same exception. Am I missing something here? I tried something similar to this during the Java EE 7 Hand-on Lab @ Devoxx UK, and it seemed to work fine then (using an older build of GlassFish).