孙卫琴的java书中写到hibernate中SessionFactory是单例类,并且网上也有很多文章说是SessionFactroy是单例的。我测试的时候
SessionFactory sf1 = config.buildSessionFactory();
SessionFactory sf2 = config.buildSessionFactory();
System.out.println(sf1==sf2);
为什么输出的是false呢
SessionFactory sf1 = config.buildSessionFactory();
SessionFactory sf2 = config.buildSessionFactory();
System.out.println(sf1==sf2);
为什么输出的是false呢
解决方案 »
- 警告: Failed to load the sqljdbc_auth.dll
- 大侠救命,关于jframe 的问题
- Swing 为何鼠标事件触发面板内组件更换失效?
- API文档相关
- thinking in java 写的好还是 java核心技术卷ii 写的好?我先看那本?
- 怎样快速生成一个指定大小的文件?
- 100---------JScrollPane中的滚动条为何不行啦?熟悉swing控件的高手请让我送点分
- 好像CSDN网站有些问题,搜索出来的都不能显示——图片显示问题(简单不能再简单)
- 关于小应用程序不能初始化的问题(在init()方法中创建了个对象,可能是该类的构造函数有问题,请大家帮帮我!急等!!!)
- 各位大哥,学java入门,要多长的时间呀,介绍一下好书吧
- 我想用Java做个类似苹果AppStore的东西
- 露大怯了。。一直傻傻的以为Java中字符是UTF-8形式的
log.debug( "Preparing to build session factory with filters : " + filterDefinitions );
secondPassCompile();
validate();
Environment.verifyProperties( properties );
Properties copy = new Properties();
copy.putAll( properties );
PropertiesHelper.resolvePlaceHolders( copy );
Settings settings = buildSettings( copy ); return new SessionFactoryImpl(
this,
mapping,
settings,
getInitializedEventListeners()
);
}
private static org.hibernate.SessionFactory sessionFactory;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
}
在使用时,我们一般会自己创建一个类,来获得SessionFactory对象,这个类使用单例模式,
使之创建的SessionFactory对象是单例的。或者用这个类保有单例的SessionFactory对象,
而分配HibernateSession对象。