SlideShare a Scribd company logo
Osama Oransa
osama-oransa.blogspot.com
 Use the proper loading strategy from either lazy
loading or eager data loading according to our
data size and usage; for small data volume, eager
loading always make sense.
 Query results pagination is also an important JPA
feature. The importance of this feature is to avoid
memory issues when the returned data is huge.
 The JPA provides two methods to control this
pagination: setMaxResults(int maxResult) and
setFirstResult(int startPosition) in the Query
object.
 Use data chunking to return the required data
only from the database side, instead of
filtering in the application side using the IN
keyword and adding the list of IDs to the
query.
 Increasing the index pre-allocation size can
speed up the creation of new objects. Also try
to minimize the usage of composite primary
keys for different entities.
 Enabling entity caching is important to
improve the performance.
 Enable the query-level caching for better
performance. One way to do that is by adding
a hint to the named query as follows:
◦ hints={@QueryHint(name="eclipselink.query-
results-cache", value="true")}

Recommended for you

Apache Struts 2 Advance
Apache Struts 2 AdvanceApache Struts 2 Advance
Apache Struts 2 Advance

The document discusses the Struts 2 framework architecture. It explains that the ActionContext stores the ValueStack which contains request data. The ValueStack holds application domain data for an action invocation and serves as the default root object for resolving OGNL expressions. Various tags like Iterator, If, Url are used to work with and retrieve data from the ValueStack. Results like DispatcherResult handles view resolution by forwarding to JSPs. The validation framework uses validators, validation metadata and domain data to validate action properties.

emprovisestruts2
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's

The Java Persistence API discusses about the Java Persistence API's advantages and how it is being developed.

spring-jpa configurationjpajpa api operation
Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"

The document discusses Java Persistence API (JPA), which is a specification that defines a Java API for object-relational mapping. It describes how JPA was created through the Java Community Process and references implementation. It also provides an overview of key JPA concepts including entities, the entity manager factory, entity managers, persistence contexts, and transaction types.

 Use batch interaction by sending a group of
inserts/updates/deletes to the database in a
single transaction by setting
"eclipselink.jdbc.batch-writing"="JDBC" in
persistence.xml, we can also specify the size of
the batch using another property,
"eclipselink.jdbc.batch-writing.size"="2000".
 Use the read-only entities when the entities are
not going to be modified, such as entities for
lookup data (for example, e-mail templates).
◦ This can be done using the @ReadOnly annotation on
the level of the entity.
 Access the JPA from stateless session beans
as a façade layer to get benefits from
different resource injections and EJB
transactional and security handling, and
encapsulate all JPA access logic.
 Don’t put (or minimize) any business logic in
Entity setters and getters.
 The shared-cache-mode element in the
persistence.xml deployment descriptor.
<persistence-unit name="examplePU" transaction-
type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvid
er
</provider>
<jta-data-source>jdbc/__default</jta-data-source>
<shared-cache-mode>ENABLE_SELECTIVE</shared-
cache-mode>
</persistence-unit>
 Cache retrieval mode, set by
the javax.persistence.retrieveMode property,
controls how data is read from the cache for calls
to the EntityManager.find method and from
queries.
 The retrieveMode property can be set to one of
the constants defined by
the javax.persistence.CacheRetrieveMode enum
type, either USE (the default) or BYPASS.
◦ USE, data is retrieved from the second-level cache, if
available. If the data is not in the cache, the persistence
provider will read it from the database.
◦ BYPASS, the second-level cache is bypassed and a call to
the database is made to retrieve the data.

Recommended for you

Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa

Spring Data is a high level SpringSource project whose purpose is to unify and ease the access to different kinds of persistence stores, both relational database systems and NoSQL data stores.

spring data jpaspring boot
Introduction to Datastore
Introduction to DatastoreIntroduction to Datastore
Introduction to Datastore

Introduction to Datastore by Assoc.Prof. Dr.Thanachart Numnonda Asst.Prof. Thanisa Kruawaisayawan Mini Master of Java Technology KMITL July 2012

datastoresoftware parkintroduction
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA

The document discusses using Hibernate, an object-relational mapping framework, to provide object persistence and retrieval by mapping Java objects to database tables. It explains what Hibernate does, its features like object-relational mapping and transaction management, and provides examples of persisting and retrieving objects using Hibernate's API. The document also shows how to configure a Hibernate project and map classes and associations between objects and database tables.

 The cache store mode, set by
the javax.persistence.storeMode property, controls
how data is stored in the cache.
 The storeMode property can be set to one of the
constants defined by
the javax.persistence.CacheStoreMode enumerated
type, either:
◦ USE (the default)
◦ BYPASS
◦ REFRESH
 Example:
EntityManager em = ...;
em.setProperty("javax.persistence.cache.storeMode",
"BYPASS");
 USE the cache data is created or updated when
data is read from or committed to the database.
◦ If data is already in the cache, setting the store mode
to USE will not force a refresh when data is read from the
database.
 BYPASS, data read from or committed to the
database is not inserted or updated in the cache.
◦ That is, the cache is unchanged.
 REFRESH, the cache data is created or updated
when data is read from or committed to the
database, and a refresh is forced on data in the
cache upon database reads.
EntityManager em = ...;
Map<String, Object> props = new
HashMap<String, Object>();
props.put("javax.persistence.cache.retrieveMod
e", "BYPASS");
String personPK = ...;
Person person = em.find(Person.class,
personPK, props);
EntityManager em = ...;
CriteriaQuery<Person> cq = ...;
TypedQuery<Person> q = em.createQuery(cq);
q.setHint("javax.persistence.cache.storeMode",
"REFRESH");

Recommended for you

JPA 2.0
JPA 2.0JPA 2.0
JPA 2.0

The document summarizes new features in JPA 2.0, including: 1) Standard properties were added to persistence.xml to configure things like locking timeouts and query timeouts. 2) The @AccessType annotation now allows mixed field and property access within an entity, overriding the default at the attribute level. 3) Derived identifiers allow identifiers to be derived from relationships rather than requiring a separate foreign key field. 4) New features were added for collections like @ElementCollection and support for ordering collections with @OrderColumn.

