users@glassfish.java.net

Glassfish & TopLink: db not updated

From: <glassfish_at_javadesktop.org>
Date: Sat, 25 Jul 2009 06:22:12 PDT

Hi,
I'm using Glassfish v2.1 and TopLink (both shipped with NetBeans v6.7). When I update an entity inside an EJB, it seems this is sometimes only known to the EJB/Glassfish, but not outside (db-viewer, other EntityManager).

Here is a test case (from a JUnit test):
public void test() throws NamingException {
        TestService testService =
                (TestService) new InitialContext().lookup("TestService");
        Long id = testService.create("foo1");
        System.out.println("1-service: " + testService.getFoo(id));
        System.out.println("1-direct: " + getFoo(id));

        testService.update(id, "foo2");
        System.out.println("2-service: " + testService.getFoo(id));
        System.out.println("2-direct: " + getFoo(id));
    }

    private String getFoo(Long id) {

        entityManager.getTransaction().begin();
        String foo = entityManager.find(TestEntity.class, id).getFoo();
        entityManager.getTransaction().commit();
        return foo;
    }

And here the service implementation:

@Stateless(mappedName="TestService")
public class TestServiceBean implements TestService {

    @PersistenceContext(unitName = "foo")
    private EntityManager enityManager;

    @Override
    public Long create(String foo) {
        TestEntity entity = new TestEntity();
        entity.setFoo(foo);
        enityManager.persist(entity);
        return entity.getId();
    }

    @Override
    public String update(Long id, String foo) {
        TestEntity entity = enityManager.find(TestEntity.class, id);
        entity.setFoo(foo);
        return getFoo(id);
    }

    public String getFoo(Long id) {
        return enityManager.find(TestEntity.class, id).getFoo();
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method" or "Web Service > Add Operation")
 
}

The output is:
1-service: foo1
1-direct: foo1
2-service: foo2
2-direct: foo1

-> after the update, the data is not visible outside glassfish (2-direct still gets "foo1")

What is going wrong? As far as I know the default for EJBs is CMT, which should do the commit after leaving the method - and this should update the db, shouldn't it?

Sometimes I do see the updates, though. Or parts of an update (in more complex cases).
[Message sent by forum member 'puce' (puce)]

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