哪为朋友知道SessionFactory.java中session = sessionFactory.openSession()是干什么用的?老是这句出错!在线候着
public class SessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static final Configuration cfg = new Configuration();
private static net.sf.hibernate.SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession(); //就是这句!
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
private SessionFactory() {
}
}
2个例子都出现这个错,我用的MYECLIPSE 4.2,Hibernate 2。session = sessionFactory.openSession();这句有什么玄机吗?
java.lang.NullPointerException
com.nscorp.hibernate.SessionFactory.currentSession(SessionFactory.java:31)
com.nscorp.hibernate.VipService.getVipdataList(VipService.java:200)
org.apache.jsp.AddVipData_jsp._jspService(AddVipData_jsp.java:120)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static final Configuration cfg = new Configuration();
private static net.sf.hibernate.SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession(); //就是这句!
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
private SessionFactory() {
}
}
2个例子都出现这个错,我用的MYECLIPSE 4.2,Hibernate 2。session = sessionFactory.openSession();这句有什么玄机吗?
java.lang.NullPointerException
com.nscorp.hibernate.SessionFactory.currentSession(SessionFactory.java:31)
com.nscorp.hibernate.VipService.getVipdataList(VipService.java:200)
org.apache.jsp.AddVipData_jsp._jspService(AddVipData_jsp.java:120)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
...全文
相关推荐
新Hibernate SessionFactory().getCurrentSession()猫腻
NULL 博文链接:https://zgdkik.iteye.com/blog/1835667
Java中Hibernate手动获取Session的完整方法与实战详解
在没有Spring等框架支持的纯Hibernate环境中,我们可以借助机制,自己动手封装一个线程安全的Session管理工具。if (tx!// 防止内存泄漏!这个工具类有几个精妙之处:🔸保证线程隔离🔸 提供事务快捷方法,减少样板代码🔸 在中调用remove(),防止内存泄漏(特别是在线程池环境下)然后在Filter中统一拦截:try {throw e;
SessionFactory.getCurrentSession与openSession的区别
博文链接:https://shaqiang32.iteye.com/blog/201918
getCurrentSession 与 openSession() 的区别
NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085
SSH整合中 hibernate托管给Spring得到SessionFactory
Spring文件中的 SessionFactory中 加入为了能得到同一个Session


