在应用程序中使用两个hibernate.cfg.xml文件

时间:2014-08-25 12:18:35

标签: java hibernate

如何为一个应用程序指定两个hibernate配置, 我创建了两个hibernate文件,并在SessionFactory.util中提及它们

hibernateMaster.cfg.xml文件工作正常.. 但是当关闭Master数据库服务器并尝试使用hibernateMaster.cfg.xml的应用程序并检索数据时,它会给我" null"

但如果我重新启动应用程序,它可以正常使用hibernateMaster.cfg.xml文件

这是我的hibernate文件

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

        <!-- Database Connection Settings -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://192.168.232.143:3306/cps</property>
        <property name="hibernate.connection.username">gaiz</property>
        <property name="hibernate.connection.password">mysql</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>


        <mapping resource="lib/driver/mappings/Patient.hbm.xml"/>
        <mapping resource="lib/driver/mappings/Allergy.hbm.xml"/>
        <mapping resource="lib/driver/mappings/Hospital.hbm.xml"/>
        <mapping resource="lib/driver/mappings/StatStaff.hbm.xml"/>
        <mapping resource="lib/driver/mappings/StatWard.hbm.xml"/>
        <mapping resource="lib/driver/mappings/User.hbm.xml"/>
        <mapping resource="lib/driver/mappings/UserRole.hbm.xml"/>
        <mapping resource="lib/driver/mappings/UserLog.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

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

        <!-- Database Connection Settings -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://192.168.232.144:3306/cps</property>
        <property name="hibernate.connection.username">gaiz2</property>
        <property name="hibernate.connection.password">mysql</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>


        <mapping resource="lib/driver/mappings/Patient.hbm.xml"/>
        <mapping resource="lib/driver/mappings/Allergy.hbm.xml"/>
        <mapping resource="lib/driver/mappings/Hospital.hbm.xml"/>
        <mapping resource="lib/driver/mappings/StatStaff.hbm.xml"/>
        <mapping resource="lib/driver/mappings/StatWard.hbm.xml"/>
        <mapping resource="lib/driver/mappings/User.hbm.xml"/>
        <mapping resource="lib/driver/mappings/UserRole.hbm.xml"/>
        <mapping resource="lib/driver/mappings/UserLog.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

SessionFactoryUtil.java

package lib;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import core.resources.statisticalResource;

public class SessionFactoryUtil {
    //This class creates a session factory object by looking at the hibernate configuration (hibernate.cfg.xml)

    private static SessionFactory sesFactory;
    private static ServiceRegistry sesRegistry;
    static Configuration cfg;
    static{
        try{
             cfg= new Configuration().configure("lib/hibernateMaster.cfg.xml");
            sesRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
            sesFactory=cfg.buildSessionFactory(sesRegistry);
            try{

                Session session = SessionFactoryUtil.getSessionFactory().openSession();
                Transaction tx = session.beginTransaction();
                System.out.println("Connected to Master Database Server");
            }                   
            catch(Throwable ex){
                cfg= new Configuration().configure("lib/hibernateSlave.cfg.xml"); 
                sesRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
                sesFactory=cfg.buildSessionFactory(sesRegistry);
                System.out.println("Connected to Slave Database Server");
            }
        }
        catch(Throwable ex){
            System.out.println("Master & Slave Database Error.");
            System.err.println("Initial SessionFactory Creation Failed"+ex);
            throw new ExceptionInInitializerError(ex);
        }
    }   
    public static SessionFactory getSessionFactory() {
            return sesFactory;
    }
}

我知道在SessionFactoryUtil.java中做错了什么我做错了什么。

1 个答案:

答案 0 :(得分:0)

您的问题是您以静态方式初始化工厂。

static{
  ...
}

当您启动应用程序时,该代码仅执行一次。这样当你关闭你的主服务器时,hibernate仍会要求它。

您应该初始化两个配置并在其他情况下使用main(如果存在)或secondary。您可以在getSessionFactory()方法中执行此操作,该方法在每次调用方法时执行,但不在静态初始化程序中执行。