-->
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.  [ 2 posts ] 
Author Message
 Post subject: SQLException: connection closed when use session.connection
PostPosted: Thu Feb 19, 2004 9:02 pm 
Beginner
Beginner

Joined: Wed Oct 08, 2003 4:22 pm
Posts: 29
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 4:13 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You give hibernate a user connection: thus Hibernate does not manage it. Are you sure, your code don't do some weird stuff with the connection.
PS: I think you should close hib session before closing the connection

_________________
Emmanuel


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