-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Batch processing
PostPosted: Mon Jun 30, 2008 11:45 am 
Beginner
Beginner

Joined: Fri May 16, 2008 3:59 pm
Posts: 30
Im trying to batch saveOrUpdate but I keep getting NonUniqueObjectException errors.

How can I pull of a batch saveOrUpdate where if one save/update fails, the whole process is rolled back?

Code:
session = HibernateUtil.getSessionFactory().getCurrentSession();
      tx = session.beginTransaction();
      for (int i = 0; i < records.size(); i++) {
         
            FinancialAccountId id = new FinancialAccountId(ACCOUNTNUMBER,
                  FUNDNAME, SSN);

            FinancialAccount fa = new FinancialAccount();
            fa.setId(id);
            fa.setDealernumber(DEALERNUMBER);
            fa.setAccountname(ACCOUNTNAME);
            fa.setBranchnumber(BRANCHNUMBER);
            fa.setEstablisheddate(ESTABLISHEDDATE);
            fa.setLastmaintenancedate(LASTMAINTENANCEDATE);

            fa.setAccounttype(ACCOUNTTYPE);
            fa.setAddress(ADDRESS);
            fa.setState(STATE);
            fa.setZipcode(ZIPCODE1 + " " + ZIPCODE2);
            fa.setSwp(SWP);
            fa.setNav(NAV);

            fa.setInterestedparty(INTERESTEDPARTY);
            fa.setRepid(REPID);
            fa.setBalance(new BigDecimal("00.00"));
            fa.setSubaccount(SUBACCOUNT);

            //hashmap
                               accountsToSave.put(i, fa);
         }//end for loop.

         
      }
      
      session = HibernateUtil.getSessionFactory().getCurrentSession();
      tx = session.beginTransaction();
      for(int i =0;i<accountsToSave.size();i++){
         session.saveOrUpdate(accountsToSave.get(i));
         if(i%20==0){
            session.flush();
            session.clear();
         }
      }
      tx.commit();
      session.close();


Top
 Profile  
 
 Post subject: Batch processing
PostPosted: Mon Jun 30, 2008 12:56 pm 
Beginner
Beginner

Joined: Fri May 16, 2008 3:59 pm
Posts: 30
This is what I came up with:

Code:
session = HibernateUtil.getSessionFactory().getCurrentSession();
      tx = session.beginTransaction();
      for(int i =0;i<accountsToSave.size();i++){
         session.saveOrUpdate(accountsToSave.get(i));
         
            session.flush();
            session.clear();
         
      }
      tx.commit();


Can I get an opinion? This is supposed to handle batch insert/update.
If one fails, the entire transaction should rollback.

Also,
when I tried to add:
Code:
if(i %20==0){
session.flush();
session.clear();
}


this gives me a NonUniqueObjectexception. I understand that the same object is being saved, but Am I supposed to use a new session for each object?


Top
 Profile  
 
 Post subject: Batch processing
PostPosted: Wed Jul 02, 2008 4:52 pm 
Beginner
Beginner

Joined: Fri May 16, 2008 3:59 pm
Posts: 30
Anyone?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 1:12 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Simply use a single transaction. If any write fails, roll back the transaction. As per the ACID properties of a proper transaction, if any part of the transaction fails, the whole process is rolled back to the point it was before the transaction began.

That's how transactions work!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.