Hibernate Performance Configuration Tips

Practical

We can check the hibernate behavior via different methods,

  1. Profiler on DAO/Session.query() methods.
  2. VisualVM for heap usage.
  3. Hibernate JMX.
  4. RDBMS monitoring tools.

Analyzing Hibernate

  1. Using properties like show_sql and format_sql.
  2. Log4J configurations,
    1. hibernate.SQL->DEBUG
    2. hibernate.type->TRACE
  3. Use P6Spy/Log4JDBC on JDBC connection.

Large Collections

  1. Using @BatchSize(size=100) on entities and attributes.
  2. Use fetch join
  3. Extra lazy collection fetching
    1. @OneToMany(fetch=FetchType.Lazy)
    2. @LazyCollection(LazyCollectionOption.EXTRA)
  4. Use “hibernate.jdbc.batch_size” / “hibernate.order_inserts” / “hibernate.order_updates”
  5. Use StatelessSession
    1. StatelessSession stateless = session.getSessionFactory().openStatelessSession();

Query Hints

  1. Speed up read-only service calls
    • Query query = session.createQuery(“——-”);
    • setReadOnly(true);
  2. Query Set Hints
    • setHint(“org.hibernate.fetchSize”, 50);
    • setHint(“org.hibernate.cacheable”, true);
  3. Never use second level cache.

Large Updates

  1. Use VERSION keyword for optimistic concurrency.
  2. Entities are not always necessary,
    1. createQuery(“UPDATE User SET active=false”).executeUpdate();
  3. Consider use of stored procedures.

General

  • Data and schema outlive your application
  • Good indexes makes huge differences
  • Stored procedures are not evil
  • Do not let hibernate to dictate your schema