无法创建sessionFactory object.org.hibernate.exception.JDBCConnectionException:调用Driver#connect时出错

时间:2014-06-14 17:59:16

标签: java hibernate

我尝试连接到远程服务器上的数据库。我使用jdk 1.7.0_60Hibernate-release-4.3.5 final。 项目包括在罐子之后:

antlr-2.7.7.jar;
dom4j-1.6.1.jar,

hibernate-commons-annotations-4.04.Final.jar,
hibernate-core-4.3.5.Final.jar, hibernate-entitymanager-4.3.5.Final.jar, javassist-3.18.1-GA.jar, jboss-logging-3.1.3.GA.jar, jboss-logging-annotations-1.2.0.Beta1.jar,jboss-transaction-api_1.2_spec-1.0.0.Final.jar, mysql-connector-java-5.1.30-bin.jar.

例外如下:

Jun 14, 2014 7:31:56 PM  org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Jun 14, 2014 7:31:56 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
Jun 14, 2014 7:31:56 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 14, 2014 7:31:56 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 14, 2014 7:31:56 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: swmhibernate.cfg.xml
Jun 14, 2014 7:31:56 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: swmhibernate.cfg.xml
Jun 14, 2014 7:31:56 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jun 14, 2014 7:31:57 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jun 14, 2014 7:31:57 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL   [jdbc:mysql://sopro.informatik.uni-augsburg.de:3306/team02]
Jun 14, 2014 7:31:57 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=team02, password=****, autocommit=false}
Jun 14, 2014 7:31:57 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Jun 14, 2014 7:31:57 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Failed to create sessionFactory object.org.hibernate.exception.JDBCConnectionException:   Error calling Driver#connect
Exception in thread "main" java.lang.ExceptionInInitializerError
at de.swm.hibernate.Funktionen.main(Funktionen.java:25)
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:132)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:118)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at de.swm.hibernate.Funktionen.main(Funktionen.java:22)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1127)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:356)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2502)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
... 14 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:258)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:306)
... 27 more

配置文件是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-  configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- connecting to the database --> 
<property 

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

name="hibernate.connection.url">jdbc:mysql://host:3306/team02</property> 
<property name="connection.username">name</property>
<property name="connection.password">****** </property>  


<property name="connection.pool_size">1</property>

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

 <property name="hibernate.connection.autocommit">false</property>
 <property name="show_sql">true</property>
 <property name="format_sql">true</property>
 <property name="use_sql_comments">true</property>
 <property name="hbm2ddl.auto">update</property>
 <!-- for jdbc transaction -->
 <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

 <!-- mapping file -->
 <mapping class="de.swm.hibernate.Address"/>
 <mapping class="de.swm.hibernate.Plant"/>
 <mapping class="de.swm.hibernate.Plantprocessinfo"/>
 <mapping class="de.swm.hibernate.User"/>
 <mapping class="de.swm.hibernate.Directmarketing"/>
 <mapping class="de.swm.hibernate.Account"/>
 <mapping class="de.swm.hibernate.Customer"/>
 <mapping class="de.swm.hibernate.Report"/>
 <mapping class="de.swm.hibernate.Mrl"/>
 <mapping class="de.swm.hibernate.Srl"/>
 <mapping class="de.swm.hibernate.MrlmarketingInfo"/>
 <mapping class="de.swm.hibernate.Srl_outputpreis"/>
  <mapping class="de.swm.hibernate.Srl_workprice"/>
 <mapping class="de.swm.hibernate.InvoiceMailingaddress"/>

</session-factory>

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:8)

问题在于mysql-connector-java-5.1.30-bin.jar。将其替换为mysql-connector-java-5.0.8-bin.jar。它将解决该异常。

答案 1 :(得分:6)

请确保数据库服务器已启动并正在运行。看起来服务器没有响应,因为没有收到数据包并且发生了超时。

答案 2 :(得分:0)

Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect

<!DOCTYPE hibernate-configuration PUBLIC
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</propert``y> 
    <property name="connection.url">jdbc:mysql:/@localhost/3306/test</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <mapping resource="com/nt/cfgs/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

检查数据库用户名和密码是否有错误。

答案 3 :(得分:0)

我遇到了这个错误,问题是SQL服务器没有响应。所以,我尝试了几种解决方案,但没有达到预期的效果。

对于具有相同错误的用户,请尝试在SQL命令行提示符中设置最大连接数(增加)。

答案 4 :(得分:0)

您也可以尝试更改休眠版本。使用 hibernate 5,它帮助我解决了这个错误。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.10.Final</version>
</dependency>