2016-09-24 64 views
0

我是Java網站的新手。我正在使用Hibernate和MySQL開發一個使用Java web的應用程序。問題是,當我嘗試登錄時,如果嘗試連接遠程數據庫,它有時會引發通信鏈接失敗異常。什麼可能導致這個問題? (我也再次改變超時值會拋出異常。)Hibernate-MySQL連接

Hibernate的版本:4.3.3.Final

GlassFish服務器:4.1

相關錯誤SS:http://hackengine-er.com/exceptions/link-failure.png

項目源代碼: https://github.com/Muslum10cel/Vaccine

我的hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?> 
    <hibernate-configuration> 
     <session-factory> 
     <property name="hibernate.dialect">{MySQLDialect}</property> 
     <property name="hibernate.connection.driver_class">{jdbcdriver}</property> 
     <property name="hibernate.connection.url">{connectionstring}</property> 
     <property name="hibernate.connection.username">{username}</property> 
     <property name="hibernate.connection.password">{password}</property> 
     <property name="hibernate.show_sql">true</property> 
     <property name="hibernate.hbm2ddl.auto">update</property> 
     <property name="hibernate.current_session_context_class">thread</property> 
     <property name="hibernate.c3p0.timeout">0</property> 
    </session-factory> 

     <!-- Entity mappings --> 
    </hibernate-configuration> 

感謝您的幫助

回答

0

作爲第一步,我會建議更改一些配置:

  1. 使用org.hibernate.dialect.MySQL5Dialect,而不是org.hibernate.dialect.MySQLDialect
  2. 使用該驅動程序com.mysql.cj.jdbc.Driver代替com.mysql.jdbc.Driver
  3. 刪除?autoReconnect=true
  4. 您正在使用root用戶訪問數據庫,我建議創建一個ded使用者
  5. 您沒有提及任何密碼。即使你使用root用戶,它也有一個密碼。用它。

org.hibernate.dialect.MySQL5Dialect

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

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

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

<property name="hibernate.connection.password">root_password_missing</property> 
+0

感謝您的答覆。對於帖子,我已經刪除了密碼。我將用這些配置進行測試。非常感謝 –