Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: JDBC batching, IDENTITY generator, and StatelessSession
PostPosted: Wed Aug 30, 2017 7:37 am 
Newbie

Joined: Wed Aug 30, 2017 7:29 am
Posts: 4
Greetings!

Docs wrote:
Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator.

i guess because batch inserts can not return many generated values
but for Stateless sessions they can be ignored, right?
So, may be that rule can be muted for StatlessSessions?

Bless you..


Top
 Profile  
 
 Post subject: Re: Batching, IDENTITY, StatelessSession
PostPosted: Wed Aug 30, 2017 8:13 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1439
The reason why IDENTITY generated does not mix with JDBC batch inserts is because by the time flush comes, Hibernate already executed the inserts to know the entity identifier upon attaching the entity to the currently running Persistence Context.

However, because the StatelessSession does not have a Persistence Context, it has no way of delaying the inserts so that they can be batched during flushing, right?

Prior to joining the Hibernate team, I've been also involved in developing enterprise applications using Hibernate, especially Batch Processors. However, there was no instance where we ever needed to use the StatelessSession which has many inconveniences:

- does not implement a first-level cache
- nor interact with any second-level cache
- nor does it implement transactional write-behind or automatic dirty checking
- nor do operations cascade to associated instances
- Collections are ignored by a stateless session.
- bypass Hibernate's event model and interceptors (not very good for Envers either).
- vulnerable to data aliasing effects, due to the lack of a first-level cache.

So, we've been using the standard Session for all our batch processing. For more details about the best way of batching using JPA and Hibernate, [url=
https://vladmihalcea.com/2017/04/25/the ... hibernate/]check out this article[/url].

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Batching, IDENTITY, StatelessSession
PostPosted: Wed Aug 30, 2017 8:32 am 
Newbie

Joined: Wed Aug 30, 2017 7:29 am
Posts: 4
vlad wrote:
However, because the StatelessSession does not have a Persistence Context, it has no way of delaying the inserts so that they can be batched during flushing, right?


Absolutely, it seems hibernate doing mistake extending that rule for StatelesSession too
StatelessSession inconveniences is not subject of my question, but thanks for that.

IDENTIY in my solution potentially can provide better performance
Because SequenceStyleGenerator has extra queries to sequences


Top
 Profile  
 
 Post subject: Re: JDBC batching, IDENTITY generator, and StatelessSession
PostPosted: Wed Aug 30, 2017 8:38 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1439
Quote:
Because SequenceStyleGenerator has extra queries to sequences


You're jumping to conclusions here without actually benchmarking.

As I explained in my book, High-Performace Java Persistence, SEQUENCE can benefit from pooled and pooled-lo optimizers which reduce the overhead of extra DB roundtrips.

However, calling the DB sequence is actually extremely fast.

Compare the overhead of IDENTITY (blue)

Image

with the one for SEQUENCE (blue) on the following chart:

Image

So, no need to use IDENTITY if your DB supports SEQUENCE.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: JDBC batching, IDENTITY generator, and StatelessSession
PostPosted: Wed Aug 30, 2017 8:47 am 
Newbie

Joined: Wed Aug 30, 2017 7:29 am
Posts: 4
In case of Oracle RDBMS
Identity is just wrapper under Sequence


Top
 Profile  
 
 Post subject: Re: JDBC batching, IDENTITY generator, and StatelessSession
PostPosted: Wed Aug 30, 2017 8:51 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1439
That's the case for PostgreSQL as well. However, it's much more efficient for Hibernate to use a SEQUENCE than IDENTITY.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: JDBC batching, IDENTITY generator, and StatelessSession
PostPosted: Wed Aug 30, 2017 8:54 am 
Newbie

Joined: Wed Aug 30, 2017 7:29 am
Posts: 4
I got it, thank you very much!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.