0

I've developed a Java application that use Hibernate to connect to a MySQL DB. Using Mac OS X the app works fine, but if I try to use it under Windows (XP) I get this error:

    Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1395)
    at persistence.Axdbv4HibernateManager.getNRecords(Axdbv4HibernateManager.java:51)
    at manager.Phase1.startPhase1(Phase1.java:28)
    at manager.Phase1.main(Phase1.java:89)
    at manager.Main.main(Main.java:60)
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:677)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
    at org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:79)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
    ... 8 more
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
    at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1406)
    at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:594)
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:514)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:743)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:670)
    ... 12 more

This is my hibernate configuration file

<?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>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost/axdbv4</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">mypassword</property> 

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

<property name="hibernate.c3p0.min_size">1</property> 

<property name="hibernate.c3p0.max_size">300</property>

<property name="hibernate.c3p0.timeout">30</property> 

<property name="hibernate.c3p0.max_statements">50</property> 

<property name="hibernate.c3p0.idle_test_period">100</property>

<property name="hibernate.c3p0.maxPoolSize">100</property>

<property name="hibernate.c3p0.minPoolSize">5</property>

<mapping resource="domain/DcmiRecord.hbm.xml" />

<mapping resource="domain/OccurrenceNameRecord.hbm.xml" />

<mapping resource="domain/OptionalFieldRecord.hbm.xml" />

<mapping resource="domain/RepresentationsNamesRecord.hbm.xml" />

<mapping resource="domain/NIDInfoRecord.hbm.xml" />

</session-factory>

</hibernate-configuration>

I've already tried to connect to the db using

mysql -u root -h localhost -p mypassword

and it works fine. Does anyone know how to handle it? Thanks!

displayName
  • 29
  • 1
  • 3
  • 7
  • The stack trace suggests that your problem is with C3P0. – duffymo May 09 '13 at 09:27
  • @duffymo Looks like the connection pool is exhausted (maximum connections reached) – Mark Rotteveel May 09 '13 at 09:30
  • yes it's true, but I cant' get what's wrong. As I said it, the same configuration works fine using OSX – displayName May 09 '13 at 09:30
  • View this: [com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source](http://stackoverflow.com/questions/3465872/com-mchange-v2-resourcepool-cannotacquireresourceexception-a-resourcepool-could) – davidpolo May 09 '13 at 09:31
  • Nope. I've also setup c3p0 maxPoolSize and minPoolSize property but it doesn't work.... – displayName May 09 '13 at 10:51

1 Answers1

0

Ok, I've finally solved! The problem was about the encoding used during the connection. My DB uses the UTF-8 but it seems that by default Hibernate uses latin-1. So it's necessary to add the following property to the hibernate.cfg.xml file:

<property key="hibernate.connection.characterEncoding">UTF-8</property>
<property key="hibernate.connection.useUnicode">true</property>
displayName
  • 29
  • 1
  • 3
  • 7