Hibernate large amount of data operation solution

Source: Internet
Author: User

After reading the hibernate reference, you can use batch processing, when the inserted data exceeds 10000, flush session and clear.

Here is a test method.

1/**//**
2 * Test Batch Insert data transaction, return succeeded
3 *
4 * @param objpo Object
5 * @return BOOLEAN
6 */
7 public boolean insertbatch (final Object objpo) {
8 Boolean issuccess = false;
9 Tr Ansaction transaction = NULL;
Sessions session = Opensession ();
One try {
transaction = Session.begintransaction ();
for int i = 0; i < 100000 ; i + +) {
Session.save (OBJPO);
if (i% = 0) {
//flush a batch of       Inserts and release memory
Session.flush ();
Session.clear ();
}
20
Transaction.commit ();
Logger.info ("transaction.wascommitted:"
+ transaction.wascommitted ());
Issuccess = true;          
Hibernateexception ex) {
(transaction!= null) {
try {
28 TraNsaction.rollback ();
Logger.error ("Transaction.wasrolledback:"
+ transaction.wasrolledback ());
To catch (Hibernateexception Ex1) {
Logger.error (ex1.getmessage ());
Ex1.prin Tstacktrace ();
34}
35}
Logger.error ("Insert Batch PO Error:" + ex.getmessage ());
Notoginseng ex.printstacktrace ();
-} finally {
41-if (transaction!= null) {
transaction = null;
:}< br> Session.close ();
43}
return issuccess;
45}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.