persistancyjavajpa
Spring framework part 2
Spring framework part 2Spring framework part 2
Spring framework part 2

This document provides an overview of Spring's transaction management capabilities. It discusses how Spring provides a uniform transaction management API that can work with various transaction technologies like JDBC, Hibernate, JPA etc. It also describes how transactions can be handled programmatically using the TransactionTemplate or directly through the transaction manager. Finally, it covers Spring's declarative transaction support using XML or annotations.

spring framework integration
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery

jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.

introduction to jquery
EntityManager em = ...;
Cache cache =
em.getEntityManagerFactory().getCache();
String personPK = ...;
if (cache.contains(Person.class, personPK)) {
// the data is cached
} else {
// the data is NOT cached
}
EntityManager em = ...;
Cache cache =
em.getEntityManagerFactory().getCache();
cache.evict(Person.class, personPK);
cache.evictAll();
 Many metadata annotations in JPA have a fetch property.
 This property can take on one of two
values: FetchType.EAGER or FetchType.LAZY.
 FetchType.EAGER means that the field is loaded by the JPA
implementation before it returns the persistent object to
you.
◦ Whenever you retrieve an entity from a query or from
the EntityManager, you are guaranteed that all of its eager fields
are populated with datastore data.
 FetchType.LAZY is a hint to the JPA runtime that you want
to defer loading of the field until you access it.
◦ This is called lazy loading. Lazy loading is completely transparent;
◦ When you attempt to read the field for the first time, the JPA
runtime will load the value from the datastore and populate the
field automatically.
◦ Lazy loading is only a hint and not a directive because some JPA
implementations cannot lazy-load certain field types.
 With a mix of eager and lazily-loaded fields,
you can ensure that commonly-used fields
load efficiently, and that other state loads
transparently when accessed.
 Example:
@OneToOne(fetch=FetchType.LAZY)
private Article article;

Recommended for you

Spring Data in 10 minutes
Spring Data in 10 minutesSpring Data in 10 minutes
Spring Data in 10 minutes

Spring Data provides a common data access layer for various data stores like MongoDB, JPA, Neo4J, and Redis. It simplifies polyglot persistence through repositories that provide common CRUD methods. Developers define entities specific to the data store and a repository interface with finder methods. Spring Data generates an implementation that interacts with the underlying data store transparently. This allows writing data access code that is portable across data sources.

mongodbspring frameworkjpa
Hibernate
HibernateHibernate
Hibernate

