SlideShare una empresa de Scribd logo
1 de 47
Descargar para leer sin conexión
Hibernate ORM:
Tips, Tricks, and
Performance Techniques
Brett Meyer
Senior Software Engineer
Hibernate ORM, Red Hat
Brett Meyer
• Hibernate ORM
– ORM 4 & 5 development
– Hibernate OSGi
– Developer community engagement
– Red Hat enterprise support, Hibernate engineering lead

• Other contributions
– Apache Camel
– Infinispan

• Contact me
– @brettemeyer or +brettmeyer
– Freenode #hibernate or #hibernate-dev (brmeyer)
github.com/brmeyer
/HibernateDemos

slideshare.net/brmeyer
ORM? JPA?
• ORM: Object/Relational Mapping
– Persistence: Data objects outlive the JVM app
– Maps Java POJOs to relational databases
– Supports OO concepts: inheritance, object identity, etc.
– Navigate data by walking the object graph, not the explicit
relational model

• JPA: Java Persistence API
• Hibernate ORM provides its own native API, in
addition to full JPA support
• Annotations and XML
Overview
•
•
•
•
•
•
•
•

Fetching Strategies
2nd Level Entity Cache
Query Cache
Cache Management
Bytecode Enhancement
Hibernate Search
Misc. Tips
Q&A
Caveats
• No “one strategy to rule them all”
• Varies greatly between apps
• Important to understand concepts, then
apply as necessary
• Does not replace database tuning!
First, the antithesis:
public User get(String username) {
final Session session = openSession();
session.getTransaction().begin();
final User user = (User) session.get( User.class, username );
session.getTransaction().commit();
return user;
}
public boolean login(String username) {
return get(username) != null;
}

Clean? Yes. But...
EAGER Demo
• Prototypes start “simple”
– EAGER
– No caching
– Overuse of the kitchen sink

• DAO looks clean
• Then you see the SQL logs
Fetching Strategies
Fetching Strategies
• By far, the most frequent mistake
• Also the most costly
• 2 concepts:
– WHEN (fetch timing)
– HOW (fetch style)
WHEN (fetch timing)
Fetch Timing: EAGER
•
•
•
•

Immediate
Can be convenient (in some ways)
Significantly increases payload
Enormous amount of unnecessary
fetches for deep association tree
Fetch Timing: LAZY
• Fetched when first accessed
• Collections
– LAZY by default
– Utilizes Hibernate's internal concept of “persistent
collections”
– DEMO

