-->
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.  [ 8 posts ] 
Author Message
 Post subject: ReleaseMode with Iterator
PostPosted: Mon Jan 09, 2006 3:13 pm 
Newbie

Joined: Mon Dec 19, 2005 5:36 pm
Posts: 14
We have a problem with connection. We access some objects with iterator, for each of them, there is a transaction. The code is like this (not exact code, but pretty self-documented):

Code:
ThreadLocalSession.beginSession();
Session sess = ThreadLocalSession.getCurrentSession();
Query q = sess.createQuery(QUERY_OBJECT);
Iterator ite = q.iterate();

while(ite.hasNext()){
  Object o = ite.next();
  ThreadLocalSession.beginTransaction();

  // Blabla

  ThreadLocalSession.endTransaction();
}


Sometime, we get the exceptioin said that we can not get next object because the connection is closed. I investigated the issue, and find out that the session does not held an jdbc connection all the time. When to get/release connection is controled by ReleaseMode. For our case, the session release the connection every time it commit a transaction. We have to change the relese mode to ON_CLOSE. It works. But the document said that is is hightly discouraged, which is pretty scary.

1. What I do not understand is that session is getting/realseing connection all the time, sometimes it works, sometimes it does not. Should it not work at all?
2. How is hibernate iteration implemented? Is it using database cusor?
3. Why ReleaseMode.ON_CLOSE is highly discouraged?

It seems like a pretty general question. Any idea/information is welcomed!

Thanks

Error:

Exception in thread "Thread-11" org.hibernate.exception.GenericJDBCException: could not get next iterator result
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.impl.IteratorImpl.next(IteratorImpl.java:125)
at com.brightcove.payments.PaymentSchedulerFacade.processRecordForConsumerAccount(PaymentSchedulerFacade.java:328)
at com.brightcove.payments.PaymentSchedulerFacade.createConsumerInvoiceFromConsumerRecord(PaymentSchedulerFacade.java:111)
at com.brightcove.payments.ThreadConsumerPurchases.run(PaymentSchedulerFacadePayMediaLongTest.java:292)
Caused by: java.sql.SQLException: Closed Connection: next
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
at oracle.jdbc.driver.DatabaseError.check_error(DatabaseError.java:861)
at oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:171)
at org.hibernate.impl.IteratorImpl.postNext(IteratorImpl.java:83)
at org.hibernate.impl.IteratorImpl.next(IteratorImpl.java:120)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 6:14 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
I propose to put your transaction around your loop. You are loading data outside of the transaction. May be this leads to the problem.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 7:07 pm 
Newbie

Joined: Mon Dec 19, 2005 5:36 pm
Posts: 14
Yes. That is the problem. But there is something we want to do, becuase we have to iterate thousands of objects.

I am trying to understand how iteration is implemented. It seems depends on what connection pool you are using. And, in term of our transaction setup, different connection has different behaviors.

Dongsheng


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 7:10 pm 
Newbie

Joined: Mon Dec 19, 2005 5:36 pm
Posts: 14
I mean I really need to keep the transacton setup (thousand of small transaction instead of a huge transaction)

Is anyway to make it work instead of change the ReleaseMode to ON_CLOSE?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 7:10 pm 
Newbie

Joined: Mon Dec 19, 2005 5:36 pm
Posts: 14
Thanks for reply.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 7:16 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
I assume that you have little influence what the session is doing outside an transaction except the solution you found.

Just some ideas to work around
a)
if you are not obliged to use small transaction, use pagination to iterate through less than your total items. eg. 100 per each transaction
b)
fetch the ids into an array/list and loop over this list and fetch each item inside a transaction

Regards Sebastian

Please rate, if this helped.

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 7:34 pm 
Newbie

Joined: Mon Dec 19, 2005 5:36 pm
Posts: 14
Thanks. that helped.

I am still curious about how iterator is implenmented here.

Dongsheng


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 7:53 pm 
Newbie

Joined: Mon Dec 19, 2005 5:36 pm
Posts: 14
Actually, one thing I really do not understand is that for each iteration, the connection is new, but somehow the .next() still return correction results, especially for the one thread case.

By the way, I am uisng Hibernate built in connection provider.

DS


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