-->
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.  [ 7 posts ] 
Author Message
 Post subject: mysql connection closed
PostPosted: Sun Sep 07, 2008 11:12 am 
Newbie

Joined: Sat Aug 23, 2008 9:10 am
Posts: 5
i'm using hibernate3.

is my dao using the right way to retrieve object from mysql db ? i don't close connection, and i use hibernatesessionfactory.currentSession() to always get current session

Code:
public MyMember getMyMember(String name) {
        MyMember MyMember = new MyMember();
        try {
           
            Session sess = HibernateSessionFactory.currentSession();
            sess.clear();
            HibernateSessionFactory.beginTransaction();
            MyMember = (MyMember)sess.get(MyMember.class, name);
            HibernateSessionFactory.commitTransaction();
        } catch(HibernateException e) {
            //placeholder
            System.out.println("There was a problem retrieving the MyMember: " + name + " : " + e.getMessage());
            HibernateSessionFactory.rollbackTransaction(); //i get exception on this line
        }
       
       
        return MyMember;
    }








sometimes, i will get error like below

Code:
0:53:20 PM com.mchange.v2.c3p0.impl.NewPooledConnection handleThrowable
WARNING: [c3p0] Another error has occurred [ com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state. ] which will not be reported to listeners!
com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
   at com.mysql.jdbc.Connection.getMutex(Connection.java:3640)
   at com.mysql.jdbc.Connection.rollback(Connection.java:5088)
   at com.mchange.v2.c3p0.impl.NewProxyConnection.rollback(NewProxyConnection.java:855)
   at org.hibernate.transaction.JDBCTransaction.rollbackAndResetAutoCommit(JDBCTransaction.java:183)
   at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:162)
   at com.theor.Member.model.HibernateSessionFactory.rollbackTransaction(HibernateSessionFactory.java:125)
   at com.theor.Member.model.MyMemberDao.getMyMember(MyMemberDao.java:57)
   at com.theor.Member.model.HttpMember.service(HttpMember.java:255)
   at $IEngineService_11c3795d1c9.service($IEngineService_11c3795d1c




my hibernate.cfg.xml configuration

Code:
 
        <property name="c3p0.min_size">10</property>
        <property name="c3p0.max_size">100</property>
        <property name="c3p0.timeout">10</property>
        <property name="c3p0.acquireRetryAttempts">30</property>
       
        <property name="c3p0.acquireIncrement">5</property>
       
        <property name="c3p0.idleConnectionTestPeriod">300</property>
       
        <property name="c3p0.initialPoolSize">20</property>
        <property name="c3p0.maxPoolSize">100</property>
        <property name="c3p0.maxIdleTime">300</property>
        <property name="c3p0.maxStatements">50</property>
        <property name="c3p0.minPoolSize">10</property>
       
       
       
        <!-- Transaction isolation 2 = READ_COMMITTED -->
        <property name="connection.isolation">2</property>       
       




//my hibernate factory look like this

Code:


public static Session currentSession() throws NestedRuntimeException {
            Session session = (Session) threadSession.get();
            try {
                if (session == null) {
                    session = sessionFactory.openSession();
                    threadSession.set(session);
                }
         } catch(HibernateException e) {
             throw new NestedRuntimeException("There was a problem retrieving the current session",e);
         }
            return session;
    }


    public static void commitTransaction() throws NestedRuntimeException {
        try {
            Transaction tx = (Transaction)threadTransaction.get();
           
            if(tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
                tx.commit();
            threadTransaction.set(null);
        } catch (HibernateException e) {
           
        }
       
    }
   
    /**
     * Rollback a hibernate transaction
     *
     * @throws NestedRuntimeException
     */
    public static void rollbackTransaction() throws NestedRuntimeException {
        try {
            Transaction tx = (Transaction)threadTransaction.get();
            threadTransaction.set(null);
            if(tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
                tx.rollback();
            closeSession();
        } catch (HibernateException e) {
            throw new NestedRuntimeException("There was a problem rolling back the transaction", e);
        }
       
    }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 8:03 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, you don't want to be opening sessions and managing transactions within the DAO - that will cause problems. Make sure the client is opening the session and demarcating the transaction.

Using Hibernate DAOs in a Servlet Web Application (Data Access Objects)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 12:16 am 
Newbie

Joined: Sat Aug 23, 2008 9:10 am
Posts: 5
i already changed my codes to include finally { session.close} . is that what u mean?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 9:46 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Are you managing the session inside the DAO, or outside of the DAO. That's what I mean.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 10:26 pm 
Beginner
Beginner

Joined: Wed Oct 03, 2007 9:46 pm
Posts: 37
i will open and close session for each transaction in dao


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 12:40 pm 
Newbie

Joined: Thu Oct 26, 2006 11:50 am
Posts: 17
Location: Chesterfield, VA
Make sure that you have autoReconnect set to true on your connection URL

jdbc:mysql://[host]:[port]/[databaseName]?autoReconnection=true


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 12:40 pm 
Newbie

Joined: Thu Oct 26, 2006 11:50 am
Posts: 17
Location: Chesterfield, VA
Make sure that you have autoReconnect set to true on your connection URL

jdbc:mysql://[host]:[port]/[databaseName]?autoReconnection=true


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