SlideShare une entreprise Scribd logo
1  sur  28
When Hibernate Attacks!
Hibernate Best Practice and Pitfalls

© 2011 Altisource Portfolio Solutions. All rights reserved. Altisource™, Altisource Portfolio Solutions™, the Altisource Logo, the "REAL" family of trademarks and service marks, and all other marks identified herein are trademarks or service marks of Altisource
© 2011 S.A. or its subsidiaries and may be S.A. All with the United States Patent and Trademark Office and in other countries. Proprietary and Confidential.
Portfolio Solutions Altisource Portfolio Solutionsregistered rights reserved. Proprietary and Confidential.

Page | 1
Agenda
– Me
– Hibernate in two slides
– Hibernate
 The Good, The Bad
 Sanity Check
 My Hibernate Divorce

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 2
Me
–
–
–
–

Roy Russo
Former JBoss Portal Co-Founder
LoopFuse Co-Founder
Senior Software Architect @ AltiSource
 We’re Hiring… and we don’t suck.

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 3
Hibernate in Two Slides
– Addresses the ‘Object-Relational Impedance
Mismatch’
– Persistence classes using idiomatic Java
– Transparent
 No interfaces or build dependencies
– Querying Facilities:
 HQL, Criteria API, JPAQL, SQL
– Database-Agnostic
– Performance:
 Caching , Lazy initialization, fetching
strategies, versioning, etc…
– Automatic DDL generation

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

public class User implements Serializable
{
private long userid = -1;
private String userName;
private String firstName;
private String lastName;
private String password;
private Map props;
private Set roles;
…

Page | 4
Hibernate in 2 Slides
<hibernate-mapping>
<class
name="com.loopfuse.domain.auth.User"
table="LF_USERS">
<id
name="userid"
column="USER_ID"
type="java.lang.Long">
<generator class="native"/>
</id>
<property
name="userName"
column="USERNAME"
type="java.lang.String"
unique="true"/>
…
<set
name="roles"
table="LF_ROLE_MEMBERS"
lazy="false"
inverse="false"
cascade="none"
sort="unsorted">
<key column="USER_ID"/>
<many-to-many
class="com.loopfuse.domain.auth.Role"
column="ROLE_ID"
outer-join="true"/>
</set>
</class>

public class User implements Serializable
{
private long userid;
private String userName;
private String firstName;
private String lastName;
private String password;
private String email;
private Map props;
private Set roles;
…

public Task getUserByID(long ID, Session session) throws DataSourceException
{
Transaction tx = session.beginTransaction();
User user = null;
try
{
Query query = session.createQuery("from User where userID=? ");
query.setParameter(0, ID);
user = (User) query.uniqueResult();
tx.commit();
}
catch (RuntimeException e)
{…}
return user;
}

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 5
First… The Basics
– It’s JDBC
– It’s a Database
– (Please, work with a DBA)
“Some developers come to using a tool like
Hibernate because they are uncomfortable
with SQL and with relational databases. We
would say that this is exactly the wrong
reason to use Hibernate. You should be very
comfortable with SQL and JDBC before you
start using Hibernate - Hibernate builds on
JDBC, it does not replace it. “
 Gavin King, Hibernate Founder

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 6
Why we love Hibernate
– Lazy developers love Hibernate:
 Near-flat learning curve
 Little knowledge of JDBC or DB schema
design
 Little knowledge of Hibernate mapping
– Lazy loading, cache, session mgmt

 Little knowledge on how to tune Hibernate or
its JDBC parameters
 Ignore Hibernate SQL Query log

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 7
The Good and the Bad
– Hibernate can rock:
 Rapid development
 Cache
 Pooling
 Support / Docs
– Hibernate can suck:
 Performance problems
 Session Management
 Development Time

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 8
Problem: Schema-Ownership
Development 101:
1. Developers begin project with a clean slate.
2. Developers define schema in the DB AND *.hbm.xml
3. Developers ship product
4. DBAs held accountable for performance
5. DBAs call for refactoring / denormalization
6. Now what?

– Who owns the DB schema?
– Marketing called and needs 500 reports written.
Who delivers?
– Sales needs integration with Crystal Reports
– Dual metadata: Changes to either schema requires
changes in the other.
 (IDEs won’t refactor/migrate/adapt DB schema)
© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 9
Solution: Schema-Ownership, cont.
– Work with a DBA
 Be prepared: Likely minimize reliance on Hibernate
 DBA provides Query plan.
– Consider Hibernate for simple CRUD
– Set up a mirror DB specifically for reports
 Possible silo

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 10
Problem: n+1 SELECT
So you set lazy=“true”…
Retrieve all Items for User:
Iterator items = session.createCriteria(Item.class)
.add( Expression.eq("item.seller", user) )
.list()
.iterator();

The n+1 problem is difficult to
detect, as it is usually hidden
inside application logic.

Find maximum Bid for Items:
List maxAmounts = new ArrayList();
while (items.hasNext()) {
Item item = (Item) items.next();
BigDecimal maxAmount = new BigDecimal("0");
for ( Iterator b = item.getBids().iterator(); b.hasNext(); ) {
Bid bid = (Bid) b.next();
if ( bid.getAmount().compareTo(maxAmount) == 1 )
maxAmount = bid.getAmount();
}
maxAmounts.add( new MaxAmount( item.getId(), maxAmount ) );
}

Hibernate issues 1 SELECT per Bid…
© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 11
Solution: n+1 SELECT
Enable Batch Fetching:
<set name="bids" lazy="true" inverse="true" batch‐size="10">

– Hibernate pre-fetches the next 10 collections
– Problem is now reduced to n/10+1 SELECTs
– A little better, but now other transactions will fetch collections
unnecessarily.
HQL Aggregation:
String query = "select MaxAmount( item.id, max(bid.amount) )" +
" from Item item join fetch item.bids bid" + " where item.seller = :user group by item.id";
List maxAmounts = session.createQuery(query).setEntity("user", user).list();

– Possible solution, unless we want to do complex processing of Bids.
Enable Eager Fetching:
<set name="bids" inverse="true" outer‐join="true">

– Note: HQL ignores outer-join, but you can use the Criteria API
– You should be fired.
© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 12
Solution: n+1 SELECT
Runtime Declaration of Fetch Strategy:
List results = session.createCriteria(Item.class)
.add( Expression.eq("item.seller", user) )
.setFetchMode("bids", FetchMode.EAGER)
.list();

Iterator items = new HashSet(results).iterator();
List maxAmounts = new ArrayList();
for ( ; items.hasNext(); ) {
Item item = (Item) items.next();
BigDecimal maxAmount = new BigDecimal("0");
for ( Iterator b = item.getBids().iterator(); b.hasNext(); ) {
Bid bid = (Bid) b.next();
if ( bid.getAmount().compareTo(maxAmount) == 1 )
maxAmount = bid.getAmount();
}
maxAmounts.add( new MaxAmount( item.getId(), maxAmount ) );
}

– No guarantee of distinct Items returned.

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 13
Problem: LazyInitializationException
– Cause:
 Thrown when an unitialized collection (or proxy)
is accessed outside of the scope of the Session.
– Detached collections can’t be initialized

