无法将两个不同的数据库与休眠连接



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

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

一.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>

第二.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);
        }
    }

}

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

但是我得到了失败.org.hibernate.exception.JDBCConnectionException:当我尝试连接时调用驱动程序#connect时出错。

我能够弄清楚这一点,这是我指向的URL的问题,这很奇怪,错误消息错误地打印正确。

最新更新