无法使用Hibernate连接两个不同的数据库

时间:2014-02-26 17:09:52

标签: java database oracle hibernate

我有两个不同的配置文件用于两个不同的数据库,每个数据库都有会话打开。以下是代码。

仅供参考 - 出于安全原因,我删除了所有凭证,并在我的配置文件中放置了虚拟凭证

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

}

通过打开和关闭会话,我正在检索值。

但我正在测试failed.org.hibernate.exception.JDBCConnectionException:当我尝试连接时调用Driver#connect时出错。

1 个答案:

答案 0 :(得分:0)

我能够解决这个问题,这是我指向的URL的问题,奇怪的是,错误消息dint将其打印出来是正确的。