 Lazy initialization turned on by default v3.
– Recommended best practice to leave it turned on.

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 14
Solution: LazyInitializationException
– If Session is open, use Hibernate.initialize(item), forcing initialization, as
long as the Session is open.
– Keep the Session open:
 Open Session in View pattern: Use a servlet filter to close the Session
at the end of a request.
– You must alter application architecture
– You must address exception handling

 Call Hibernate.initialize(item) in the business layer before returning to
the web tier, or resort to eager fetching.
 May not be a feasible strategy in n-tier environments

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 15
Problem: “Hibernate is Slow”
– Hibernate addresses performance:
 Cache
 Amount of data loaded
– Finding the Cause:
 Monitor the DB
– In MySQL:
•
•
•
•

show processlist;
log_slow_queries = /var/log/mysql/mysql-slow.log &
long_query_time = 1
log-queries-not-using-indexes

 Monitor Hibernate logging/queries:
– hibernate.show_sql=true / log4j.logger.org.hibernate.type=debug
– The output will scare you, but you can use it to execute a test query…
• EXPLAIN [SQL]

 Use a profiler
© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 16
Addressing: “Hibernate is Slow”
– Are tables properly indexed? Call the DBA.
– Slow INSERT?
 Consider “batch” INSERT
– Explicit flushing:
• Dirty checking of objects can take a lot of time

– Slow SELECT?
 SELECT on PK. Avoids DB call – uses cache
 Are you loading everything in memory and looping through it? Don’t.
– 2nd Level cache on read-only entities
– Use SQL ;-)
– If appropriate, consider batch processing…

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 17
Problem: OOM on Batch Processing

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
}
tx.commit();
session.close();

– Above code will lead to OOM
 Hibernate caches all newly created Customer instances

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 18
Solution: OOM on Batch Processing
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();

for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();

ScrollableResults customers = session.getNamedQuery("GetCustomers")
.scroll(ScrollMode.FORWARD_ONLY);
while ( customers.next() ) {
Customer customer = (Customer) customers.get(0);
customer.updateStuff(...);
session.update(customer);
}
tx.commit();
session.close();

– Flush and Clear the Session regularly
– Use the StatelessSession Interface
 Bypass cache
 No dirty-checking
 Ignores collections on entities
 Bypass Hibernate interceptors and event model
© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 19
Sanity Check
–

–

–

Addresses the ‘Object-Relational Impedance
Mismatch’
 Good marketing. ORM is the impedance
mismatch. It creates the inefficient connection.
 DBs provide permanent storage. Programming
languages process data in step-wide fashion.
Morphing both is a mismatch. Promoting their
strengths is the ideal scenario.
Transparent
 You’re in control of the level of depth
Hibernate permeates your codebase.
 Expect some magic.
Querying Facilities (HQL, Criteria API, JPAQL, SQL)
 In the end, they output SQL. Ugly SQL.

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 20
Sanity Check
– Database-Agnostic
 Who cares?
– “Performance”: Lazy initialization, fetching
strategies, versioning, etc…
 Faster than what?
 If performance is a concern, raw JDBC/SQL
is your answer.
– Caching:
 Databases already offer this.
 Roll your own cache
 Beware of cached data modified by other
applications.
– Automatic DDL generation
 Never use this!
© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 21
My Hibernate Divorce
– Scenario:
 Analytics company processing several million
hits, email clicks, email opens, and form posts
per day.
 Over 800 customer in multi-tenant-style
schema
 75+ tables per schema
 2 MySQL servers in Master-Slave
configuration
 Application servers from 8GB to 16GB physical
memory.
– Hibernate Use:
 100%
 Originally JSF+Hibernate = Rapid Development

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 22
My Hibernate Divorce
– Problem #1:
 Random OOMs
