Hibernate Framework Hibernate Performance Tuning 2 — Questions and Answers
Question 1: What does session.clear() do in Hibernate?
- Clears the first-level cache, detaching all managed entities (Correct answer)
- Deletes all rows from the database
- Commits the current transaction
- Closes the session
Correct answer: Clears the first-level cache, detaching all managed entities
session.clear() removes all entities from the Session's first-level cache, releasing their memory without closing the session.
Question 2: Why should session.clear() be called periodically during bulk inserts?
- To prevent OutOfMemoryError by releasing accumulated entities from the first-level cache (Correct answer)
- To commit partial results
- To improve SQL generation speed
- To reset the connection pool
Correct answer: To prevent OutOfMemoryError by releasing accumulated entities from the first-level cache
During bulk operations, the first-level cache accumulates all processed entities in memory; periodically calling clear() prevents heap exhaustion.
Question 3: What does the 'hibernate.order_inserts=true' property do?
- Groups INSERT statements by entity type to improve JDBC batch efficiency (Correct answer)
- Inserts rows in alphabetical order
- Orders inserts by primary key
- Prevents duplicate inserts
Correct answer: Groups INSERT statements by entity type to improve JDBC batch efficiency
Ordering inserts by entity type allows Hibernate to batch together INSERT statements for the same table, maximizing JDBC batch efficiency.
Question 4: What query hint can be used to force Hibernate to use a specific database index?
- query.addQueryHint("indexName") (Correct answer)
- query.useIndex("indexName")
- query.forceIndex("indexName")
- query.hint("indexName")
Correct answer: query.addQueryHint("indexName")
The addQueryHint() method passes a native database index hint to the generated SQL, guiding the query planner to use a specific index.
Question 5: What is a projection query in Hibernate and why is it more efficient?
- A query that selects specific fields instead of full entities, reducing data transfer (Correct answer)
- A query that projects onto multiple tables
- A spatial query for geographic data
- A query that projects results to XML
Correct answer: A query that selects specific fields instead of full entities, reducing data transfer
Projection queries select only the columns needed rather than loading full entity objects, reducing memory usage and data transfer from the database.
Question 6: Which Hibernate feature helps identify slow queries and performance bottlenecks?
- Hibernate Statistics API (session.getSessionFactory().getStatistics()) (Correct answer)
- The Criteria API
- Hibernate Validator
- The SchemaExport tool
Correct answer: Hibernate Statistics API (session.getSessionFactory().getStatistics())
The Statistics API tracks query execution counts, timings, cache hit ratios, and other metrics to help identify performance issues.
What does session.clear() do in Hibernate?