Having problems making a connection when using c3pO. The only reason I'm trying to incorporate c3pO is that the connect is lost after an extended period of time of inactivity with MySQL. If there is a better way, let me know.
I'm using:
Hibernate3
MySQL 4.1
Java
hibernate.cfg.xml:
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- Use the C3P0 connection pool provider -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
...
Code to create seesion factory:
public static SessionFactory createSessionFactory( String database,
String username, String password, String server, int port,
boolean createDb )
throws HibernateException{
boolean flg = true;
if( flg ){
// first, close existing factory, if any
if (sessionFactory != null) {
try {
sessionFactory.close();
} catch (HibernateException he) {
} finally {
sessionFactory = null;
}
}
// now try to create a new one with the supplied credentials
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
cfg.setProperty( "hibernate.connection.url", "jdbc:mysql://" +
server + ":" + port + "/" + database );
cfg.setProperty( "hibernate.connection.username", username );
cfg.setProperty( "hibernate.connection.password", password );
if (createDb) {
//cfg.setProperty( "hibernate.hbm2ddl.auto", "create" );
}
try {
System.out.println( "Validating Schema" );
SchemaValidator sv = new SchemaValidator( cfg );
sv.validate();
System.out.println( "Validation complete" );
} catch (Exception e) {
System.out.println( "Validation exception: " + e );
}
sessionFactory = cfg.buildSessionFactory();
} catch (HibernateException ex) {
throw ex;
}
}
return sessionFactory;
}
The above code is not mine but I have to work with it.
Stacktrace:
INFO: Initializing c3p0-0.9.0 [built 11-July-2005 00:43:29 -0400; debug? true; trace: 10]
Jul 25, 2007 4:29:18 PM com.mchange.v2.c3p0.PoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@39f16f [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@6dd108 [ acquireIncrement -> 1, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 6dd108, idleConnectionTestPeriod -> 3000, initialPoolSize -> 5, maxIdleTime -> 300, maxPoolSize -> 20, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@9c1f2c [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 9c1f2c, jdbcUrl -> jdbc:mysql://localhost:3306/txs_bhs, properties -> {user=******, password=******} ], preferredTestQuery -> null, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ], factoryClassLocation -> null, identityToken -> 39f16f, numHelperThreads -> 3 ]
Jul 25, 2007 4:29:47 PM com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
WARNING: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@135572b -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30).
Jul 25, 2007 4:29:47 PM com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
WARNING: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@46a658 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30).
16:29:47,531 ERROR AWT-EventQueue-1 SchemaValidator - could not get database metadata
java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:104)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:236)
at com.mchange.v2.c3p0.PoolBackedDataSource.getConnection(PoolBackedDataSource.java:94)
at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:56)
at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionHelper.prepare(ManagedProviderConnectionHelper.java:28)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:107)
...
Please help me with this, cuz I'm about to throw my computer out the window:)
Thanks.
|