– Diagnosis:
 Boot up Jprofiler. Nothing there.
 Analyze heap dump file:
– Hibernate holding 5MB per SessionFactory in memory, ~4GB per server

 Reporting and other intensive operations cause OOM
 By design, lazy-loading was disabled.

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 23
My Hibernate Divorce
– Solution:
 Add more RAM. Could not scale horizontally.
 Load balance customers across servers (ala SFDC)
 Rethink lazy-loading strategy:
– Massive re-architecture
– Suboptimal in certain parts of the system.
• Recording hits/clicks/opens
• Reporting

 Rip out Hibernate for memory intensive operations.
–
–
–
–

Table per Class – this was going to take a while.
Perform processing in DB, not code! That’s what they’re designed to do!
Some stored proc.
Get a DBA.

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 24
My Hibernate Divorce
– Problem #2:
 Slow boot time: ~20 minutes.
 With hibernate update 2+ hours. (and system crashes)
 Marketing people aren’t patient.
– Diagnosis:
 Hibernate SessionFactory creation across 800 schema can be slow. ;-)
 hbm2ddl.auto=update is evil
– Solution:
 hbm2ddl.auto=none
– Handle schema updates manually (yay, PHP!)
– Migration and adapting data a manual process.

 Load SessionFactories on-demand.
– When user logs in, load SessionFactory

 Reap stale SessionFactories
© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 25
Final Thoughts
Anti-Pattern:
1. Some repeated pattern of action, process or structure
that initially appears to be beneficial, but ultimately
produces more bad consequences than beneficial
results, and
2. A refactored solution exists that is clearly
documented, proven in actual practice and repeatable.

If you really must use Hibernate:
– Never let Hibernate design your schema
– Your schema will outlive your application
– Learn SQL, Stored Proc, and database design principles.
– Proper indexing is paramount! Get a DBA.
– Consider a mix: 50% Hibernate (75% or 90%)
© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 26
And Today…
“... ‘a lot of open source projects just outgrow
Hibernate. Hibernate was great to get
started and that although it has a richer set
of capabilities than iBatis, sometimes it just
gets in the way’.”
- John Newton, CTO, Alfresco

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 27
Questions?

© 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential.

Page | 28

Contenu connexe

Tendances

React Redux Tutorial | Redux Tutorial for Beginners | React Redux Training | ...
React Redux Tutorial | Redux Tutorial for Beginners | React Redux Training | ...React Redux Tutorial | Redux Tutorial for Beginners | React Redux Training | ...
React Redux Tutorial | Redux Tutorial for Beginners | React Redux Training | ...Edureka!
 
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX LondonJAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX LondonArun Gupta
 
JAX-RS 2.0: What’s New in JSR 339 ?
JAX-RS 2.0: What’s New in JSR 339 ?JAX-RS 2.0: What’s New in JSR 339 ?
JAX-RS 2.0: What’s New in JSR 339 ?Arun Gupta
 
Hibernate 5 – merge() Example
Hibernate 5 – merge() ExampleHibernate 5 – merge() Example
Hibernate 5 – merge() ExampleDucat India
 
SwiftUI and Combine All the Things
SwiftUI and Combine All the ThingsSwiftUI and Combine All the Things
SwiftUI and Combine All the ThingsScott Gardner
 

Tendances (6)

React Redux Tutorial | Redux Tutorial for Beginners | React Redux Training | ...
React Redux Tutorial | Redux Tutorial for Beginners | React Redux Training | ...React Redux Tutorial | Redux Tutorial for Beginners | React Redux Training | ...
React Redux Tutorial | Redux Tutorial for Beginners | React Redux Training | ...
 
Hibernate 3
Hibernate 3Hibernate 3
Hibernate 3
 
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX LondonJAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
 
JAX-RS 2.0: What’s New in JSR 339 ?
JAX-RS 2.0: What’s New in JSR 339 ?JAX-RS 2.0: What’s New in JSR 339 ?
JAX-RS 2.0: What’s New in JSR 339 ?
 
Hibernate 5 – merge() Example
Hibernate 5 – merge() ExampleHibernate 5 – merge() Example
Hibernate 5 – merge() Example
 
SwiftUI and Combine All the Things
SwiftUI and Combine All the ThingsSwiftUI and Combine All the Things
SwiftUI and Combine All the Things
 

En vedette

Daniel_Bouska_Resume
Daniel_Bouska_ResumeDaniel_Bouska_Resume
Daniel_Bouska_ResumeDaniel Bouska
 
Informatica Solidale - Assemblea 2014
Informatica Solidale - Assemblea 2014Informatica Solidale - Assemblea 2014
Informatica Solidale - Assemblea 2014Claudio Tancini
 
APC implementation on CCR Plant 2009
APC implementation on CCR Plant 2009APC implementation on CCR Plant 2009
APC implementation on CCR Plant 2009Pranob Banerjee
 
Инструкция по настройке сервиса Daas на базе мини пк
Инструкция по настройке сервиса  Daas на базе мини пкИнструкция по настройке сервиса  Daas на базе мини пк
Инструкция по настройке сервиса Daas на базе мини пкЕлена Кузовкина
 
Инструкция по настройке сервиса виртуальное рабочее место на базе планшета I...
Инструкция по настройке сервиса  виртуальное рабочее место на базе планшета I...Инструкция по настройке сервиса  виртуальное рабочее место на базе планшета I...
Инструкция по настройке сервиса виртуальное рабочее место на базе планшета I...Елена Кузовкина
 
