-->
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.  [ 5 posts ] 
Author Message
 Post subject: MySQL Database connection with c3p0 fails (works only once)
PostPosted: Wed Oct 04, 2006 10:42 am 
Newbie

Joined: Wed Oct 04, 2006 10:24 am
Posts: 3
Hi everybody,

I am building a small CMS application with struts and hibernate on tomcat 5.5.17

Since I've discovered that the built in connection pool is not intended for production use, I am trying to switch over to c3p0 and there are my problems. (Using c3p0-0.9.1-pre6)

The connection with c3p0 works only for one database query, after that the connection is closed and I can't use it anymore. If I restart my tomcat, I am able to execute the next query and the connection is closed again and so on. With the built in connection pool (almost) everything works fine.

See the detailed information below:

Hibernate version:

3.1.3

Mapping documents:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
<!-- jdbc:hsqldb:hsql://localhost/newsletter -->
jdbc:mysql://localhost:3306/pmcms
</property>
<property name="connection.username">xxxxx</property>
<property name="connection.password">xxxxxx</property>



<!-- C3P0 connection pool -->

<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.max_size">100</property>


<!-- SQL dialect -->

<property name="dialect">

org.hibernate.dialect.MySQLDialect
</property>

<property name="current_session_context_class">thread</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>

<!-- Drop and re-create the database schema on startup
<property name="hbm2ddl.auto">create</property> -->


</session-factory>
</hibernate-configuration>



Code between sessionFactory.openSession() and session.close():


Code:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.*;

public class HibernateUtil {
   
   public static final SessionFactory sessionFactory;
   public static final ThreadLocal session = new ThreadLocal();
   
   static {
      try {
         // Create the SessionFactory from hibernate.cfg.xml
         sessionFactory = new Configuration().configure().buildSessionFactory();
         
         
      } catch (Throwable ex) {
         
         // Make sure you log the exception, as it might be swallowed
         System.err.println("Initial SessionFactory creation failed." + ex);
         throw new ExceptionInInitializerError(ex);
      }
   }   

   @SuppressWarnings("unchecked")
   public static Session currentSession() throws HibernateException {
      Session s = (Session) session.get();
      // Open a new Session, if this thread has none yet
      if (s == null) {
         s = sessionFactory.openSession();
         // Store it in the ThreadLocal variable
         session.set(s);
      }
      return s;
   }

   @SuppressWarnings("unchecked")
   public static void closeSession() throws HibernateException {
      Session s = (Session) session.get();
      if (s != null)
         s.close();
      session.set(null);
   }
    /**
    * @return  Returns the sessionFactory.
    * @uml.property  name="sessionFactory"
    */
    public static SessionFactory getSessionFactory() {       
        return sessionFactory;
    }   
   
   
}



Full stack trace of any exception that occurs:

ERROR - Servlet.service() for servlet action threw exception
org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:327)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:118)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:127)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1307)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:292)
at $Proxy0.beginTransaction(Unknown Source)
at org.pmedv.cms.daos.AbstractDAO.findAllItems(AbstractDAO.java:42)
at org.pmedv.actions.RegionListAction.execute(RegionListAction.java:30)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.SQLException: com.mchange.v2.c3p0.PoolBackedDataSource@1c2dad7 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSo
urce@d175ff [ acquireIncrement -> 1, acquireRetryAttempts -> 10, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAc
quireFailure -> false, checkoutTimeout -> 36000, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, factoryClassLocation -> null, fo
rceIgnoreUnresolvedTransactions -> false, identityToken -> d175ff, idleConnectionTestPeriod -> 0, initialPoolSize -> 1, maxIdleTime -> 0, maxPoolSize -> 100, ma
xStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 1, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@20ca8b [ description -> nul
l, driverClass -> null, factoryClassLocation -> null, identityToken -> 20ca8b, jdbcUrl -> jdbc:mysql://localhost:3306/pmcms, properties -> {user=******, passwor
d=******} ], preferredTestQuery -> null, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectivePro
xies -> false; userOverrides: {} ], dataSourceName -> 1c2dad7, factoryClassLocation -> null, identityToken -> 1c2dad7, numHelperThreads -> 3 ] has been closed()
-- you can no longer use it.
at com.mchange.v2.c3p0.PoolBackedDataSource.assertCpds(PoolBackedDataSource.java:254)
at com.mchange.v2.c3p0.PoolBackedDataSource.getPoolManager(PoolBackedDataSource.java:266)
at com.mchange.v2.c3p0.PoolBackedDataSource.getConnection(PoolBackedDataSource.java:120)
at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:35)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:324)
... 32 more


Using MySQL 4.1 and 5.0 under WinXP

Any help is really appreciated.

Greetings

Matthias


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 1:07 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
You do not actually show the Code between sessionFactory.openSession() and session.close(), unless I am missing something.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 6:58 pm 
C3P0 Developer
C3P0 Developer

Joined: Tue Jan 06, 2004 8:58 pm
Posts: 145
So, from what's posted, it's hard to say what, but something is closing your c3p0 DataSource. Your attempts to get a Connection are failing because apparently the DataSource has been explicitly closed.

If you have a hard time figuring out what inn your code is causing this to happen, configure the logger "com.mchange.v2.resourcepool.BasicResourcePool" to DEBUG (log4j) or FINEST (jdk14 logging). At this level, the resource pool will dump a debug stack trace of the thread closing the pool.

smiles,
Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 3:40 am 
Newbie

Joined: Wed Oct 04, 2006 10:24 am
Posts: 3
Thank you for your answers, in the meantime I figured it out.

Instead of calling

getSessionFactory().close();

I just had to flush the session after the update and everything works as expected.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 06, 2006 12:09 am 
Newbie

Joined: Sat Mar 18, 2006 1:27 am
Posts: 15
Pudutzki wrote:
Thank you for your answers, in the meantime I figured it out.

Instead of calling

getSessionFactory().close();

I just had to flush the session after the update and everything works as expected.


u can simply use Transaction.commit() which calls the flush()


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