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.  [ 10 posts ] 
Author Message
 Post subject: Session Closing - Urgent
PostPosted: Thu Aug 25, 2005 1:05 pm 
Newbie

Joined: Mon Mar 07, 2005 7:18 pm
Posts: 16
Hi,

I've a question regarding session closing. I need to perform a batch transaction several times (read data froma flat file and update the database).

Here's the code that updates the database.

public void update(IMessage accountMessage, String action)
throws HibernateException {
Session session = null;
Transaction tx = null;
try {
session = HibernateSession.currentSession();
tx = session.beginTransaction();
ProcessHandler acctUserLoaderLog = new
AcctUserLoaderLogHandler();
acctUserLoaderLog.load(accountMessage, action);

} catch (Exception e) {
logger.error("Error happened")
} finally
{
try {
if (tx != null)
tx.commit();
tx = null;
if (session != null){
session.close();
}
}


I call update method for every record I read from the file.
Should I call close session for every record. or call just once. Becuase as it is, it throws an exception 'Session already closed'

Thu Aug 25 11:31:52:186 CDT 2005 MM:ERROR net.sf.hibernate.HibernateException: Session is closed
net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3292)
net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:65)
net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:779)
net.sf.hibernate.loader.Loader.doQuery(Loader.java:265)
net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
net.sf.hibernate.loader.Loader.list(Loader.java:1024)
net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1544)


Thanks for your time
Ram
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 1:09 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
open session, begin transaction, update-update-update, commit transaction, close session . do not close the session after each update.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 1:23 pm 
Newbie

Joined: Mon Mar 07, 2005 7:18 pm
Posts: 16
Thanks Dennis.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 3:30 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Great. Now can you log back in, and click on the little "yes" link right next to "Did it solve the problem?"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 3:59 am 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
dennisbyrne wrote:
open session, begin transaction, update-update-update, commit transaction, close session . do not close the session after each update.


Is this safe for like millions of records ?

open session, begin transaction, get-get-get, update, get-get-get, update, get-get-get, commit transaction, close session

I'm pretty sure I had to use evict and flush to make my job run sucessfully. The job scans a whole table looking for candidates to do work on, marks them and hands off the ID to another thread to perform business logic centered around it.

The pattern you suggest works well for the business logic operation centered around an object, but for jobs that do full table scans ?

Maybe I should write a WiKi entry on that type of application, or maybe I am just missing the point.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 4:20 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Table scans and row count are irrelevant. Evict, Flush? Not sure why an evict is necessary and each Transaction.commit is an implicit flush anyways.

You mention "another thread". If you are using the HibernateUtil strategy, you will need something more complex - as each thread potentially has it's own session.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 12:18 pm 
Newbie

Joined: Mon Mar 07, 2005 7:18 pm
Posts: 16
Yes. It worked. But when I process million of transaction should I call session.clear() periodically like once every 1000 records or so.

Thanks for your time
Ram


Top
 Profile  
 
 Post subject: look in the documentation
PostPosted: Fri Aug 26, 2005 12:42 pm 
Beginner
Beginner

Joined: Thu Jun 23, 2005 4:11 pm
Posts: 24
http://www.hibernate.org/hib_docs/v3/reference/en/html/batch.html#batch-update


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 2:06 pm 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
dennisbyrne wrote:
Table scans and row count are irrelevant. Evict, Flush? Not sure why an evict is necessary and each Transaction.commit is an implicit flush anyways.

You mention "another thread". If you are using the HibernateUtil strategy, you will need something more complex - as each thread potentially has it's own session.


I think I read that Hibernate keeps all the get's in its level 1 cache, so for a whole table scan memory just fills up with rows that are now dead. I suppose what I'm putting up is that there are cases where the pattern the topic creator is already using would be better than the one you are citing, and some cases where the one you are citing would be fine if you evaluate you need to call evict() is necessary.

But I accept completely the one you are citing is the general case for most "business operation" work.

I may do 250,000 get's between each modify and flush(), so I forcefully evict() the dead objects once I've finished processing that row.

The other thread works fine in the pattern you are describing, both threads have their own Session object to work from.

Ram/chicagorarn: If you are doing a beginTransaction() commit() there is no need to call Session.clear() once a while. My problem is where you call beginTransaction() and then go on to do a get() for every row of a huge table, one huge transaction. Really in my case the processing of each row is a transaction in itself, but that then de-generates the design pattern into the one you started with.


Top
 Profile  
 
 Post subject: Sessions have to keep object references for preserving == id
PostPosted: Sun Aug 28, 2005 2:17 pm 
Newbie

Joined: Sun Aug 28, 2005 2:13 pm
Posts: 9
Sessions keep all persistent objects in memory to provide == identity. This even works across transaction boundaries. Thus it is necessary, to actually call clear() on the session or to evict the objects to free that memory. Merely flushing (which is done on every commit) is not sufficient.


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