public class SessionFactory {
private SessionFactory() {
} 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;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Build hibernate session factory
*
*/
public static void buildSessionFactory() {
try {
if(sessionFactory==null){
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
}
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close hibernate session factory
*
*/
public static void closeSessionFactory() {
try {
if(sessionFactory!=null){
configuration=null;
if(!sessionFactory.isClosed())
sessionFactory.close();
sessionFactory=null;
threadLocal.set(null);
}
} catch (Exception e) {
System.err
.println("%%%% Error Close SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
SessionFactory.configFile = configFile;
sessionFactory = null;
} /**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}} 一个静态的SessionFactory被初始化以后,何时内存会被回收?
我的项目跑在websphere6.0上.重启websphere会释放静态的SessionFactory成员变量吗?
private SessionFactory() {
} 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;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Build hibernate session factory
*
*/
public static void buildSessionFactory() {
try {
if(sessionFactory==null){
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
}
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close hibernate session factory
*
*/
public static void closeSessionFactory() {
try {
if(sessionFactory!=null){
configuration=null;
if(!sessionFactory.isClosed())
sessionFactory.close();
sessionFactory=null;
threadLocal.set(null);
}
} catch (Exception e) {
System.err
.println("%%%% Error Close SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
SessionFactory.configFile = configFile;
sessionFactory = null;
} /**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}} 一个静态的SessionFactory被初始化以后,何时内存会被回收?
我的项目跑在websphere6.0上.重启websphere会释放静态的SessionFactory成员变量吗?
一般我们在方法调用结束会去关闭SessionFactory的.
SessionFactory是单利模式,如果没有去显示的关闭,他的周期会一直到浏览器的关闭.
private static org.hibernate.SessionFactory sessionFactory;
此变量何时内存会被回收?是不我的项目一直开着.SessionFactory 就一直占用着内存.重启websphere后会被释放吗?谢谢
当static被置为null真的可以被gc收回所占用的内存空间??
jdk的回收,程序不能控制吧
我重启was6.0后 类的static成员变量内存会被回收吗?给个确定的答案!这对我来说很重要