In my application, I had a case where I needed to count no. of users from DB. So for doing this in my DAO Implementation I used the below code at first:
1.
Code:
public long getUserCount() {
return (Long) getHibernateTemplate().iterate("select count(*) from User").next();
}
}
Now this throws the below exception:
------------------------------------------------------------------------------------------------------------------------------
org.hibernate.exception.GenericJDBCException: could not get next iterator result
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.handledNonSpecificException (SQLStateConverter.java: 103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) at org.hibernate.exception.SQLStateConverter.convert (SQLStateConverter.java: 91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.exception.JDBCExceptionHelper.convert (JDBCExceptionHelper.java: 43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29) at org.hibernate.exception.JDBCExceptionHelper.convert (JDBCExceptionHelper.java: 29)
at org.hibernate.impl.IteratorImpl.next(IteratorImpl.java:125) at org.hibernate.impl.IteratorImpl.next (IteratorImpl.java: 125)
at bo.client.dao.ClientDAOHibernate.getListeParNom(ClientDAOHibernate.java:70) at bo.client.dao.ClientDAOHibernate.getListeParNom (ClientDAOHibernate.java: 70)
at tools.App.main(App.java:25) at tools.App.main (App.java: 25)
Caused by: java.sql.SQLException: Operation not allowed after ResultSet closed Caused by: java.sql.SQLException: Operation not allowed after ResultSet closed
------------------------------------------------------------------------------------------------------------------------------
Since this was not working, I used another way to implement my problem
2. Spring DataAccessUtils
Code:
public long getUserCount() throws DataAccessException {
return DataAccessUtils.longResult(getHibernateTemplate().find("select count(*) from User"));
}
This works smoothly.
3. Hibernate Callback method
If I implement solution for my problem using HibernateCallBack this too works smoothly, see below code
Code:
public long getUserCount(){
long count;
count = (Long) getHibernateTemplate().execute (new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException,SQLException {
Query query = session.createQuery("select count(*) from User");
return ( (Long) query.iterate().next() ).longValue();
}
});
}
Hence, the only doubt that remains for me is why method (1) --> getHibernateTemplate.iterate
throws me an exception. Since Im using spring's getHibernateTemplate, I assume that all works related to
Hibernate Session, resultset would be taken care by this template, then why am I getting this weird exception that "Resultset is already closed" for method (1) and not for method (2) & (3).
Thnx
Nitesh