-->
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.  [ 4 posts ] 
Author Message
 Post subject: disconnecting session gets SQLException: Already closed
PostPosted: Thu Dec 11, 2003 6:05 pm 
Beginner
Beginner

Joined: Wed Sep 10, 2003 5:32 pm
Posts: 28
Hi everyone,

I got the following exception when my application was trying to close the session:

15:19:14,802 DEBUG SessionImpl:3201 - disconnecting session
15:19:14,838 DEBUG JDBCExceptionReporter:36 - SQL Exception
java.sql.SQLException: Already closed.
at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:111)
at net.sf.hibernate.connection.DBCPConnectionProvider.closeConnection(DBCPConnectionProvider.java:48)
at net.sf.hibernate.impl.BatcherImpl.closeConnection(BatcherImpl.java:275)
at net.sf.hibernate.impl.SessionImpl.disconnect(SessionImpl.java:3217)
at net.sf.hibernate.impl.SessionImpl.close(SessionImpl.java:547)
at com.accucast4.customerdatasource.DbCustomerDataSourceImp.deleteRecipientList(DbCustomerDataSourceImp.java:2777)
at com.accucast3.buildmanager.TrialBuilder.run(TrialBuilder.java:226)

I might have done something wrong on my coding level for managing JDBC connection manually. Here is the code that is causing problem:
Code:
        Session session = null;
        Connection conn = null;
        PreparedStatement ps = null;
        StringBuffer deleteString = null;
        int deleteCount = 0;
        try {
            String deleteSql = "something delete sql";
            logger.debug("delete sql statement:" + deleteSql);
            session = (Session)getConnection();
            conn = session.connection();
            ps = conn.prepareStatement(deleteSql);
            ps.setLong(1, id);
            deleteCount = ps.executeUpdate();
            conn.commit();
        } catch (HibernateException he){
            logger.error("unable to delete something", he);
            try {
                if (conn != null){
                    conn.rollback();
                }
            } catch (SQLException sqlex){
                logger.error("unable to rollback something", sqlex);
            }
            throw new SomeException ("unable to delete something",
                                      1, he);
        } catch (SQLException sqlError){
            logger.error("unable to delete something", sqlError);
            try {
                if (conn != null){
                    conn.rollback();
                }
            } catch (SQLException sqlex){
                logger.error("unable to rollback delete something", sqlex);
            }
            throw new SomeException ("unable to delete something",
                                      1, he);
        } finally {
            try {
                if (ps != null)
                    ps.close();
            } catch (SQLException sqlexps){
                logger.error("unable to close preparedstatement", sqlexps);
            }
            try {
                if (conn != null){
                    conn.close();
                }
            } catch (SQLException sqlex){
                logger.error("unable to close session", sqlex);
            }
            try {
                if (session != null){
                    session.flush();
                    session.close();
                }
            } catch (HibernateException he){
                logger.error("unable to close session", he);
            }
        }
        return deleteCount;


My question is: do I need to do conn.close and session.close or should I just code with session.close?? I would really appreciate if anyone can give me some inputs on this question. Thanks.

Vivian Fonger


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 12:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I assume you are using one of the hibenate supplied database pool managers. In this case, don't close the connection even after you have used it with direct JDBC calls as hibernate is managing the database resource through the configured database pool. You will still need to close the session.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 12:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
BTW: All transcation management should still be done through the session and not the db connection. Only use the connection for doing the JDBC calls. Thhen you clean up result sets and prepared statements. Every thing else use the session object to control the database resource.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 10:49 am 
Beginner
Beginner

Joined: Wed Sep 10, 2003 5:32 pm
Posts: 28
Let me get this straight, I don't need to implement connection.close, I need to close resultset and preparedstatement. How about transaction??? According to the Hibernate manual, I still need to commit and/rollback through connection.commit() and connection.rollback(), is that still true????

Thank you very much.

Vivian


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