Anonymous7
Anonymous7

Reputation: 41

Performance issue with hibernate insert

I am new to hibernate. Please correct me if any of the below is wrong.

There is an entity which is using a sequencegenerator for id generation. Because of this every insert is having two calls one to get sequence and other to do the insert

@Id
@SequenceGenerator(name="i_gen", sequenceName="b_seq", allocationSize=6)
@GeneratedValue(generator="i_gen")
private Long id;

During my research about allocationsize i understood that on specifying the allocation size, we will not call db for next few allocatedsize but simply increment by 1. But i don't see this behavior. Please correct me if i am wrong.

The following are the sql logs that i see

[INFO ] [26082016.002159] sqltiming:357  - select b_seq.nextval from dual  {executed in 69 msec}
[INFO ] [26082016.002200] sqltiming:357  - select b_seq.nextval from dual  {executed in 174 msec}
[INFO ] [26082016.002200] sqltiming:357  - select b_seq.nextval from dual  {executed in 74 msec}
[INFO ] [26082016.002200] sqltiming:357  - select b_seq.nextval from dual  {executed in 77 msec}
[INFO ] [26082016.002200] sqltiming:357  - select b_seq.nextval from dual  {executed in 78 msec}
[INFO ] [26082016.002200] sqltiming:357  - select b_seq.nextval from dual  {executed in 161 msec}

For 6 objects insert in a single transaction, i see 6 calls to db to get nextval and 6 inserts. I optimized the inserts by using batch inserts. However i am unable to get around the seq generator issue. I see a huge performance hit because of this.

Is there anything else for this not to make so many db calls for getting seqnumber?

Upvotes: 1

Views: 1366

Answers (1)

veljkost
veljkost

Reputation: 1932

You would have to set your DB sequence to INCREMENT BY 6. As described here The allocationSize=n means: "Go and fetch the next value from the database once in every n persist calls. And locally increment the value by 1 in between.". Also, check out this answer on SO.

Upvotes: 1

Related Questions