0

我开发了一个使用 Hibernate 连接到 MySQL 数据库的 Java 应用程序。使用 Mac OS X 该应用程序运行良好,但如果我尝试在 Windows (XP) 下使用它,我会收到此错误:

    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

这是我的休眠配置文件

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

我已经尝试使用连接到数据库

mysql -u root -h 本地主机 -p 我的密码

它工作正常。有谁知道如何处理它?谢谢!

4

1 回答 1

0

好的,我终于解决了!问题在于连接期间使用的编码。我的数据库使用 UTF-8,但似乎默认情况下 Hibernate 使用 latin-1。因此有必要在 hibernate.cfg.xml 文件中添加以下属性:

<property key="hibernate.connection.characterEncoding">UTF-8</property>
<property key="hibernate.connection.useUnicode">true</property>
于 2013-05-10T08:03:23.770 回答