2011-12-23 61 views
1

您好我正在開發一個struts + hibernate網站。我遇到了hibernate連接問題。當我在我的本地主機中使用它時,它工作正常。問題是我使用與服務器數據庫連接用戶名和密碼相同的配置文件連接未打開。我已將錯誤消息寫入服務器中的文件,並將錯誤消息顯示爲「無法打開連接」。休眠無法打開服務器連接

這裏是我的Hibernate配置文件

<?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"> 
<?xmsl version="1.0" encoding="UTF-8"?> 
<hibernate-configuration> 
    <session-factory name="session1"> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost/dev_mydatabase?autoReconnect=false</property> 
    <property name="hibernate.connection.username">myusername(not original)</property> 
    <property name="hibernate.connection.password">mypassword(no original)</property> 
    <property name="show_sql">true</property> 
    <property name="hibernate.cglib.use_reflection_optimizer">false</property> 
    <property name="current_session_context_class">managed</property> 
    <property name="hibernate.c3p0.max_statements">50</property> 

    <!-- For seeing hibernate log on the console --> 
    <property name="show_sql">true</property> 
    <!-- End--> 

    <property name="hibernate.hbm2ddl.auto">update</property> 
    <property name="log4j.logger.org.hibernate.SQL">trace</property> 
    <!-- <property name="hbm2ddl.auto">update</property>--> 

<!-- All my mapping files are here.--> 
    </session-factory> 
</hibernate-configuration> 

和吾道文件我寫查詢作爲

SessionFactory sessionFactory = 
        (SessionFactory) ServletActionContext.getServletContext().getAttribute(HibernateListener.KEY_NAME); 
      Session Hibernatesession = sessionFactory.openSession(); 
      Hibernatesession.beginTransaction(); 
      doctorList = Hibernatesession.createCriteria("Hibernate query will be here"). 

我不明白什麼是這裏的問題發生。任何人都請幫助我。

謝謝。

+1

- 顯示stactrace – Bozho 2011-12-23 13:22:03

回答

1

問題是您的hibernate配置中的localhost如下所示。將其更改爲指向實際的數據庫地址和端口。

<property name="hibernate.connection.url">jdbc:mysql://localhost/dev_mydatabase?autoReconnect=false</property> 
+0

無連接的詳細信息我在這裏提供的虛擬數據。我的hibernate配置文件中的實際數據是不同的。我用一個簡單的jsp頁面測試了連接。它工作完美。 – anoop 2011-12-23 17:25:52

+0

@anoopkattodi發佈堆棧跟蹤然後 – 2011-12-23 17:46:41

+0

其實我不知道如何從服務器採取stracktrace。我已經使用log4j從服務器獲取日誌。它只說**不能打開連接** .. – anoop 2011-12-23 19:07:47