You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@geronimo.apache.org by David Jencks <da...@yahoo.com> on 2012/10/04 01:47:24 UTC

Can jpa queries be cached?

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:

@Stateless
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
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 tx?

advice really appreciated :-)

thanks
david jencks



Re: Can jpa queries be cached?

Posted by Mark Struberg <st...@yahoo.de>.
OpenJPA has the ability to cache both.

LieGrue,
struy




----- Original Message -----
> From: Romain Manni-Bucau <rm...@gmail.com>
> To: dev@openejb.apache.org
> Cc: 
> Sent: Thursday, October 4, 2012 7:18 AM
> Subject: Re: Can jpa queries be cached?
> 
> Hi,
> 
> With a jta persistence unit you shouldnt manage these things.
> 
> Then the cache of queries should be configured for sql queries. Jpa queries
> are not so costly.
> 
> - Romain
> Le 4 oct. 2012 01:47, "David Jencks" <da...@yahoo.com> a 
> écrit :
> 
>>  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:
>> 
>>  @Stateless
>>  @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
>>  @TransactionManagement(TransactionManagementType.CONTAINER)
>>  @TransactionAttribute(TransactionAttributeType.REQUIRED)
>>  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 tx?
>> 
>>  advice really appreciated :-)
>> 
>>  thanks
>>  david jencks
>> 
>> 
>> 
> 

Re: Can jpa queries be cached?

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi,

With a jta persistence unit you shouldnt manage these things.

Then the cache of queries should be configured for sql queries. Jpa queries
are not so costly.

- Romain
Le 4 oct. 2012 01:47, "David Jencks" <da...@yahoo.com> a écrit :

> 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:
>
> @Stateless
> @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
> @TransactionManagement(TransactionManagementType.CONTAINER)
> @TransactionAttribute(TransactionAttributeType.REQUIRED)
> 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 tx?
>
> advice really appreciated :-)
>
> thanks
> david jencks
>
>
>