-->
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.  [ 6 posts ] 
Author Message
 Post subject: Can I commit more than once on a Transaction?
PostPosted: Mon Apr 24, 2006 11:55 am 
Beginner
Beginner

Joined: Tue Mar 15, 2005 1:26 am
Posts: 20
I have a EAI application. need to import data file(csv format) into system.
some of the EAI process may cause more than 20 minutes. So I want to commit after process some data. the code is like this:

Code:
Session sess = factory.openSession();
Transaction tx;
try {
     tx = sess.beginTransaction();

[b]     while(...){
         //do some work
         ...
         tx.commit();
     }[/b]
}
catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
}
finally {
     sess.close();
}


Is there any problem ?
If can't use this, how to change the code?

_________________
OR maping? not familar. seems easy select, hard delete/update


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 12:24 pm 
Newbie

Joined: Mon Apr 24, 2006 11:43 am
Posts: 4
Location: Edinburgh
The commit will flush the session so it wont work properly on the second commit. I would suggest trying something like this (though probably not exactly).

Code:


Session sess = factory.openSession();
Transaction tx;

try {
    while(...){
       try{
            tx = sess.beginTransaction();
            //do some work
            tx.commit();
       catch (Exception e){
            if (tx!=null) tx.rollback();
            throw e;
       }
     }
       catch (Exception e){
          .....
       }
finally {
     sess.close();
}


That will commit each time through as a new transaction. I can't for definate say what you've got wont work but it just looks wrong.

Andrew Aitken


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 1:28 pm 
Beginner
Beginner

Joined: Fri Feb 17, 2006 1:28 pm
Posts: 24
This is done with Batch Processing. All you need to do is when you reach a certain amount of reconds, flush and clear the session...

Code:
Session sess = factory.openSession();
Transaction tx;

try {
    int 1 = 0;
    while(...){
       try{
            tx = sess.beginTransaction();
            //do some work
            // here you also call sess.save(obj);
            if(i % 100 == 0) {
                sess.flush();
                sess.clear();
             }
       catch (Exception e){
            if (tx!=null) tx.rollback();
            throw e;
       }
       tx.commit(); 
     }
       catch (Exception e){
          .....
       }
finally {
     sess.close();
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 29, 2006 11:52 am 
Beginner
Beginner

Joined: Tue Mar 15, 2005 1:26 am
Posts: 20
is it good to use one session for many commit? or need to create different session for different commit?
there are 2 different ways:
1. get session --> begin transaction -> update -> commit -> get session --> begin transaction -> update -> commit -> get session --> begin transaction -> update -> commit ...

2. get session --> begin transaction -> update -> commit -> begin transaction -> update -> commit -> begin transaction -> update -> commit ...

which is better?

_________________
OR maping? not familar. seems easy select, hard delete/update


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 1:15 pm 
Beginner
Beginner

Joined: Tue Mar 15, 2005 1:26 am
Posts: 20
any help?

_________________
OR maping? not familar. seems easy select, hard delete/update


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 11:22 am 
Beginner
Beginner

Joined: Fri Feb 17, 2006 1:28 pm
Posts: 24
Sorry its been a while since I have been on this post.

Yes, that’s no problem at all. You want to keep it within the same session. That’s important if you need to roll back the entries.

When you call session.flush() and session.close() that *Doesn’t* write these objects to the database. It writes them to the DB engine and if the session is committed, then these objects will be committed to the DB, otherwise they will be rolled back.

If you don’t flush and clear the session, chances are you will run out of memory and the batch will start running incredibly slow.


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