A escola não se pode dar ao luxo de ser permissiva
A escola não se pode dar ao luxo de ser permissivaA escola não se pode dar ao luxo de ser permissiva
A escola não se pode dar ao luxo de ser permissivaDo outro lado da barricada
 
Decreto lei n.º 132%2 f2012%2c de 27 de junho
Decreto lei n.º 132%2 f2012%2c de 27 de junhoDecreto lei n.º 132%2 f2012%2c de 27 de junho
Decreto lei n.º 132%2 f2012%2c de 27 de junhoDo outro lado da barricada
 
Marcelle Poirier is a Reputed Avocat Francophone in Miami
Marcelle Poirier is a Reputed Avocat Francophone in MiamiMarcelle Poirier is a Reputed Avocat Francophone in Miami
Marcelle Poirier is a Reputed Avocat Francophone in Miamimarcellepoirier
 
How to generate sales leads
How to generate sales leadsHow to generate sales leads
How to generate sales leadsFundoodata.com
 
Blackwall partners 2 qtr 2016- transient volatility part iii
Blackwall partners  2 qtr 2016- transient volatility part iiiBlackwall partners  2 qtr 2016- transient volatility part iii
Blackwall partners 2 qtr 2016- transient volatility part iiiMichael Durante
 
Floating Point Unit (FPU)
Floating Point Unit (FPU)Floating Point Unit (FPU)
Floating Point Unit (FPU)Silicon Mentor
 
Kiril mitovski-2014eng-1
Kiril mitovski-2014eng-1Kiril mitovski-2014eng-1
Kiril mitovski-2014eng-1Sim Aleksiev
 
1600 1620 siwanon jirawatnotai
1600 1620 siwanon jirawatnotai1600 1620 siwanon jirawatnotai
1600 1620 siwanon jirawatnotaispa718
 

En vedette (20)

Daniel_Bouska_Resume
Daniel_Bouska_ResumeDaniel_Bouska_Resume
Daniel_Bouska_Resume
 
InternReport
InternReportInternReport
InternReport
 
Informatica Solidale - Assemblea 2014
Informatica Solidale - Assemblea 2014Informatica Solidale - Assemblea 2014
Informatica Solidale - Assemblea 2014
 
Eletrical current
Eletrical currentEletrical current
Eletrical current
 
#Shareyouresearch - L.Benacchio
#Shareyouresearch - L.Benacchio#Shareyouresearch - L.Benacchio
#Shareyouresearch - L.Benacchio
 
APC implementation on CCR Plant 2009
APC implementation on CCR Plant 2009APC implementation on CCR Plant 2009
APC implementation on CCR Plant 2009
 
Инструкция по настройке сервиса Daas на базе мини пк
Инструкция по настройке сервиса  Daas на базе мини пкИнструкция по настройке сервиса  Daas на базе мини пк
Инструкция по настройке сервиса Daas на базе мини пк
 
Инструкция по настройке сервиса виртуальное рабочее место на базе планшета I...
Инструкция по настройке сервиса  виртуальное рабочее место на базе планшета I...Инструкция по настройке сервиса  виртуальное рабочее место на базе планшета I...
Инструкция по настройке сервиса виртуальное рабочее место на базе планшета I...
 
La membrana plasmàtica
La membrana plasmàticaLa membrana plasmàtica
La membrana plasmàtica
 
A escola não se pode dar ao luxo de ser permissiva
A escola não se pode dar ao luxo de ser permissivaA escola não se pode dar ao luxo de ser permissiva
A escola não se pode dar ao luxo de ser permissiva
 
Decreto lei n.º 132%2 f2012%2c de 27 de junho
Decreto lei n.º 132%2 f2012%2c de 27 de junhoDecreto lei n.º 132%2 f2012%2c de 27 de junho
Decreto lei n.º 132%2 f2012%2c de 27 de junho
 
Portaria n.º 9/2017
Portaria n.º 9/2017Portaria n.º 9/2017
Portaria n.º 9/2017
 
Marcelle Poirier is a Reputed Avocat Francophone in Miami
Marcelle Poirier is a Reputed Avocat Francophone in MiamiMarcelle Poirier is a Reputed Avocat Francophone in Miami
Marcelle Poirier is a Reputed Avocat Francophone in Miami
 
How to generate sales leads
How to generate sales leadsHow to generate sales leads
How to generate sales leads
 
Blackwall partners 2 qtr 2016- transient volatility part iii
Blackwall partners  2 qtr 2016- transient volatility part iiiBlackwall partners  2 qtr 2016- transient volatility part iii
Blackwall partners 2 qtr 2016- transient volatility part iii
 
Floating Point Unit (FPU)
Floating Point Unit (FPU)Floating Point Unit (FPU)
Floating Point Unit (FPU)
 
Arquitectura
ArquitecturaArquitectura
Arquitectura
 
Kiril mitovski-2014eng-1
Kiril mitovski-2014eng-1Kiril mitovski-2014eng-1
Kiril mitovski-2014eng-1
 
1600 1620 siwanon jirawatnotai
1600 1620 siwanon jirawatnotai1600 1620 siwanon jirawatnotai
1600 1620 siwanon jirawatnotai
 
Risk management
Risk management Risk management
Risk management
 

Similaire à Ajug hibernate-dos-donts

