Reputation: 623
I am trying to insert every 100 records from a file using hibernate batch insert.If i wrap the logic inside a transaction, is it going to work as expected, e.g lets say i am creating 3 batch out of 300 records and wrapping the logic inside a transaction ,then is the transaction going to roll back 1st and 2nd batch, if 3rd batch got a problem.
Upvotes: 0
Views: 2185
Reputation: 40036
In brief: it will works as expected.
There is no restriction in Hibernate that each batch insert needs to be in a separate transaction. If you perform several batch inserts in one transaction, they will be committed/rollback as a whole.
(of course, it is another story if you have nested transaction or using milestone in transaction. )
Upvotes: 0
Reputation: 80166
Always wrap your batch in a transaction. If you are batching 100 inserts per transaction then either all 100 will be committed if everything is fine. I'd also suggest you to use stateless sessions to speed up the batch inserts. I strongly recommend you to read the Batch processing tutorial of Hibernate documentation.
Upvotes: 3