• Single attribute (basic)
– Requires bytecode enhancement
– Not typically used nor beneficial
Fetch Timing: LAZY (cont'd)
• Single (ToOne) associations
– Fetched when accessed
– Proxy
• Default
• Fetched when accessed (except ID)
• Handled internally by Hibernate

– No-proxy
• Fetched when accessed (including ID)
• No visible proxy
• Requires buildtime bytecode enhancement
Fetch Timing: EXTRA LAZY
•
•
•
•

Collections only
Fetch individual elements as accessed
Does not fetch entire collection
DEMO
HOW (fetch style)
Fetch Style: JOIN
• Join fetch (left/outer join)
• Great for ToOne associations
• Multiple collection joins
– Possible for non-bags
– Warning: Cartesian product! SELECT is
normally faster

• DEMO
Fetch Style: SELECT
• Follow-up selects
• Default style for collections (avoids
cartesian products)
• Can result in 1+N selects (fetch per
collection entry)
• DEMO
Fetch Style: BATCH
• Fetches multiple entries when one is
accessed
• Configurable on both class and collection
levels (“batch-size”)
• Simple select and list of keys
• Multiple algorithms (new as of 4.2)
• Determines # of entries to fetch, based on
# of provided keys
Fetch Style: BATCH (cont'd)
• Legacy: pre-determined sizes, rounded
down
– batch-size==25, 24 elements in collection
– Fetches -> 12, 10, 2

• Padded: same as Legacy, size rounded up
• Dynamic: builds SQL for the # of keys,
limited by “batch-size”
• DEMO
Fetch Style: SUBSELECT
• Follow-up select
• Fetches all collection entries when
accessed for the 1st time
• Original root entry select used as
subselect
• Performance depends on the DB itself
• DEMO
Fetch Style: PROFILE
• named profile defining fetch styles
• “activated” through the Session
@Entity
@FetchProfile(name = "customer-with-orders", fetchOverrides = {
@FetchProfile.FetchOverride(entity = Customer.class,
association = "orders", mode = FetchMode.JOIN)
})
public class Customer {
...
@OneToMany
private Set<Order> orders;
...
}
Session session = ...;
session.enableFetchProfile( "customer-with-orders" );
Customer customer = (Customer) session.get(
Customer.class, customerId );
Fetch Style Tips
• Best to leave defaults in mappings
• Define the style at the query level
• Granular control
nd

2 Level Entity
Cache (2LC)
2LC
• differs from the 1st level (Session)
• SessionFactory level (JVM)
• can scale horizontally on commodity
hardware (clustered)
• multiple providers
• currently focus on Infinispan (JBoss) &
EHCache (Terracotta)
2LC (cont'd)
• disabled by default
– can enable globally (not recommended)
– enable for individual entities and
collections
– configurable at the Session level w/
CacheMode

• cache all properties (default) or only
non-lazy
2LC (cont'd)
• Since JVM-level, concurrency is an issue
• Concurrency strategies
– Read only: simplest and optimal
– Read-write
• Supports updates
• Should not use for serializable transaction isolation

– Nonstrict-read-write
• If updates are occasional and unlikely to collide
• No strict transaction isolation

– Transactional:
• Fully transactional cache providers (ie, Infinispan and EHCache)
• JTA required
2LC (cont'd)
• DEMO
Query Cache
Query Cache
• Caches query result sets
• Repetitive queries with identical params
• Different from an entity cache
– Does not maintain entity state
– Holds on to sets of identifiers

• If used, best in conjunction with 2LC
Query Cache (cont'd)
• Disabled by default
• Many applications do not gain anything
• Queries invalidated upon relevant
updates
• Increases overhead by some: tracks
when to invalidate based on commits
• DEMO
Cache Management
Cache Management
• Entities stored in Session cache when saved,
updated, or retrieved
• Session#flush syncs all cached with DB
– Minimize usage

• Manage memory:
– Session#evict(entity) when no longer needed
– Session#clear for all
– Important for large dataset handling

• Same for 2LC, but methods on
SessionFactory#getCache
Bytecode
Enhancement
Bytecode Enhancement
• Not just for no-proxy, ToOne laziness
• Reduced load on PersistenceContext
– EntityEntry
• Internally provides state & Session association
• “Heavy” operation and typically a hotspot (multiple Maps)

– ManagedEntity
• Reduced memory and CPU loads
• Entities maintain their own state with bytecode enhancement
• (ManagedEntity)entity.$$_hibernate_getEntityEntry();

– 3 ways:
• Entity implements ManagedEntity and maintains the association
• Buildtime instrumentation (Ant and recently Gradle/Maven)
• Runtime instrumentation
Bytecode (cont'd)
• dirtiness checking
– Legacy: “dirtiness” determined by deep comparison of
entity state
– Enhancement: entity tracks its own dirtiness
– Simply check the flag (no deep comparison)
– Already mentioned…
• Minimize amount of flushes to begin with
• Minimize the # of entities in the cache -- evict when possible

• Many additional bytecode improvements planned
Hibernate Search
Hibernate Search
• Full-text search on the DB
– Bad performance
– CPU/IO overhead

• Offload full-text queries to Hibernate
Search engine
– Fully indexed
– Horizontally scalable

• Based on Apache Lucene
Hibernate Search (cont'd)
• Annotate entities with @Indexed
• Annotate properties with @Field
– Index the text: index=Index.YES
– “Analyze” the text: analyze=Analyze.YES
•
•
•
•

Lucene analyzer
Chunks sentences into words
Lowercase all of them
Exclude common words (“a”, “the”)

• Combo of indexing and analysis == performant
full-text searches!
Misc. Tips
Misc. Tips
• Easy to overlook unintended fetches
– Ex: Fully implemented toString with all
associations (fetches everything simply for
logging)

• Use @Immutable when possible
– Excludes entity from dirtiness checks
– Other internal optimizations
Misc. Tips (cont'd)
• Use Bag or List for inverse collections, not Set
– Collection#add & #addAll always return true (don't
need to check for pre-existing values)
– I.e., can add a value without fetching the collection
Parent p = (Parent) session.load(Parent.class, id);
Child c = new Child();
c.setParent(p);
p.addChild(c);
...
// does *not* fetch the collection
p.getChildren().add(c);
Misc. Tips (cont'd)
• One-shot delete (non-inverse collections)
– Scenario: 20 elements, need to delete 18 & add 3
– Default: Hibernate would delete the 18 one-byone, then add the 3
– Instead:
• Discard the entire collection (deletes all elements)
• Save a new collection with 5 elements
• 1 bulk delete SQL and 5 inserts

– Important concept for large amounts of data
How to Help:
hibernate.org
/orm/contribute
QUESTIONS?
•
•
•
•

Q&A
#hibernate or #hibernate-dev (brmeyer)
@brettemeyer
+brettmeyer

Más contenido relacionado

La actualidad más candente

API Testing Using REST Assured with TestNG
API Testing Using REST Assured with TestNGAPI Testing Using REST Assured with TestNG
API Testing Using REST Assured with TestNGSiddharth Sharma
 
Introduction to Robot Framework – Exove
Introduction to Robot Framework – ExoveIntroduction to Robot Framework – Exove
Introduction to Robot Framework – ExoveExove
 
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesVMware Tanzu
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcachedJurriaan Persyn
 
Introduction to JMeter
Introduction to JMeterIntroduction to JMeter
Introduction to JMeterGalih Lasahido
 
Presto At Treasure Data
Presto At Treasure DataPresto At Treasure Data
Presto At Treasure DataTaro L. Saito
 
Cloudera Impala: A Modern SQL Engine for Apache Hadoop
Cloudera Impala: A Modern SQL Engine for Apache HadoopCloudera Impala: A Modern SQL Engine for Apache Hadoop
Cloudera Impala: A Modern SQL Engine for Apache HadoopCloudera, Inc.
 
JBoss Application Server 7
JBoss Application Server 7JBoss Application Server 7
JBoss Application Server 7Ray Ploski
 
Unit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma introUnit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma introMaurice De Beijer [MVP]
 
Writing Scalable Software in Java
Writing Scalable Software in JavaWriting Scalable Software in Java
Writing Scalable Software in JavaRuben Badaró
 
Software architecture for high traffic website
Software architecture for high traffic websiteSoftware architecture for high traffic website
Software architecture for high traffic websiteTung Nguyen Thanh
 
Apache Tez - A New Chapter in Hadoop Data Processing
Apache Tez - A New Chapter in Hadoop Data ProcessingApache Tez - A New Chapter in Hadoop Data Processing
Apache Tez - A New Chapter in Hadoop Data ProcessingDataWorks Summit
 
Introduction to Storm
Introduction to Storm Introduction to Storm
Introduction to Storm Chandler Huang
 
Spring Framework Petclinic sample application
Spring Framework Petclinic sample applicationSpring Framework Petclinic sample application
Spring Framework Petclinic sample applicationAntoine Rey
 
Rest API Automation with REST Assured
Rest API Automation with REST AssuredRest API Automation with REST Assured
Rest API Automation with REST AssuredTO THE NEW Pvt. Ltd.
 
(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막
(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막
(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막Jay Park
 
Hybrid Apache Spark Architecture with YARN and Kubernetes
Hybrid Apache Spark Architecture with YARN and KubernetesHybrid Apache Spark Architecture with YARN and Kubernetes
Hybrid Apache Spark Architecture with YARN and KubernetesDatabricks
 
Apache Hive Tutorial
Apache Hive TutorialApache Hive Tutorial
Apache Hive TutorialSandeep Patil
 

La actualidad más candente (20)

API Testing Using REST Assured with TestNG
API Testing Using REST Assured with TestNGAPI Testing Using REST Assured with TestNG
API Testing Using REST Assured with TestNG
 
Introduction to Robot Framework – Exove
Introduction to Robot Framework – ExoveIntroduction to Robot Framework – Exove
Introduction to Robot Framework – Exove
 
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutes
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcached
 
Rest assured
Rest assuredRest assured
Rest assured
 
Introduction to JMeter
Introduction to JMeterIntroduction to JMeter
Introduction to JMeter
 
Presto At Treasure Data
Presto At Treasure DataPresto At Treasure Data
Presto At Treasure Data
 
Cloudera Impala: A Modern SQL Engine for Apache Hadoop
Cloudera Impala: A Modern SQL Engine for Apache HadoopCloudera Impala: A Modern SQL Engine for Apache Hadoop
Cloudera Impala: A Modern SQL Engine for Apache Hadoop
 
JBoss Application Server 7
JBoss Application Server 7JBoss Application Server 7
JBoss Application Server 7
 
Unit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma introUnit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma intro
 
Writing Scalable Software in Java
Writing Scalable Software in JavaWriting Scalable Software in Java
Writing Scalable Software in Java
 
Software architecture for high traffic website
Software architecture for high traffic websiteSoftware architecture for high traffic website
Software architecture for high traffic website
 
Apache Tez - A New Chapter in Hadoop Data Processing
Apache Tez - A New Chapter in Hadoop Data ProcessingApache Tez - A New Chapter in Hadoop Data Processing
Apache Tez - A New Chapter in Hadoop Data Processing
 
Introduction to Storm
Introduction to Storm Introduction to Storm
Introduction to Storm
 
Api testing
Api testingApi testing
Api testing
 
Spring Framework Petclinic sample application
Spring Framework Petclinic sample applicationSpring Framework Petclinic sample application
Spring Framework Petclinic sample application
 
Rest API Automation with REST Assured
Rest API Automation with REST AssuredRest API Automation with REST Assured
Rest API Automation with REST Assured
 
(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막
(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막
(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막
 
Hybrid Apache Spark Architecture with YARN and Kubernetes
Hybrid Apache Spark Architecture with YARN and KubernetesHybrid Apache Spark Architecture with YARN and Kubernetes
Hybrid Apache Spark Architecture with YARN and Kubernetes
 
Apache Hive Tutorial
Apache Hive TutorialApache Hive Tutorial
Apache Hive Tutorial
 

Similar a Hibernate ORM: Tips, Tricks, and Performance Techniques

hibernateormfeatures-140223193044-phpapp02.pdf
hibernateormfeatures-140223193044-phpapp02.pdfhibernateormfeatures-140223193044-phpapp02.pdf
hibernateormfeatures-140223193044-phpapp02.pdfPatiento Del Mar
 
How to Write the Fastest JSON Parser/Writer in the World
How to Write the Fastest JSON Parser/Writer in the WorldHow to Write the Fastest JSON Parser/Writer in the World
How to Write the Fastest JSON Parser/Writer in the WorldMilo Yip
 
Yapc10 Cdt World Domination
Yapc10   Cdt World DominationYapc10   Cdt World Domination
Yapc10 Cdt World DominationcPanel
 
Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Ryan Cuprak
 
Performance and Abstractions
Performance and AbstractionsPerformance and Abstractions
Performance and AbstractionsMetosin Oy
 
Black Hat: XML Out-Of-Band Data Retrieval
Black Hat: XML Out-Of-Band Data RetrievalBlack Hat: XML Out-Of-Band Data Retrieval
Black Hat: XML Out-Of-Band Data Retrievalqqlan
 
Alfresco Business Reporting - Tech Talk Live 20130501
Alfresco Business Reporting - Tech Talk Live 20130501Alfresco Business Reporting - Tech Talk Live 20130501
Alfresco Business Reporting - Tech Talk Live 20130501Tjarda Peelen
 
ITB2017 - Slaying the ORM dragons with cborm
ITB2017 - Slaying the ORM dragons with cbormITB2017 - Slaying the ORM dragons with cborm
ITB2017 - Slaying the ORM dragons with cbormOrtus Solutions, Corp
 
Tuenti Release Workflow
Tuenti Release WorkflowTuenti Release Workflow
Tuenti Release WorkflowTuenti
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Codersebbe
 
What-is-Laravel-23-August-2017.pptx
What-is-Laravel-23-August-2017.pptxWhat-is-Laravel-23-August-2017.pptx
What-is-Laravel-23-August-2017.pptxAbhijeetKumar456867
 
[Russia] Bugs -> max, time &lt;= T
[Russia] Bugs -> max, time &lt;= T[Russia] Bugs -> max, time &lt;= T
[Russia] Bugs -> max, time &lt;= TOWASP EEE
 
Introduction of vertical crawler
Introduction of vertical crawlerIntroduction of vertical crawler
Introduction of vertical crawlerJinglun Li
 
Find maximum bugs in limited time
Find maximum bugs in limited timeFind maximum bugs in limited time
Find maximum bugs in limited timebeched
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage SystemsSATOSHI TAGOMORI
 
Killing Shark-Riding Dinosaurs with ORM
Killing Shark-Riding Dinosaurs with ORMKilling Shark-Riding Dinosaurs with ORM
Killing Shark-Riding Dinosaurs with ORMOrtus Solutions, Corp
 
Apache Solr - Enterprise search platform
Apache Solr - Enterprise search platformApache Solr - Enterprise search platform
Apache Solr - Enterprise search platformTommaso Teofili
 
Scaling with swagger
Scaling with swaggerScaling with swagger
Scaling with swaggerTony Tam
 
Introduction to libre « fulltext » technology
Introduction to libre « fulltext » technologyIntroduction to libre « fulltext » technology
Introduction to libre « fulltext » technologyRobert Viseur
 

Similar a Hibernate ORM: Tips, Tricks, and Performance Techniques (20)

hibernateormfeatures-140223193044-phpapp02.pdf
hibernateormfeatures-140223193044-phpapp02.pdfhibernateormfeatures-140223193044-phpapp02.pdf
hibernateormfeatures-140223193044-phpapp02.pdf
 
How to Write the Fastest JSON Parser/Writer in the World
How to Write the Fastest JSON Parser/Writer in the WorldHow to Write the Fastest JSON Parser/Writer in the World
How to Write the Fastest JSON Parser/Writer in the World
 
Yapc10 Cdt World Domination
Yapc10   Cdt World DominationYapc10   Cdt World Domination
Yapc10 Cdt World Domination
 
Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)
 
Performance and Abstractions
Performance and AbstractionsPerformance and Abstractions
Performance and Abstractions
 
Black Hat: XML Out-Of-Band Data Retrieval
Black Hat: XML Out-Of-Band Data RetrievalBlack Hat: XML Out-Of-Band Data Retrieval
Black Hat: XML Out-Of-Band Data Retrieval
 
Alfresco Business Reporting - Tech Talk Live 20130501
Alfresco Business Reporting - Tech Talk Live 20130501Alfresco Business Reporting - Tech Talk Live 20130501
Alfresco Business Reporting - Tech Talk Live 20130501
 
ITB2017 - Slaying the ORM dragons with cborm
ITB2017 - Slaying the ORM dragons with cbormITB2017 - Slaying the ORM dragons with cborm
ITB2017 - Slaying the ORM dragons with cborm
 
Tuenti Release Workflow
Tuenti Release WorkflowTuenti Release Workflow
Tuenti Release Workflow
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Code
 
Introduction_to_Python.pptx
Introduction_to_Python.pptxIntroduction_to_Python.pptx
Introduction_to_Python.pptx
 
What-is-Laravel-23-August-2017.pptx
What-is-Laravel-23-August-2017.pptxWhat-is-Laravel-23-August-2017.pptx
What-is-Laravel-23-August-2017.pptx
 
[Russia] Bugs -> max, time &lt;= T
[Russia] Bugs -> max, time &lt;= T[Russia] Bugs -> max, time &lt;= T
[Russia] Bugs -> max, time &lt;= T
 
Introduction of vertical crawler
Introduction of vertical crawlerIntroduction of vertical crawler
Introduction of vertical crawler
 
Find maximum bugs in limited time
Find maximum bugs in limited timeFind maximum bugs in limited time
Find maximum bugs in limited time
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage Systems
 
Killing Shark-Riding Dinosaurs with ORM
Killing Shark-Riding Dinosaurs with ORMKilling Shark-Riding Dinosaurs with ORM
Killing Shark-Riding Dinosaurs with ORM
 
Apache Solr - Enterprise search platform
Apache Solr - Enterprise search platformApache Solr - Enterprise search platform
Apache Solr - Enterprise search platform
 
Scaling with swagger
Scaling with swaggerScaling with swagger
Scaling with swagger
 
Introduction to libre « fulltext » technology
Introduction to libre « fulltext » technologyIntroduction to libre « fulltext » technology
Introduction to libre « fulltext » technology
 

Último

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Último (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Hibernate ORM: Tips, Tricks, and Performance Techniques

  • 1.
  • 2. Hibernate ORM: Tips, Tricks, and Performance Techniques Brett Meyer Senior Software Engineer Hibernate ORM, Red Hat
  • 3. Brett Meyer • Hibernate ORM – ORM 4 & 5 development – Hibernate OSGi – Developer community engagement – Red Hat enterprise support, Hibernate engineering lead • Other contributions – Apache Camel – Infinispan • Contact me – @brettemeyer or +brettmeyer – Freenode #hibernate or #hibernate-dev (brmeyer)
  • 5. ORM? JPA? • ORM: Object/Relational Mapping – Persistence: Data objects outlive the JVM app – Maps Java POJOs to relational databases – Supports OO concepts: inheritance, object identity, etc. – Navigate data by walking the object graph, not the explicit relational model • JPA: Java Persistence API • Hibernate ORM provides its own native API, in addition to full JPA support • Annotations and XML
  • 6. Overview • • • • • • • • Fetching Strategies 2nd Level Entity Cache Query Cache Cache Management Bytecode Enhancement Hibernate Search Misc. Tips Q&A
  • 7. Caveats • No “one strategy to rule them all” • Varies greatly between apps • Important to understand concepts, then apply as necessary • Does not replace database tuning!
  • 8. First, the antithesis: public User get(String username) { final Session session = openSession(); session.getTransaction().begin(); final User user = (User) session.get( User.class, username ); session.getTransaction().commit(); return user; } public boolean login(String username) { return get(username) != null; } Clean? Yes. But...
  • 9. EAGER Demo • Prototypes start “simple” – EAGER – No caching – Overuse of the kitchen sink • DAO looks clean • Then you see the SQL logs
  • 11. Fetching Strategies • By far, the most frequent mistake • Also the most costly • 2 concepts: – WHEN (fetch timing) – HOW (fetch style)
  • 13. Fetch Timing: EAGER • • • • Immediate Can be convenient (in some ways) Significantly increases payload Enormous amount of unnecessary fetches for deep association tree
  • 14. Fetch Timing: LAZY • Fetched when first accessed • Collections – LAZY by default – Utilizes Hibernate's internal concept of “persistent collections” – DEMO • Single attribute (basic) – Requires bytecode enhancement – Not typically used nor beneficial
  • 15. Fetch Timing: LAZY (cont'd) • Single (ToOne) associations – Fetched when accessed – Proxy • Default • Fetched when accessed (except ID) • Handled internally by Hibernate – No-proxy • Fetched when accessed (including ID) • No visible proxy • Requires buildtime bytecode enhancement
  • 16. Fetch Timing: EXTRA LAZY • • • • Collections only Fetch individual elements as accessed Does not fetch entire collection DEMO
  • 18. Fetch Style: JOIN • Join fetch (left/outer join) • Great for ToOne associations • Multiple collection joins – Possible for non-bags – Warning: Cartesian product! SELECT is normally faster • DEMO
  • 19. Fetch Style: SELECT • Follow-up selects • Default style for collections (avoids cartesian products) • Can result in 1+N selects (fetch per collection entry) • DEMO
  • 20. Fetch Style: BATCH • Fetches multiple entries when one is accessed • Configurable on both class and collection levels (“batch-size”) • Simple select and list of keys • Multiple algorithms (new as of 4.2) • Determines # of entries to fetch, based on # of provided keys
  • 21. Fetch Style: BATCH (cont'd) • Legacy: pre-determined sizes, rounded down – batch-size==25, 24 elements in collection – Fetches -> 12, 10, 2 • Padded: same as Legacy, size rounded up • Dynamic: builds SQL for the # of keys, limited by “batch-size” • DEMO
  • 22. Fetch Style: SUBSELECT • Follow-up select • Fetches all collection entries when accessed for the 1st time • Original root entry select used as subselect • Performance depends on the DB itself • DEMO
  • 23. Fetch Style: PROFILE • named profile defining fetch styles • “activated” through the Session
  • 24. @Entity @FetchProfile(name = "customer-with-orders", fetchOverrides = { @FetchProfile.FetchOverride(entity = Customer.class, association = "orders", mode = FetchMode.JOIN) }) public class Customer { ... @OneToMany private Set<Order> orders; ... } Session session = ...; session.enableFetchProfile( "customer-with-orders" ); Customer customer = (Customer) session.get( Customer.class, customerId );
  • 25. Fetch Style Tips • Best to leave defaults in mappings • Define the style at the query level • Granular control
  • 27. 2LC • differs from the 1st level (Session) • SessionFactory level (JVM) • can scale horizontally on commodity hardware (clustered) • multiple providers • currently focus on Infinispan (JBoss) & EHCache (Terracotta)
  • 28. 2LC (cont'd) • disabled by default – can enable globally (not recommended) – enable for individual entities and collections – configurable at the Session level w/ CacheMode • cache all properties (default) or only non-lazy
  • 29. 2LC (cont'd) • Since JVM-level, concurrency is an issue • Concurrency strategies – Read only: simplest and optimal – Read-write • Supports updates • Should not use for serializable transaction isolation – Nonstrict-read-write • If updates are occasional and unlikely to collide • No strict transaction isolation – Transactional: • Fully transactional cache providers (ie, Infinispan and EHCache) • JTA required
  • 32. Query Cache • Caches query result sets • Repetitive queries with identical params • Different from an entity cache – Does not maintain entity state – Holds on to sets of identifiers • If used, best in conjunction with 2LC
  • 33. Query Cache (cont'd) • Disabled by default • Many applications do not gain anything • Queries invalidated upon relevant updates • Increases overhead by some: tracks when to invalidate based on commits • DEMO
  • 35. Cache Management • Entities stored in Session cache when saved, updated, or retrieved • Session#flush syncs all cached with DB – Minimize usage • Manage memory: – Session#evict(entity) when no longer needed – Session#clear for all – Important for large dataset handling • Same for 2LC, but methods on SessionFactory#getCache
  • 37. Bytecode Enhancement • Not just for no-proxy, ToOne laziness • Reduced load on PersistenceContext – EntityEntry • Internally provides state & Session association • “Heavy” operation and typically a hotspot (multiple Maps) – ManagedEntity • Reduced memory and CPU loads • Entities maintain their own state with bytecode enhancement • (ManagedEntity)entity.$$_hibernate_getEntityEntry(); – 3 ways: • Entity implements ManagedEntity and maintains the association • Buildtime instrumentation (Ant and recently Gradle/Maven) • Runtime instrumentation
  • 38. Bytecode (cont'd) • dirtiness checking – Legacy: “dirtiness” determined by deep comparison of entity state – Enhancement: entity tracks its own dirtiness – Simply check the flag (no deep comparison) – Already mentioned… • Minimize amount of flushes to begin with • Minimize the # of entities in the cache -- evict when possible • Many additional bytecode improvements planned
  • 40. Hibernate Search • Full-text search on the DB – Bad performance – CPU/IO overhead • Offload full-text queries to Hibernate Search engine – Fully indexed – Horizontally scalable • Based on Apache Lucene
  • 41. Hibernate Search (cont'd) • Annotate entities with @Indexed • Annotate properties with @Field – Index the text: index=Index.YES – “Analyze” the text: analyze=Analyze.YES • • • • Lucene analyzer Chunks sentences into words Lowercase all of them Exclude common words (“a”, “the”) • Combo of indexing and analysis == performant full-text searches!
  • 43. Misc. Tips • Easy to overlook unintended fetches – Ex: Fully implemented toString with all associations (fetches everything simply for logging) • Use @Immutable when possible – Excludes entity from dirtiness checks – Other internal optimizations
  • 44. Misc. Tips (cont'd) • Use Bag or List for inverse collections, not Set – Collection#add & #addAll always return true (don't need to check for pre-existing values) – I.e., can add a value without fetching the collection Parent p = (Parent) session.load(Parent.class, id); Child c = new Child(); c.setParent(p); p.addChild(c); ... // does *not* fetch the collection p.getChildren().add(c);
  • 45. Misc. Tips (cont'd) • One-shot delete (non-inverse collections) – Scenario: 20 elements, need to delete 18 & add 3 – Default: Hibernate would delete the 18 one-byone, then add the 3 – Instead: • Discard the entire collection (deletes all elements) • Save a new collection with 5 elements • 1 bulk delete SQL and 5 inserts – Important concept for large amounts of data