The document discusses object-relational mapping and Hibernate. It describes common object-relational mismatches including problems of granularity, subtypes, associations, and data navigation. It then provides an overview of Hibernate and JPA, how to start a Hibernate project, POJOs, Hibernate APIs, configuration, annotations, identifier generators, dynamic SQL generation, immutable entities, property access strategies, generated property values, default property values, entities vs value types, and mapping of collection properties.

hibernate ormhibernatehibernate tutorial
Easy data-with-spring-data-jpa
Easy data-with-spring-data-jpaEasy data-with-spring-data-jpa
Easy data-with-spring-data-jpa

Java and Spring Data JPA: Easy SQL Data Access Abstract Presenter: Miya W. Longwe, MBA, MSE, Tech Lead, Staples, Inc, Framingham MA 01702 Accessing data repositories in various applications programming languages typically involves writing of tedious boilerplate lines of code. Some application development frameworks such as Spring have tried to make the experience more succinct by providing abstraction layers such as HibernateTemplate and JdbcTemplate, etc. Despite these APIs, the developers still spend a lot time writing repetitive code than concentrating on implementing business requirements. Developers at Spring, led by Oliver Gierke, introduced Spring Data JPA which “aims to significantly improve the implementation of data access layers by reducing the effort to the amount that's actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically”. Spring Data JPA provides a powerful, out-of-the-box alternative to creating your own DAO framework. You declare custom repository operations on an interface, and the framework generates dynamic implementations (not code generation) automatically, based on conventions around method names. As part of the presentation, we'll also review a demo to look at Spring Java configuration (as opposed to XML configuration), and investigate the @Profile annotation – configuration details which may make life a bit easier in various ways when setting up unit testing of our repository classes, using out-of-the-box alternative to creating DAO framework, how to create custom repositories, pagination and support for custom queries among other features. Presenter's Bio Miya W. Longwe is a Senior Software Engineer and Tech Lead at Staples, Inc. where he is currently working on an initiative to re-platform the company’s ecommerce architecture to offer platform-driven, modular products that can be quickly customized, enhanced, and branded as needed. Miya has been a software professional since 1997. His 16 years software development career includes working for large companies to small startups, building solutions for enterprises and consumers, working with a broad range of technologies. Miya Longwe is a hands-on java developer. He believes that in order to be a relevant and effective software developer one needs to remain a deeply knowledgeable, up-to-date, and productive software developer. His research interests include model-driven engineering, domain specific languages, test driven development and project risk management. Miya graduated from the University of Malawi (Lilongwe, Malawi) and has an MBA from the University of Wales Cardiff Business School (Wales, UK) and a Masters in Software Engineering from Brandeis University (MA, USA). Occasionally, Miya can be spotted fishing the banks of the south shore (MA) with his two boys, William and Daniel.

spring dataspringspring data jpa
To get more details about Java EE 7 performance
tuning, check my book at the following URL:
http://www.packtpub.com/java-ee-7-performance-tuning-and-
optimization/book

More Related Content

What's hot

Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
ecosio GmbH
 
Configuring jpa in a Spring application
Configuring jpa in a  Spring applicationConfiguring jpa in a  Spring application
Configuring jpa in a Spring application
Jayasree Perilakkalam
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
John Slick
 
Apache Struts 2 Advance
Apache Struts 2 AdvanceApache Struts 2 Advance
Apache Struts 2 Advance
Emprovise
 
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
NarayanaMurthy Ganashree
 
Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"
Anna Shymchenko
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
Jeevesh Pandey
 
Introduction to Datastore
Introduction to DatastoreIntroduction to Datastore
Introduction to Datastore
Software Park Thailand
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
Mohammad Faizan
 
JPA 2.0
JPA 2.0JPA 2.0
Spring framework part 2
Spring framework part 2Spring framework part 2
Spring framework part 2
Haroon Idrees
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
Collaboration Technologies
 
Spring Data in 10 minutes
Spring Data in 10 minutesSpring Data in 10 minutes
Spring Data in 10 minutes
Corneil du Plessis
 
Hibernate
HibernateHibernate
Hibernate
Prashant Kalkar
 
Easy data-with-spring-data-jpa
Easy data-with-spring-data-jpaEasy data-with-spring-data-jpa
Easy data-with-spring-data-jpa
Staples
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
Ram132
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
Spring Transaction Management
Spring Transaction ManagementSpring Transaction Management
Spring Transaction Management
Ye Win
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Cheng Ta Yeh
 