Oracle Code Event - MySQL JSON Document Store
Oracle Code Event - MySQL JSON Document StoreOracle Code Event - MySQL JSON Document Store
Oracle Code Event - MySQL JSON Document StoreMark Swarbrick
 
MySQL 8.0 Introduction to NoSQL + SQL
MySQL 8.0 Introduction to NoSQL + SQLMySQL 8.0 Introduction to NoSQL + SQL
MySQL 8.0 Introduction to NoSQL + SQLManuel Contreras
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsAmitaSuri
 
Dolibarr information for developers - Christmas devcamp in Valence
Dolibarr information for developers - Christmas devcamp in ValenceDolibarr information for developers - Christmas devcamp in Valence
Dolibarr information for developers - Christmas devcamp in ValenceLaurent Destailleur
 
JMP103 : Extending Your App Arsenal With OpenSocial
JMP103 : Extending Your App Arsenal With OpenSocialJMP103 : Extending Your App Arsenal With OpenSocial
JMP103 : Extending Your App Arsenal With OpenSocialRyan Baxter
 
IBM Connect 2014 - JMP103: Extending Your Application Arsenal With OpenSocial
IBM Connect 2014 - JMP103: Extending Your Application Arsenal With OpenSocialIBM Connect 2014 - JMP103: Extending Your Application Arsenal With OpenSocial
IBM Connect 2014 - JMP103: Extending Your Application Arsenal With OpenSocialIBM Connections Developers
 
Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store
Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document StoreConnector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store
Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document StoreFilipe Silva
 
MySQL Document Store (Oracle Code Warsaw 2018)
MySQL Document Store (Oracle Code Warsaw 2018)MySQL Document Store (Oracle Code Warsaw 2018)
MySQL Document Store (Oracle Code Warsaw 2018)Vittorio Cioe
 
Oracle OpenWorld 2013 - HOL9737 MySQL Replication Best Practices
Oracle OpenWorld 2013 - HOL9737 MySQL Replication Best PracticesOracle OpenWorld 2013 - HOL9737 MySQL Replication Best Practices
Oracle OpenWorld 2013 - HOL9737 MySQL Replication Best PracticesSven Sandberg
 
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.Cloud Native Day Tel Aviv
 
WordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeWordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeRakesh Kushwaha
 
MySQL 8.0 - What's New ?
MySQL 8.0 - What's New ?MySQL 8.0 - What's New ?
MySQL 8.0 - What's New ?Olivier DASINI
 
Upgrading to my sql 8.0
Upgrading to my sql 8.0Upgrading to my sql 8.0
Upgrading to my sql 8.0Ståle Deraas
 
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten ZiegelerOSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegelermfrancis
 
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?Olivier DASINI
 
Multi-tenancy with Rails
Multi-tenancy with RailsMulti-tenancy with Rails
Multi-tenancy with RailsPaul Gallagher
 
ServerTemplate Deep Dive
ServerTemplate Deep DiveServerTemplate Deep Dive
ServerTemplate Deep DiveRightScale
 

Similaire à Ajug hibernate-dos-donts (20)

Oracle Code Event - MySQL JSON Document Store
Oracle Code Event - MySQL JSON Document StoreOracle Code Event - MySQL JSON Document Store
Oracle Code Event - MySQL JSON Document Store
 
MySQL 8.0 Introduction to NoSQL + SQL
MySQL 8.0 Introduction to NoSQL + SQLMySQL 8.0 Introduction to NoSQL + SQL
MySQL 8.0 Introduction to NoSQL + SQL
 
My sql8 innodb_cluster
My sql8 innodb_clusterMy sql8 innodb_cluster
My sql8 innodb_cluster
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha Projects
 
Dolibarr information for developers - Christmas devcamp in Valence
Dolibarr information for developers - Christmas devcamp in ValenceDolibarr information for developers - Christmas devcamp in Valence
Dolibarr information for developers - Christmas devcamp in Valence
 
JMP103 : Extending Your App Arsenal With OpenSocial
JMP103 : Extending Your App Arsenal With OpenSocialJMP103 : Extending Your App Arsenal With OpenSocial
JMP103 : Extending Your App Arsenal With OpenSocial
 
IBM Connect 2014 - JMP103: Extending Your Application Arsenal With OpenSocial
IBM Connect 2014 - JMP103: Extending Your Application Arsenal With OpenSocialIBM Connect 2014 - JMP103: Extending Your Application Arsenal With OpenSocial
IBM Connect 2014 - JMP103: Extending Your Application Arsenal With OpenSocial
 
Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store
Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document StoreConnector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store
Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store
 
MySQL Document Store (Oracle Code Warsaw 2018)
MySQL Document Store (Oracle Code Warsaw 2018)MySQL Document Store (Oracle Code Warsaw 2018)
MySQL Document Store (Oracle Code Warsaw 2018)
 
Marcin Szałowicz - MySQL Workbench
Marcin Szałowicz - MySQL WorkbenchMarcin Szałowicz - MySQL Workbench
Marcin Szałowicz - MySQL Workbench
 
Oracle OpenWorld 2013 - HOL9737 MySQL Replication Best Practices
Oracle OpenWorld 2013 - HOL9737 MySQL Replication Best PracticesOracle OpenWorld 2013 - HOL9737 MySQL Replication Best Practices
Oracle OpenWorld 2013 - HOL9737 MySQL Replication Best Practices
 
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
 
WordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeWordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcode
 
