我最近开始使用Hibernate
,但这个问题令我感到困惑。我已经看到,如果我们有多个数据源,可以多次创建Session Factory
。但同时我也读到sessionFactory
也实现了singleton
。这不矛盾??
答案 0 :(得分:-1)
//use singleton hibernateUtil.java class
package com.onlinetutorialspoint.config;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtility {
public static SessionFactory factory;
//to disallow creating objects by other classes.
private HibernateUtility() {
}
//maling the Hibernate SessionFactory object as singleton
public static synchronized SessionFactory getSessionFactory() {
if (factory == null) {
factory = new Configuration().configure("hibernate.cfg.xml").
buildSessionFactory();
}
return factory;
}
}