Hello,
I'm using the raw JDBC Connection for a Java clob implementation (db doesn't support it). So I get it from the Hibernate session.
I ran into a problem when attempting to use it after the session had already been used (to do a retrieval using list()), I get an exception thrown. The first exception I saw said that the Hibernate session was not connected. So i added code if !session.isConnected session.reconnect, and that exception went away, but in its place I get this SQLException:
java.sql.SQLException: Connection handle has been closed and is unusable
This goes away if I retrieve a new Session for the clob insertion (instead of trying to reuse the one used for the list() call). But why is this? Shouldn't a session be able to be used for a list, and then its underlying connection be used?
Code:
Session session = null;
try {
SessionFactory factory = (SessionFactory)ServiceLocator.getInstance().getHibernateSessionFactory();
session = factory.openSession(getConnFromDataSource(dataSourceName));
} catch (SQLException e) {
log.error(e);
}
// this part works fine
net.sf.hibernate.Criteria crit = session.createCriteria(klass);
List coll = crit.list();
// now when attempting to use Connection from session, exception thrown
Connection conn = null;
try {
// Get Connection from Hibernate session
if (!session.isConnected())
session.reconnect();
conn = session.connection();
if (conn != null) {
try {
PreparedStatement ps = connection.prepareStatement(CLOB_QUERY);
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
String value = assembleCLOB(rs);
rs.close();
ps.close();
return value;
} catch (Exception e) {
log.error("Unable to retrieve CLOB for VO[" + obj + "] " + e.getMessage());
throw new DataAccessException("Unable to retrieve CLOB for VO: " + obj, e);
} finally {
try {
conn.close();
} catch (SQLException e) {
log.info("Error closing connection in clob method: ", e);
}
}
}
} catch (HibernateException e) {
log.error("Cannot retrieve Connection from Hibernate session: ", e);
throw new DataAccessException("Cannot retrieve Connection from Hibernate session: ", e);
}
Hibernate 2.0.3