ORM Concepts and JPA 2.0 Specifications
ORM Concepts and JPA 2.0 SpecificationsORM Concepts and JPA 2.0 Specifications
ORM Concepts and JPA 2.0 Specifications
Ahmed Ramzy
 

What's hot (20)

Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
 
Configuring jpa in a Spring application
Configuring jpa in a  Spring applicationConfiguring jpa in a  Spring application
Configuring jpa in a Spring application
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
 
Apache Struts 2 Advance
Apache Struts 2 AdvanceApache Struts 2 Advance
Apache Struts 2 Advance
 
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
 
Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Introduction to Datastore
Introduction to DatastoreIntroduction to Datastore
Introduction to Datastore
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
 
JPA 2.0
JPA 2.0JPA 2.0
JPA 2.0
 
Spring framework part 2
Spring framework part 2Spring framework part 2
Spring framework part 2
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Spring Data in 10 minutes
Spring Data in 10 minutesSpring Data in 10 minutes
Spring Data in 10 minutes
 
Hibernate
HibernateHibernate
Hibernate
 
Easy data-with-spring-data-jpa
Easy data-with-spring-data-jpaEasy data-with-spring-data-jpa
Easy data-with-spring-data-jpa
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
 
Spring Transaction Management
Spring Transaction ManagementSpring Transaction Management
Spring Transaction Management
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
ORM Concepts and JPA 2.0 Specifications
ORM Concepts and JPA 2.0 SpecificationsORM Concepts and JPA 2.0 Specifications
ORM Concepts and JPA 2.0 Specifications
 

Viewers also liked

JPA - Java Persistence API
JPA - Java Persistence APIJPA - Java Persistence API
JPA - Java Persistence API
Thomas Wöhlke
 
Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)
Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)
Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)
Thorben Janssen
 
Rest
RestRest
RESTful services with JAXB and JPA
RESTful services with JAXB and JPARESTful services with JAXB and JPA
RESTful services with JAXB and JPA
Shaun Smith
 
EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535
EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535
EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535
Ahmad Gohar
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
Mumbai Academisc
 
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...
Dmytro Sokolov
 
Spring batch
Spring batchSpring batch
Spring batch
nishasowdri
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplate
Guo Albert
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuning
osa_ora
 
Введение в hibernate
Введение в hibernateВведение в hibernate
Введение в hibernate
Unguryan Vitaliy
 
Spring Batch Introduction
Spring Batch IntroductionSpring Batch Introduction
Spring Batch Introduction
Tadaya Tsuyukubo
 
Orquestación o coreografía
Orquestación o coreografíaOrquestación o coreografía
Orquestación o coreografía
Abimael Desales López
 
Introduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsIntroduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOs
Matt Stine
 
Parallel batch processing with spring batch slideshare
Parallel batch processing with spring batch   slideshareParallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch slideshare
Morten Andersen-Gott
 
Ahea Team Spring batch
Ahea Team Spring batchAhea Team Spring batch
Ahea Team Spring batch
Sunghyun Roh
 
Game Development Using HTML 5
Game Development Using HTML 5Game Development Using HTML 5
Game Development Using HTML 5
osa_ora
 
Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)
Federico Paparoni
 
Spring + JPA + DAO Step by Step
Spring + JPA + DAO Step by StepSpring + JPA + DAO Step by Step
Spring + JPA + DAO Step by Step
Guo Albert
 
Spring Framework - Web Flow
Spring Framework - Web FlowSpring Framework - Web Flow
Spring Framework - Web Flow
Dzmitry Naskou
 

Viewers also liked (20)

JPA - Java Persistence API
JPA - Java Persistence APIJPA - Java Persistence API
JPA - Java Persistence API
 
Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)
Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)
Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)
 
Rest
RestRest
Rest
 
RESTful services with JAXB and JPA
RESTful services with JAXB and JPARESTful services with JAXB and JPA
RESTful services with JAXB and JPA
 
EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535
EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535
EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
 
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...
 
Spring batch
Spring batchSpring batch
Spring batch
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplate
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuning
 
Введение в hibernate
Введение в hibernateВведение в hibernate
Введение в hibernate
 
Spring Batch Introduction
Spring Batch IntroductionSpring Batch Introduction
Spring Batch Introduction
 
