Java Hibernate 无法连接 MySQL 数据库

标签 java mysql hibernate

我正在尝试使用 Hibernate 5 构建一个项目。

当我测试连接时,它失败了...有人可以帮助我吗?

hibernate.cfg.xml:

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://IP_ADDRESS</property>
    <property name="hibernate.connection.username">USER</property>
    <property name="hibernate.connection.password">PASSWORD</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.connection.pool_size">30</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.hbm2ddl.auto">update</property>

    <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <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>

    <mapping class="classPath"></mapping>

</session-factory>

getSessionFactory方法:

public static SessionFactory getSessionFactory() throws DataException {
    if (sessionFactory == null) {
        synchronized (SessionFactory.class) {
               if (sessionFactory == null) {
                try {
                      Configuration cConfig = new Configuration();

                    if (hibernateConfig == null)
                        cConfig.configure();
                    else
                        cConfig.configure(hibernateConfig);
                    sessionFactory = cConfig.buildSessionFactory();
                } catch (Throwable ex) {
                    ex.printStackTrace();
                    throw new DataException(
                            DataException.GENERAL_ERROR_CODE,
                            "Failed to create sessionFactory object", ex);
                }
            } else {
                return sessionFactory;
            }
        }
    }
    return sessionFactory;
}

错误是:

org.hibernate.tool.schema.spi.SchemaManagementException: Unable to open JDBC connection for schema management target
    at org.hibernate.tool.schema.internal.TargetDatabaseImpl.prepare(TargetDatabaseImpl.java:42)
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:57)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:129)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:97)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:481)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at com.rif.datamodel.AbstractDaoV2.getSessionFactory(AbstractDaoV2.java:37)
    at com.rif.datamodel.AbstractDaoV2.hasSession(AbstractDaoV2.java:59)
    at com.rif.datamodel.AbstractDaoV2.executeQuery(AbstractDaoV2.java:419)
    at com.rif.hit.core.domain.property.test.Property_Crud.test(Property_Crud.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:689)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
    at org.hibernate.c3p0.internal.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:73)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180)
    at org.hibernate.tool.schema.internal.TargetDatabaseImpl.prepare(TargetDatabaseImpl.java:38)
    ... 34 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:1418)
    at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:606)
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:526)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:755)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:682)
    ... 38 more

为什么我的连接失败以及如何修复它?

最佳答案

我本来想发表评论,但由于代表点而无法发表评论。给您带来的不便敬请谅解

此错误可能是由于此行而发生的。

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

您是否将架构名称放在 IP_ADDRESS 之后?像这样的东西;

jdbc:mysql://localhost:3306/myDb

关于Java Hibernate 无法连接 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36826728/

相关文章:

php - 不使用 sql 外键的缺点

java - 带有 withClause 参数的 Hibernate createAlias() 会导致惰性收集

java - 如何使用 like 和 % 进行 Hibernate 查询?

java - 最小化/最大化后 JTable 重置为原始状态

php - 在没有用户交互的情况下在特定时间后运行 sql 查询?

java - for 循环中嵌套 if 语句 [JAVA/LibGDX]

php - PDO 登录验证

mysql - 如何使用 Java 和 spring/hibernate 在我的数据库中正确插入日期(在我的数据库中得到错误的时间)?

java - 为什么 ManagedChannelBuilder 没有用于与服务器建立 TLS 连接的 TLS 参数?

java - 如何找到每个单词的第一个字母?