2014-02-26 77 views
0

我有兩個不同的配置文件爲兩個不同的數據庫和他們每個人打開會話。以下是代碼。無法連接兩個不同的數據庫與休眠

僅供參考 - 出於安全原因,我打消了我所有的證件,並虛擬一個擺在我的配置文件

one.cfg.xml

<?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"> 
<hibernate-configuration> 
<session-factory> 
    <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 
    <property name="connection.url">URL</property> 
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
    <property name="connection.username">userName</property> 
    <property name="connection.password">Password</property> 
    <mapping class="com.ClassNameOfTheTable" /> 
     </session-factory> 
</hibernate-configuration> 

Second.cfg.xml

<?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"> 
<hibernate-configuration> 
<session-factory> 
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> 
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> 
<property name="hibernate.connection.url">url</property> 
<property name="hibernate.connection.username">userName</property> 
<property name="hibernate.connection.password">password</property> 
<mapping class="com.tableClassName" /> 
</session-factory> 
</hibernate-configuration> 

HibernateStratApp類(其中我設置配置文件)

package com.tn.gov; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 

    public class HibernateStartApp { 
private static final SessionFactory sessionFactory = buildSessionFactory1(); 
private static final SessionFactory sessionFactory1 = buildSessionFactory2(); 
Session session=null; 
Transaction transaction = null; 


private static SessionFactory buildSessionFactory1() { 
    try { 
     // Create the SessionFactory from hibernate.cfg.xml 
     return new Configuration().configure("one.cfg.xml").buildSessionFactory(); 
    } catch (Throwable ex) { 
     // Make sure you log the exception, as it might be swallowed 
     System.err.println("Initial SessionFactory creation failed." + ex); 
     throw new ExceptionInInitializerError(ex); 
    } 
} 
public static SessionFactory getSessionFactoryOne() { 
    return sessionFactory; 
} 
public static SessionFactory getSessionFactoryTwo(){ 
    return sessionFactory1; 
} 
    private static SessionFactory buildSessionFactory2() { 
     try { 
      // Create the SessionFactory from hibernate.cfg.xml 
      return new Configuration().configure("two.cfg.xml").buildSessionFactory(); 
     } catch (Throwable ex) { 
      // Make sure you log the exception, as it might be swallowed 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

}

而通過打開和關閉會話,我正在檢索值。

但我geting failed.org.hibernate.exception.JDBCConnectionException:錯誤調用驅動程序#連接時,當我嘗試連接。

+0

一個配置在屬性名稱中包含「hibernate」前綴是否正確?在我看來,你的代碼包含失誤。 – svaor

回答

0

我可以弄明白,這是我指向的URL的問題,這很奇怪,錯誤信息的力量打印出來是正確的。