Orquestación o coreografía
Orquestación o coreografíaOrquestación o coreografía
Orquestación o coreografía
 
Introduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsIntroduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOs
 
Parallel batch processing with spring batch slideshare
Parallel batch processing with spring batch   slideshareParallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch slideshare
 
Ahea Team Spring batch
Ahea Team Spring batchAhea Team Spring batch
Ahea Team Spring batch
 
Game Development Using HTML 5
Game Development Using HTML 5Game Development Using HTML 5
Game Development Using HTML 5
 
Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)
 
Spring + JPA + DAO Step by Step
Spring + JPA + DAO Step by StepSpring + JPA + DAO Step by Step
Spring + JPA + DAO Step by Step
 
Spring Framework - Web Flow
Spring Framework - Web FlowSpring Framework - Web Flow
Spring Framework - Web Flow
 

Similar to JPA 2.1 performance tuning tips

Spring Framework Petclinic sample application
Spring Framework Petclinic sample applicationSpring Framework Petclinic sample application
Spring Framework Petclinic sample application
Antoine Rey
 
Slice: OpenJPA for Distributed Persistence
Slice: OpenJPA for Distributed PersistenceSlice: OpenJPA for Distributed Persistence
Slice: OpenJPA for Distributed Persistence
Pinaki Poddar
 
MUC - Moodle Universal Cache
MUC - Moodle Universal CacheMUC - Moodle Universal Cache
MUC - Moodle Universal Cache
Tim Hunt
 
Java Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreJava Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : Datastore
IMC Institute
 
ORM JPA
ORM JPAORM JPA
Struts database access
Struts database accessStruts database access
Struts database access
Abass Ndiaye
 
App Grid Dev With Coherence
App Grid Dev With CoherenceApp Grid Dev With Coherence
App Grid Dev With Coherence
James Bayer
 
Application Grid Dev with Coherence
Application Grid Dev with CoherenceApplication Grid Dev with Coherence
Application Grid Dev with Coherence
James Bayer
 
App Grid Dev With Coherence
App Grid Dev With CoherenceApp Grid Dev With Coherence
App Grid Dev With Coherence
James Bayer
 
Spring data requery
Spring data requerySpring data requery
Spring data requery
Sunghyouk Bae
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
ftz 420
 
Java - Persist and Replay Runtime Data
Java - Persist and Replay Runtime Data Java - Persist and Replay Runtime Data
Java - Persist and Replay Runtime Data
Ajay Singh
 
Core Data with Swift 3.0
Core Data with Swift 3.0Core Data with Swift 3.0
Core Data with Swift 3.0
Korhan Bircan
 
Struts2 - 101
Struts2 - 101Struts2 - 101
Struts2 - 101
Munish Gupta
 
S03 hybrid app_and_gae_datastore_v1.0
S03 hybrid app_and_gae_datastore_v1.0S03 hybrid app_and_gae_datastore_v1.0
S03 hybrid app_and_gae_datastore_v1.0
Sun-Jin Jang
 
Data mining with caret package
Data mining with caret packageData mining with caret package
Data mining with caret package
Vivian S. Zhang
 
Passing java arrays in oracle stored procedure from mule esb flow
Passing java arrays in oracle stored procedure from mule esb flowPassing java arrays in oracle stored procedure from mule esb flow
Passing java arrays in oracle stored procedure from mule esb flow
Priyobroto Ghosh (Mule ESB Certified)
 
Core Data Performance Guide Line
Core Data Performance Guide LineCore Data Performance Guide Line
Core Data Performance Guide Line
Gagan Vishal Mishra
 
Core data optimization
Core data optimizationCore data optimization
Core data optimization
Gagan Vishal Mishra
 
Javatwo2012 java frameworkcomparison
Javatwo2012 java frameworkcomparisonJavatwo2012 java frameworkcomparison
Javatwo2012 java frameworkcomparison
Jini Lee
 

Similar to JPA 2.1 performance tuning tips (20)

Spring Framework Petclinic sample application
Spring Framework Petclinic sample applicationSpring Framework Petclinic sample application
Spring Framework Petclinic sample application
 
Slice: OpenJPA for Distributed Persistence
Slice: OpenJPA for Distributed PersistenceSlice: OpenJPA for Distributed Persistence
Slice: OpenJPA for Distributed Persistence
 
