users@glassfish.java.net

Transaction exception, but I think it should work

From: <glassfish_at_javadesktop.org>
Date: Mon, 21 Sep 2009 03:54:14 PDT

Hi, this is a little code first:

[code]
@Local(PersonEAO.class)
@Stateless(name = "ejb/personEAO")
public class PersonEAOBean implements PersonEAO {

    @PersistenceContext(name = "Test-JPA")
    private EntityManager em;

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    @Override
    public void add(Person person) {
        em.persist(person);
    }

    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    @Override
    public void addAll(List<Person> people) {
        for (Person person : people) {
            add(person);
        }
    }
}
[/code]
The addAll method suspends a possible transaction, and is invoked without any transaction. It in turn calls add, which always creates a new transaction, in a loop. Add uses an EntityManager to persist, and the call fails with a TransactionRequiredException. Why is that, shouldn't the add method start and finish the transaction for every call?
(I do want to call the persist method in a new transaction every time, this is a very specific part of the app and this cannot change.)

Regards.
[Message sent by forum member 'szczyp' (szczyp_at_gmail.com)]

http://forums.java.net/jive/thread.jspa?messageID=364901