MySQL 8.0 - What's New ?
MySQL 8.0 - What's New ?MySQL 8.0 - What's New ?
MySQL 8.0 - What's New ?
 
Upgrading to my sql 8.0
Upgrading to my sql 8.0Upgrading to my sql 8.0
Upgrading to my sql 8.0
 
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten ZiegelerOSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
 
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
 
Sightly_techInsight
Sightly_techInsightSightly_techInsight
Sightly_techInsight
 
Multi-tenancy with Rails
Multi-tenancy with RailsMulti-tenancy with Rails
Multi-tenancy with Rails
 
ServerTemplate Deep Dive
ServerTemplate Deep DiveServerTemplate Deep Dive
ServerTemplate Deep Dive
 

Plus de Roy Russo

Devnexus 2018
Devnexus 2018Devnexus 2018
Devnexus 2018Roy Russo
 
Dev nexus 2017
Dev nexus 2017Dev nexus 2017
Dev nexus 2017Roy Russo
 
Elasticsearch Atlanta Meetup 3/15/16
Elasticsearch Atlanta Meetup 3/15/16Elasticsearch Atlanta Meetup 3/15/16
Elasticsearch Atlanta Meetup 3/15/16Roy Russo
 
PyATL Meetup, Oct 8, 2015
PyATL Meetup, Oct 8, 2015PyATL Meetup, Oct 8, 2015
PyATL Meetup, Oct 8, 2015Roy Russo
 
Elasticsearch - DevNexus 2015
Elasticsearch - DevNexus 2015Elasticsearch - DevNexus 2015
Elasticsearch - DevNexus 2015Roy Russo
 
Introduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users GroupIntroduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users GroupRoy Russo
 
ElasticSearch - DevNexus Atlanta - 2014
ElasticSearch - DevNexus Atlanta - 2014ElasticSearch - DevNexus Atlanta - 2014
ElasticSearch - DevNexus Atlanta - 2014Roy Russo
 
ElasticSearch AJUG 2013
ElasticSearch AJUG 2013ElasticSearch AJUG 2013
ElasticSearch AJUG 2013Roy Russo
 

Plus de Roy Russo (8)

Devnexus 2018
Devnexus 2018Devnexus 2018
Devnexus 2018
 
Dev nexus 2017
Dev nexus 2017Dev nexus 2017
Dev nexus 2017
 
Elasticsearch Atlanta Meetup 3/15/16
Elasticsearch Atlanta Meetup 3/15/16Elasticsearch Atlanta Meetup 3/15/16
Elasticsearch Atlanta Meetup 3/15/16
 
PyATL Meetup, Oct 8, 2015
PyATL Meetup, Oct 8, 2015PyATL Meetup, Oct 8, 2015
PyATL Meetup, Oct 8, 2015
 
Elasticsearch - DevNexus 2015
Elasticsearch - DevNexus 2015Elasticsearch - DevNexus 2015
Elasticsearch - DevNexus 2015
 
Introduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users GroupIntroduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users Group
 
ElasticSearch - DevNexus Atlanta - 2014
ElasticSearch - DevNexus Atlanta - 2014ElasticSearch - DevNexus Atlanta - 2014
ElasticSearch - DevNexus Atlanta - 2014
 
ElasticSearch AJUG 2013
ElasticSearch AJUG 2013ElasticSearch AJUG 2013
ElasticSearch AJUG 2013
 

Dernier

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 