MUC - Moodle Universal Cache
MUC - Moodle Universal CacheMUC - Moodle Universal Cache
MUC - Moodle Universal Cache
 
Java Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreJava Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : Datastore
 
ORM JPA
ORM JPAORM JPA
ORM JPA
 
Struts database access
Struts database accessStruts database access
Struts database access
 
App Grid Dev With Coherence
App Grid Dev With CoherenceApp Grid Dev With Coherence
App Grid Dev With Coherence
 
Application Grid Dev with Coherence
Application Grid Dev with CoherenceApplication Grid Dev with Coherence
Application Grid Dev with Coherence
 
App Grid Dev With Coherence
App Grid Dev With CoherenceApp Grid Dev With Coherence
App Grid Dev With Coherence
 
Spring data requery
Spring data requerySpring data requery
Spring data requery
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 
Java - Persist and Replay Runtime Data
Java - Persist and Replay Runtime Data Java - Persist and Replay Runtime Data
Java - Persist and Replay Runtime Data
 
Core Data with Swift 3.0
Core Data with Swift 3.0Core Data with Swift 3.0
Core Data with Swift 3.0
 
Struts2 - 101
Struts2 - 101Struts2 - 101
Struts2 - 101
 
S03 hybrid app_and_gae_datastore_v1.0
S03 hybrid app_and_gae_datastore_v1.0S03 hybrid app_and_gae_datastore_v1.0
S03 hybrid app_and_gae_datastore_v1.0
 
Data mining with caret package
Data mining with caret packageData mining with caret package
Data mining with caret package
 
Passing java arrays in oracle stored procedure from mule esb flow
Passing java arrays in oracle stored procedure from mule esb flowPassing java arrays in oracle stored procedure from mule esb flow
Passing java arrays in oracle stored procedure from mule esb flow
 
Core Data Performance Guide Line
Core Data Performance Guide LineCore Data Performance Guide Line
Core Data Performance Guide Line
 
Core data optimization
Core data optimizationCore data optimization
Core data optimization
 
Javatwo2012 java frameworkcomparison
Javatwo2012 java frameworkcomparisonJavatwo2012 java frameworkcomparison
Javatwo2012 java frameworkcomparison
 

Recently uploaded

What's New in Copilot for Microsoft365 May 2024.pptx
What's New in Copilot for Microsoft365 May 2024.pptxWhat's New in Copilot for Microsoft365 May 2024.pptx
What's New in Copilot for Microsoft365 May 2024.pptx
Stephanie Beckett
 
Best Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdfBest Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdf
Tatiana Al-Chueyr
 
Details of description part II: Describing images in practice - Tech Forum 2024
Details of description part II: Describing images in practice - Tech Forum 2024Details of description part II: Describing images in practice - Tech Forum 2024
Details of description part II: Describing images in practice - Tech Forum 2024
BookNet Canada
 
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
ArgaBisma
 
WPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide DeckWPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide Deck
Lidia A.
 
Quality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of TimeQuality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of Time
Aurora Consulting
 
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Bert Blevins
 
BLOCKCHAIN FOR DUMMIES: GUIDEBOOK FOR ALL
BLOCKCHAIN FOR DUMMIES: GUIDEBOOK FOR ALLBLOCKCHAIN FOR DUMMIES: GUIDEBOOK FOR ALL
BLOCKCHAIN FOR DUMMIES: GUIDEBOOK FOR ALL
Liveplex
 
Research Directions for Cross Reality Interfaces
Research Directions for Cross Reality InterfacesResearch Directions for Cross Reality Interfaces
Research Directions for Cross Reality Interfaces
Mark Billinghurst
 
Cookies program to display the information though cookie creation
Cookies program to display the information though cookie creationCookies program to display the information though cookie creation
Cookies program to display the information though cookie creation
shanthidl1
 
Manual | Product | Research Presentation
Manual | Product | Research PresentationManual | Product | Research Presentation
Manual | Product | Research Presentation
welrejdoall
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
HackersList
 
Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...
BookNet Canada
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
RaminGhanbari2
 
INDIAN AIR FORCE FIGHTER PLANES LIST.pdf
INDIAN AIR FORCE FIGHTER PLANES LIST.pdfINDIAN AIR FORCE FIGHTER PLANES LIST.pdf
INDIAN AIR FORCE FIGHTER PLANES LIST.pdf
jackson110191
 
Pigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdfPigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdf
Pigging Solutions
 
The Rise of Supernetwork Data Intensive Computing
The Rise of Supernetwork Data Intensive ComputingThe Rise of Supernetwork Data Intensive Computing
The Rise of Supernetwork Data Intensive Computing
Larry Smarr
 
The Increasing Use of the National Research Platform by the CSU Campuses
The Increasing Use of the National Research Platform by the CSU CampusesThe Increasing Use of the National Research Platform by the CSU Campuses
The Increasing Use of the National Research Platform by the CSU Campuses
Larry Smarr
 
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
Chris Swan
 
20240705 QFM024 Irresponsible AI Reading List June 2024
20240705 QFM024 Irresponsible AI Reading List June 202420240705 QFM024 Irresponsible AI Reading List June 2024
20240705 QFM024 Irresponsible AI Reading List June 2024
Matthew Sinclair
 

Recently uploaded (20)

What's New in Copilot for Microsoft365 May 2024.pptx
What's New in Copilot for Microsoft365 May 2024.pptxWhat's New in Copilot for Microsoft365 May 2024.pptx
What's New in Copilot for Microsoft365 May 2024.pptx
 
Best Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdfBest Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdf
 
Details of description part II: Describing images in practice - Tech Forum 2024
Details of description part II: Describing images in practice - Tech Forum 2024Details of description part II: Describing images in practice - Tech Forum 2024
Details of description part II: Describing images in practice - Tech Forum 2024
 
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
 
WPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide DeckWPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide Deck
 
Quality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of TimeQuality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of Time
 
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
 
BLOCKCHAIN FOR DUMMIES: GUIDEBOOK FOR ALL
BLOCKCHAIN FOR DUMMIES: GUIDEBOOK FOR ALLBLOCKCHAIN FOR DUMMIES: GUIDEBOOK FOR ALL
BLOCKCHAIN FOR DUMMIES: GUIDEBOOK FOR ALL
 
Research Directions for Cross Reality Interfaces
Research Directions for Cross Reality InterfacesResearch Directions for Cross Reality Interfaces
Research Directions for Cross Reality Interfaces
 
Cookies program to display the information though cookie creation
Cookies program to display the information though cookie creationCookies program to display the information though cookie creation
Cookies program to display the information though cookie creation
 
Manual | Product | Research Presentation
Manual | Product | Research PresentationManual | Product | Research Presentation
Manual | Product | Research Presentation
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
 
Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
 
INDIAN AIR FORCE FIGHTER PLANES LIST.pdf
INDIAN AIR FORCE FIGHTER PLANES LIST.pdfINDIAN AIR FORCE FIGHTER PLANES LIST.pdf
INDIAN AIR FORCE FIGHTER PLANES LIST.pdf
 
Pigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdfPigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdf
 
The Rise of Supernetwork Data Intensive Computing
The Rise of Supernetwork Data Intensive ComputingThe Rise of Supernetwork Data Intensive Computing
The Rise of Supernetwork Data Intensive Computing
 
The Increasing Use of the National Research Platform by the CSU Campuses
The Increasing Use of the National Research Platform by the CSU CampusesThe Increasing Use of the National Research Platform by the CSU Campuses
The Increasing Use of the National Research Platform by the CSU Campuses
 
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
 
20240705 QFM024 Irresponsible AI Reading List June 2024
20240705 QFM024 Irresponsible AI Reading List June 202420240705 QFM024 Irresponsible AI Reading List June 2024
20240705 QFM024 Irresponsible AI Reading List June 2024
 

