2016-04-24 14 views
0

Ich versuche, ein Projekt mit Hibernate zu bauen 5.Java Hibernate kann MySQL-Datenbank nicht verbinden

Wenn ich die Verbindung bin Tests es nicht nur ... kann mir jemand helfen?

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 Methode:

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; 
} 

Der Fehler ist:

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 

Warum meine Verbindung ausfällt und wie tun ich behebe es?

+0

Können Sie Ihre Verbindungsparameter mit dem MySQL-Kommandozeilen-Tool testen: 'mysql -u youruser -p -h yourdbhost databasename' –

+0

ERROR 1045 (28000): Zugriff verweigert für User 'root' @ 'IP' (mit Passwort: JA) Wahrscheinlich ist wegen dieser –

Antwort

0
public class HibernateUtil 
{ 

    private static final SessionFactory sessionFactory = buildSessionFactory(); 

    private static SessionFactory buildSessionFactory(){ 
     try { 
      // Create the SessionFactory from standard (hibernate.cfg.xml) 
      // config file. 
      //Annotation 
      Configuration cfg = new Configuration(); 
      SessionFactory sf = (SessionFactory) cfg.configure().buildSessionFactory(); 
      return sf; 

     } catch (Throwable ex) { 
      // Log the exception. 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() 
    { 
     return sessionFactory; 
    } 
} 
+0

Initial Session Schaffung failed.org.hibernate.tool.schema.spi.SchemaManagementException: Kann nicht JDBC-Verbindung für Schema-Management Ziel öffnen –

+0

zunächst sieht es aus wie Ihre Verbindung uRL falsch ist in die XML-Datei. Bist du zufällig mit localhost verbunden? – luffy

+0

Nein, leider habe ich nur die mySql-Verbindung in Google Cloud:/ Es ist das erste Mal, dass ich es so, ich weiß nicht, was ich tun muss –

0

Ich wollte kommentieren, konnte aber nicht wegen der rep Punkte. Entschuldigung für die Unannehmlichkeit

Dieser Fehler kann aufgrund dieser Zeile auftreten.

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

Haben Sie den Schemanamen nach IP_ADDRESS eingegeben? Etwas wie das;

jdbc:mysql://localhost:3306/myDb 
+0

Ja, das war das Problem hier: jdbc: mysql: // localhost: 3306/MYDB Es löste jetzt :) Dank –

+0

Sie – erolkaya84

+0

@ F.Coelho willkommen :) r könnten Sie bitte diese Antwort als die richtige Antwort markieren? Vielen Dank. – erolkaya84

Verwandte Themen