0

I have a Java application that use Hibernate OGM framework and Mongo database. When I retrieve a lot of data, the application becomes very slow and one of the bottlenecks is Hibernate.

I read that retrive data in a read-only context improve performance a lot. How can I do that?

I had try with method calls like this:

entityManager.lock(entity, LockModeType.READ);

or:

entityManager.createNativeQuery(query, entity)
.setLockMode(LockModeType.READ)
.getResultList();

but seems there are not supported.

I can not wait 20 minutes for a few megabytes. Please help! Every suggestion is appreciated

In my case I have a tree of one to many associations. The query is on the root, where fetch is set to LAZY. Then i call in a thread the Hibernate.initialize(proxy) method on the second level where the recovery is EAGER, to the deeper level.

1 Answers1

0

With the entity manager you can use query hints:

em.createNativeQuery( query )
  .setHint( "org.hibernate.readOnly", true )
  .getResultList();

or you can use the session:

em.unwrap( OgmSession.class )
  .createNativeQuery( query )
  .setReadOnly( true )
  .list();

Note that if you can add more details about the nature of the problems and if it is an issue in Hibernate OGM, we might create a patch for it.

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • Thank you very much. I've tried both ways, but waiting times are still long. Maybe I'm wrong to do that? I made query on the highest table in the chain of associations. –  Aug 10 '17 at 13:33
  • If you can create a test for us to run we can help you more. Your question was about enabling readOnly mode, though, so I think the answer is correct. If your question is too generic for stackoverflow, you can use the hibernate forum: https://forum.hibernate.org/viewforum.php?f=31 – Davide D'Alto Aug 11 '17 at 09:33