I recently opened a topic at stackoverflow:
In a JPA (eclipselink) application I have an ejb business action which must execute a native query (oracle) before going on with non-native jpa calls. That native call is some kind of reporting, it does not select or even change any of my entities in database.
However eclipseLink marks the transaction dirty, and the following non-native jpa calls do not use the shared cache any more. This behavior is documented in the eclipseLink userguide, see here, and thus its expected.
But is there a way of telling eclipseLink not to mark the transaction dirty, eg some hint in the native query? I need the following jpa-queries to use the shared cache.
The code where the native query is created is part of an interceptor like this:
public class MyInterceptor {
@PersistenceContext( name="xxxx" )
private EntityManager em;
@AroundInvoke
Object init( InvocationContext context ) throws Exception {
// the result of the native query is a plain date resulting
// from a non-trivial query of course
Date now = (Date)em.createNativeQuery( "select sysdate from dual" )
.getSingleResult();
return context.proceed();
}
}
The rest of the business logic is encapsulated in a ejb eg
@Stateless
public class BusinessEJB {
@PersistenceContext(name="xxxx")
private EntityManager em;
@Interceptors( { MyInterceptor.class } )
public List<Result> find() {
// Result is quite complex with lots of related detail objects
// which I would like to fetch from the shared cache.
// The entity Result is annotated @Cacheable( false ) and the
// detail entities are @Cacheable( true ). persistence.xml contains
// <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
return em.createNamedQuery("Result.findAll",Result.class)
.getResultList();
}
}
Whenever I remove the interceptor everything is fine. When we enable the query log in persistence.xml we see no queries on the detail objects. But adding the interceptor results in lots of database calls fetching the details.
I use jee6 on glassfish 3.1.2.2 with eclipselink 2.3.2