Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

optimize read-only query in stateless session bean

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make a query returend by a session bean READ-ONLY for optimization:

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void queryReadOnly(){

// make query..
List<User> users = entityManager.createNamedQuery(..).getResultList();


// proof if optimization worked
for(User u: users){
log.info("entityManager.contains 'Entity': {0}", entityManager.contains(u));

}

}

But I get for all my entities 'true' which means all entities are managed - is that not paradox, I have thought using TransactionAttributeType.NOT_SUPPORTED does not use entityManager first-cache.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't follow. Why would loading entities in a method which doesn't support transactions make things read only? Am I missing something? (And what "optimization" are you trying to get by making these entities read only?)
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know,
according to hibernate spec there are some optimizations when using something like

query.readOnly(true)

or

@org.hibernate.annotations.Immutable on a class or collection.


I have a large query which contains only entities for read-only (reporting things), so I want to bypass the overhead of entitymanager:
I dont want that the persistence provider has the overhead to manage my entities as they are only read-only.

Am I false with that?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing in JPA for this, as far as I am aware. Hibernate supports read only queries, which changes the update checking logic (which would make them slightly more efficient). Its a specific query hint you use for this though.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thanks, I use aggregate queries and find out that with that I bypass the entityManager 1l-cache.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic