geronimo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Jencks <>
Subject Can jpa queries be cached?
Date Wed, 03 Oct 2012 23:47:24 GMT
I see that both geronimo and openejb have jta or non-tx jpa query wrappers that close the entity
manager after the query executes.  This pretty effectively prevents the query from being re-executed.

Despite recalling having written the geronimo version, I can't remember why this is necessary.

We've found someone who is doing something like this:

public class Foo implements FooLocal 
    @PersistenceContext(unitName="FooJpa", type = PersistenceContextType.TRANSACTION) private
EntityManager em;
    private Query findBars;
    @PostConstruct void postConstruct()
            findBars = em.createNamedQuery("findBars");

    public Collection<Bar> getAllBars() {
      return findBars.getResultList();

The second time getAllBars() is called, the em is closed.

There are some other errors in their code which might possibly be causing this, but I think
what is happening is that the postconstruct is executing outside a jta transaction, so that
the findBars query is a wrapped query that closes itself after the query executes, even though
it is executing in a jta environment.

Should our wrapper only close the em if it is executed (rather than created) outside a jta

advice really appreciated :-)

david jencks

View raw message