JPA 2.1 performance tuning tips

  • 2.  Use the proper loading strategy from either lazy loading or eager data loading according to our data size and usage; for small data volume, eager loading always make sense.  Query results pagination is also an important JPA feature. The importance of this feature is to avoid memory issues when the returned data is huge.  The JPA provides two methods to control this pagination: setMaxResults(int maxResult) and setFirstResult(int startPosition) in the Query object.
  • 3.  Use data chunking to return the required data only from the database side, instead of filtering in the application side using the IN keyword and adding the list of IDs to the query.  Increasing the index pre-allocation size can speed up the creation of new objects. Also try to minimize the usage of composite primary keys for different entities.
  • 4.  Enabling entity caching is important to improve the performance.  Enable the query-level caching for better performance. One way to do that is by adding a hint to the named query as follows: ◦ hints={@QueryHint(name="eclipselink.query- results-cache", value="true")}
  • 5.  Use batch interaction by sending a group of inserts/updates/deletes to the database in a single transaction by setting "eclipselink.jdbc.batch-writing"="JDBC" in persistence.xml, we can also specify the size of the batch using another property, "eclipselink.jdbc.batch-writing.size"="2000".  Use the read-only entities when the entities are not going to be modified, such as entities for lookup data (for example, e-mail templates). ◦ This can be done using the @ReadOnly annotation on the level of the entity.
  • 6.  Access the JPA from stateless session beans as a façade layer to get benefits from different resource injections and EJB transactional and security handling, and encapsulate all JPA access logic.  Don’t put (or minimize) any business logic in Entity setters and getters.
  • 7.  The shared-cache-mode element in the persistence.xml deployment descriptor. <persistence-unit name="examplePU" transaction- type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvid er </provider> <jta-data-source>jdbc/__default</jta-data-source> <shared-cache-mode>ENABLE_SELECTIVE</shared- cache-mode> </persistence-unit>
  • 8.  Cache retrieval mode, set by the javax.persistence.retrieveMode property, controls how data is read from the cache for calls to the EntityManager.find method and from queries.  The retrieveMode property can be set to one of the constants defined by the javax.persistence.CacheRetrieveMode enum type, either USE (the default) or BYPASS. ◦ USE, data is retrieved from the second-level cache, if available. If the data is not in the cache, the persistence provider will read it from the database. ◦ BYPASS, the second-level cache is bypassed and a call to the database is made to retrieve the data.
  • 9.  The cache store mode, set by the javax.persistence.storeMode property, controls how data is stored in the cache.  The storeMode property can be set to one of the constants defined by the javax.persistence.CacheStoreMode enumerated type, either: ◦ USE (the default) ◦ BYPASS ◦ REFRESH  Example: EntityManager em = ...; em.setProperty("javax.persistence.cache.storeMode", "BYPASS");
  • 10.  USE the cache data is created or updated when data is read from or committed to the database. ◦ If data is already in the cache, setting the store mode to USE will not force a refresh when data is read from the database.  BYPASS, data read from or committed to the database is not inserted or updated in the cache. ◦ That is, the cache is unchanged.  REFRESH, the cache data is created or updated when data is read from or committed to the database, and a refresh is forced on data in the cache upon database reads.
  • 11. EntityManager em = ...; Map<String, Object> props = new HashMap<String, Object>(); props.put("javax.persistence.cache.retrieveMod e", "BYPASS"); String personPK = ...; Person person = em.find(Person.class, personPK, props);
  • 12. EntityManager em = ...; CriteriaQuery<Person> cq = ...; TypedQuery<Person> q = em.createQuery(cq); q.setHint("javax.persistence.cache.storeMode", "REFRESH");
  • 13. EntityManager em = ...; Cache cache = em.getEntityManagerFactory().getCache(); String personPK = ...; if (cache.contains(Person.class, personPK)) { // the data is cached } else { // the data is NOT cached }
  • 14. EntityManager em = ...; Cache cache = em.getEntityManagerFactory().getCache(); cache.evict(Person.class, personPK); cache.evictAll();
  • 15.  Many metadata annotations in JPA have a fetch property.  This property can take on one of two values: FetchType.EAGER or FetchType.LAZY.  FetchType.EAGER means that the field is loaded by the JPA implementation before it returns the persistent object to you. ◦ Whenever you retrieve an entity from a query or from the EntityManager, you are guaranteed that all of its eager fields are populated with datastore data.  FetchType.LAZY is a hint to the JPA runtime that you want to defer loading of the field until you access it. ◦ This is called lazy loading. Lazy loading is completely transparent; ◦ When you attempt to read the field for the first time, the JPA runtime will load the value from the datastore and populate the field automatically. ◦ Lazy loading is only a hint and not a directive because some JPA implementations cannot lazy-load certain field types.
  • 16.  With a mix of eager and lazily-loaded fields, you can ensure that commonly-used fields load efficiently, and that other state loads transparently when accessed.  Example: @OneToOne(fetch=FetchType.LAZY) private Article article;
  • 17. To get more details about Java EE 7 performance tuning, check my book at the following URL: http://www.packtpub.com/java-ee-7-performance-tuning-and- optimization/book