Dernier (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 

Ajug hibernate-dos-donts

  • 1. When Hibernate Attacks! Hibernate Best Practice and Pitfalls © 2011 Altisource Portfolio Solutions. All rights reserved. Altisource™, Altisource Portfolio Solutions™, the Altisource Logo, the "REAL" family of trademarks and service marks, and all other marks identified herein are trademarks or service marks of Altisource © 2011 S.A. or its subsidiaries and may be S.A. All with the United States Patent and Trademark Office and in other countries. Proprietary and Confidential. Portfolio Solutions Altisource Portfolio Solutionsregistered rights reserved. Proprietary and Confidential. Page | 1
  • 2. Agenda – Me – Hibernate in two slides – Hibernate  The Good, The Bad  Sanity Check  My Hibernate Divorce © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 2
  • 3. Me – – – – Roy Russo Former JBoss Portal Co-Founder LoopFuse Co-Founder Senior Software Architect @ AltiSource  We’re Hiring… and we don’t suck. © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 3
  • 4. Hibernate in Two Slides – Addresses the ‘Object-Relational Impedance Mismatch’ – Persistence classes using idiomatic Java – Transparent  No interfaces or build dependencies – Querying Facilities:  HQL, Criteria API, JPAQL, SQL – Database-Agnostic – Performance:  Caching , Lazy initialization, fetching strategies, versioning, etc… – Automatic DDL generation © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. public class User implements Serializable { private long userid = -1; private String userName; private String firstName; private String lastName; private String password; private Map props; private Set roles; … Page | 4
  • 5. Hibernate in 2 Slides <hibernate-mapping> <class name="com.loopfuse.domain.auth.User" table="LF_USERS"> <id name="userid" column="USER_ID" type="java.lang.Long"> <generator class="native"/> </id> <property name="userName" column="USERNAME" type="java.lang.String" unique="true"/> … <set name="roles" table="LF_ROLE_MEMBERS" lazy="false" inverse="false" cascade="none" sort="unsorted"> <key column="USER_ID"/> <many-to-many class="com.loopfuse.domain.auth.Role" column="ROLE_ID" outer-join="true"/> </set> </class> public class User implements Serializable { private long userid; private String userName; private String firstName; private String lastName; private String password; private String email; private Map props; private Set roles; … public Task getUserByID(long ID, Session session) throws DataSourceException { Transaction tx = session.beginTransaction(); User user = null; try { Query query = session.createQuery("from User where userID=? "); query.setParameter(0, ID); user = (User) query.uniqueResult(); tx.commit(); } catch (RuntimeException e) {…} return user; } © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 5
  • 6. First… The Basics – It’s JDBC – It’s a Database – (Please, work with a DBA) “Some developers come to using a tool like Hibernate because they are uncomfortable with SQL and with relational databases. We would say that this is exactly the wrong reason to use Hibernate. You should be very comfortable with SQL and JDBC before you start using Hibernate - Hibernate builds on JDBC, it does not replace it. “  Gavin King, Hibernate Founder © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 6
  • 7. Why we love Hibernate – Lazy developers love Hibernate:  Near-flat learning curve  Little knowledge of JDBC or DB schema design  Little knowledge of Hibernate mapping – Lazy loading, cache, session mgmt  Little knowledge on how to tune Hibernate or its JDBC parameters  Ignore Hibernate SQL Query log © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 7
  • 8. The Good and the Bad – Hibernate can rock:  Rapid development  Cache  Pooling  Support / Docs – Hibernate can suck:  Performance problems  Session Management  Development Time © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 8
  • 9. Problem: Schema-Ownership Development 101: 1. Developers begin project with a clean slate. 2. Developers define schema in the DB AND *.hbm.xml 3. Developers ship product 4. DBAs held accountable for performance 5. DBAs call for refactoring / denormalization 6. Now what? – Who owns the DB schema? – Marketing called and needs 500 reports written. Who delivers? – Sales needs integration with Crystal Reports – Dual metadata: Changes to either schema requires changes in the other.  (IDEs won’t refactor/migrate/adapt DB schema) © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 9
  • 10. Solution: Schema-Ownership, cont. – Work with a DBA  Be prepared: Likely minimize reliance on Hibernate  DBA provides Query plan. – Consider Hibernate for simple CRUD – Set up a mirror DB specifically for reports  Possible silo © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 10
  • 11. Problem: n+1 SELECT So you set lazy=“true”… Retrieve all Items for User: Iterator items = session.createCriteria(Item.class) .add( Expression.eq("item.seller", user) ) .list() .iterator(); The n+1 problem is difficult to detect, as it is usually hidden inside application logic. Find maximum Bid for Items: List maxAmounts = new ArrayList(); while (items.hasNext()) { Item item = (Item) items.next(); BigDecimal maxAmount = new BigDecimal("0"); for ( Iterator b = item.getBids().iterator(); b.hasNext(); ) { Bid bid = (Bid) b.next(); if ( bid.getAmount().compareTo(maxAmount) == 1 ) maxAmount = bid.getAmount(); } maxAmounts.add( new MaxAmount( item.getId(), maxAmount ) ); } Hibernate issues 1 SELECT per Bid… © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 11
  • 12. Solution: n+1 SELECT Enable Batch Fetching: <set name="bids" lazy="true" inverse="true" batch‐size="10"> – Hibernate pre-fetches the next 10 collections – Problem is now reduced to n/10+1 SELECTs – A little better, but now other transactions will fetch collections unnecessarily. HQL Aggregation: String query = "select MaxAmount( item.id, max(bid.amount) )" + " from Item item join fetch item.bids bid" + " where item.seller = :user group by item.id"; List maxAmounts = session.createQuery(query).setEntity("user", user).list(); – Possible solution, unless we want to do complex processing of Bids. Enable Eager Fetching: <set name="bids" inverse="true" outer‐join="true"> – Note: HQL ignores outer-join, but you can use the Criteria API – You should be fired. © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 12
  • 13. Solution: n+1 SELECT Runtime Declaration of Fetch Strategy: List results = session.createCriteria(Item.class) .add( Expression.eq("item.seller", user) ) .setFetchMode("bids", FetchMode.EAGER) .list(); Iterator items = new HashSet(results).iterator(); List maxAmounts = new ArrayList(); for ( ; items.hasNext(); ) { Item item = (Item) items.next(); BigDecimal maxAmount = new BigDecimal("0"); for ( Iterator b = item.getBids().iterator(); b.hasNext(); ) { Bid bid = (Bid) b.next(); if ( bid.getAmount().compareTo(maxAmount) == 1 ) maxAmount = bid.getAmount(); } maxAmounts.add( new MaxAmount( item.getId(), maxAmount ) ); } – No guarantee of distinct Items returned. © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 13
  • 14. Problem: LazyInitializationException – Cause:  Thrown when an unitialized collection (or proxy) is accessed outside of the scope of the Session. – Detached collections can’t be initialized  Lazy initialization turned on by default v3. – Recommended best practice to leave it turned on. © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 14
  • 15. Solution: LazyInitializationException – If Session is open, use Hibernate.initialize(item), forcing initialization, as long as the Session is open. – Keep the Session open:  Open Session in View pattern: Use a servlet filter to close the Session at the end of a request. – You must alter application architecture – You must address exception handling  Call Hibernate.initialize(item) in the business layer before returning to the web tier, or resort to eager fetching.  May not be a feasible strategy in n-tier environments © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 15
  • 16. Problem: “Hibernate is Slow” – Hibernate addresses performance:  Cache  Amount of data loaded – Finding the Cause:  Monitor the DB – In MySQL: • • • • show processlist; log_slow_queries = /var/log/mysql/mysql-slow.log & long_query_time = 1 log-queries-not-using-indexes  Monitor Hibernate logging/queries: – hibernate.show_sql=true / log4j.logger.org.hibernate.type=debug – The output will scare you, but you can use it to execute a test query… • EXPLAIN [SQL]  Use a profiler © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 16
  • 17. Addressing: “Hibernate is Slow” – Are tables properly indexed? Call the DBA. – Slow INSERT?  Consider “batch” INSERT – Explicit flushing: • Dirty checking of objects can take a lot of time – Slow SELECT?  SELECT on PK. Avoids DB call – uses cache  Are you loading everything in memory and looping through it? Don’t. – 2nd Level cache on read-only entities – Use SQL ;-) – If appropriate, consider batch processing… © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 17
  • 18. Problem: OOM on Batch Processing Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); } tx.commit(); session.close(); – Above code will lead to OOM  Hibernate caches all newly created Customer instances © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 18
  • 19. Solution: OOM on Batch Processing Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); StatelessSession session = sessionFactory.openStatelessSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit(); session.close(); ScrollableResults customers = session.getNamedQuery("GetCustomers") .scroll(ScrollMode.FORWARD_ONLY); while ( customers.next() ) { Customer customer = (Customer) customers.get(0); customer.updateStuff(...); session.update(customer); } tx.commit(); session.close(); – Flush and Clear the Session regularly – Use the StatelessSession Interface  Bypass cache  No dirty-checking  Ignores collections on entities  Bypass Hibernate interceptors and event model © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 19
  • 20. Sanity Check – – – Addresses the ‘Object-Relational Impedance Mismatch’  Good marketing. ORM is the impedance mismatch. It creates the inefficient connection.  DBs provide permanent storage. Programming languages process data in step-wide fashion. Morphing both is a mismatch. Promoting their strengths is the ideal scenario. Transparent  You’re in control of the level of depth Hibernate permeates your codebase.  Expect some magic. Querying Facilities (HQL, Criteria API, JPAQL, SQL)  In the end, they output SQL. Ugly SQL. © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 20
  • 21. Sanity Check – Database-Agnostic  Who cares? – “Performance”: Lazy initialization, fetching strategies, versioning, etc…  Faster than what?  If performance is a concern, raw JDBC/SQL is your answer. – Caching:  Databases already offer this.  Roll your own cache  Beware of cached data modified by other applications. – Automatic DDL generation  Never use this! © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 21
  • 22. My Hibernate Divorce – Scenario:  Analytics company processing several million hits, email clicks, email opens, and form posts per day.  Over 800 customer in multi-tenant-style schema  75+ tables per schema  2 MySQL servers in Master-Slave configuration  Application servers from 8GB to 16GB physical memory. – Hibernate Use:  100%  Originally JSF+Hibernate = Rapid Development © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 22
  • 23. My Hibernate Divorce – Problem #1:  Random OOMs – Diagnosis:  Boot up Jprofiler. Nothing there.  Analyze heap dump file: – Hibernate holding 5MB per SessionFactory in memory, ~4GB per server  Reporting and other intensive operations cause OOM  By design, lazy-loading was disabled. © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 23
  • 24. My Hibernate Divorce – Solution:  Add more RAM. Could not scale horizontally.  Load balance customers across servers (ala SFDC)  Rethink lazy-loading strategy: – Massive re-architecture – Suboptimal in certain parts of the system. • Recording hits/clicks/opens • Reporting  Rip out Hibernate for memory intensive operations. – – – – Table per Class – this was going to take a while. Perform processing in DB, not code! That’s what they’re designed to do! Some stored proc. Get a DBA. © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 24
  • 25. My Hibernate Divorce – Problem #2:  Slow boot time: ~20 minutes.  With hibernate update 2+ hours. (and system crashes)  Marketing people aren’t patient. – Diagnosis:  Hibernate SessionFactory creation across 800 schema can be slow. ;-)  hbm2ddl.auto=update is evil – Solution:  hbm2ddl.auto=none – Handle schema updates manually (yay, PHP!) – Migration and adapting data a manual process.  Load SessionFactories on-demand. – When user logs in, load SessionFactory  Reap stale SessionFactories © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 25
  • 26. Final Thoughts Anti-Pattern: 1. Some repeated pattern of action, process or structure that initially appears to be beneficial, but ultimately produces more bad consequences than beneficial results, and 2. A refactored solution exists that is clearly documented, proven in actual practice and repeatable. If you really must use Hibernate: – Never let Hibernate design your schema – Your schema will outlive your application – Learn SQL, Stored Proc, and database design principles. – Proper indexing is paramount! Get a DBA. – Consider a mix: 50% Hibernate (75% or 90%) © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 26
  • 27. And Today… “... ‘a lot of open source projects just outgrow Hibernate. Hibernate was great to get started and that although it has a richer set of capabilities than iBatis, sometimes it just gets in the way’.” - John Newton, CTO, Alfresco © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 27
  • 28. Questions? © 2011 Altisource Portfolio Solutions S.A. All rights reserved. Proprietary and Confidential. Page | 28

Notes de l'éditeur

  1. http://www.hibernate.org/about/why-hibernate.html