-->
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.  [ 2 posts ] 
Author Message
 Post subject: committing/persisting without closing the connection.
PostPosted: Sat Jan 03, 2009 9:06 pm 
Newbie

Joined: Sat Jan 03, 2009 8:03 pm
Posts: 3
Hello.

i'm relative new to JPA/Hibernate and therefore have following problem:

I leech files, convert em to csv, and load them into a database.

For each leeched file i create a status-object, which holds a list of log-objects. After each step in the process, i create a log-object with some infos and add it to the log-object list within the status-object (each file gets its own status-object).

so on each new log-object within the list, the status-object should be persisted in the database.

so if my app crashes/ a power blackout occurs / a meteor hits the server / etc. ;) there should be the last step logged in the database.

i act as follows:

i create an EntityManager:

Code:
EntityManagerFactory emf;
EntityManager  em;

public void init(String unitName)
   {
      emf = Persistence.createEntityManagerFactory(unitName);
      em = emf.createEntityManager();
   }


Leech a file and after its local, create a status-object WITHOUT logs and persist it.

Code:
public void persist(Object o)
   {
         em.persist(o);
          em.flush();
   }



After this is done, i start processing the file and after each step i'm creating a log-object and add it to the logs-list within the status-object and call em.flush()

but the flush() saves the changes non-permanent, if my app crashes after the flush() no log would be in the database.

I have to call em.commit() after EACH change in the statusobject (e.g. adding a log) to make it persistent in the DB.
But commit() closes the transaction and i have to reconnect to the DB, search the status-object, add a new log and commit() again, and this for each change/log entry, IMHO thats way to expensive.

I have the feeling that there has to be another solution, i looked up the session-thing, but hibernate just describes making all changes and then commit in the end.
This makes sense in most cases, rollback clears all up etc. But i explicitly want EVERY change directly written into the database.

I would be glad if someone could point me into the right direction :), thanks for it in advance, and thanks for reading to this point :)

sincerely yours,
chillum


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2009 5:07 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
u may use batch processing for this. for example set jdbc.batch_size=10 in hibernate.cfg.xml file and then if the size of the object reaches to 10, call flush() and clear() method session. it will automatically commit all the objects into DB without calling transaction commit() method.

_________________
If u feel it will help you, don't forget to rate me....


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