threadlocal报错
package com.yinbo.tbms.jljiao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION="/hibernate.cfg.xml";
private static final ThreadLocal<Session>thredLocal=new ThreadLocal<Session>();
private static Configuration configuration=new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile=CONFIG_FILE_LOCATION;
static{
try{
configuration.configure(configFile);
sessionFactory=configuration.buildSessionFactory();
}catch(Exception e){
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory()
{
}
public static Session getSession() throws HibernateException
{
Session session=(Session)threadLocal.get();
if(session==null||!session.isOpen())
{
rebuildSessionFactory();
}
session=(sessionFactory!=null)?sessionFactory.openSession():null;
threadLocal.set(session);
return session;
}
public static void rebuildSessionFactory(){
try{
configuration.configure(configFile);
sessionFactory=configuration.buildSessionFactory();
}catch(Exception e){
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException{
Session session=(Session)threadLocal.get();
threadLocal.set(null);
if(session!=null)
{
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory(){
return sessionFactory;
}
public static void setConfigFile(String conFile){
HibernateSessionFactory.configFile=configFile;
sessionFactory=null;
}
public static Configuration getConfiguration(){
return configuration;
}
}
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION="/hibernate.cfg.xml";
private static final ThreadLocal<Session>thredLocal=new ThreadLocal<Session>();
private static Configuration configuration=new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile=CONFIG_FILE_LOCATION;
static{
try{
configuration.configure(configFile);
sessionFactory=configuration.buildSessionFactory();
}catch(Exception e){
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory()
{
}
public static Session getSession() throws HibernateException
{
Session session=(Session)threadLocal.get();
if(session==null||!session.isOpen())
{
rebuildSessionFactory();
}
session=(sessionFactory!=null)?sessionFactory.openSession():null;
threadLocal.set(session);
return session;
}
public static void rebuildSessionFactory(){
try{
configuration.configure(configFile);
sessionFactory=configuration.buildSessionFactory();
}catch(Exception e){
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException{
Session session=(Session)threadLocal.get();
threadLocal.set(null);
if(session!=null)
{
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory(){
return sessionFactory;
}
public static void setConfigFile(String conFile){
HibernateSessionFactory.configFile=configFile;
sessionFactory=null;
}
public static Configuration getConfiguration(){
return configuration;
}
}
...全文
相关推荐
深入理解Java:SimpleDateFormat安全的时间格式化
SimpleDateFormat的非线程安全性SimpleDateFormat类不是线程安全的,这意味着在多线程环境下,如果多个线程同时访问同一个SimpleDateFormat实例,可能会导致各种问题,例如转化的时间不正确、报错
ThreadLocal应用示例及理解
ThreadLocal变量值,`get()`方法用于获取当前线程的ThreadLocal变量值。
ThreadLocal
- 不适用于跨线程通信:ThreadLocal只保证同一线程内的数据隔离,不同线程间无法共享ThreadLocal变量。
ThreadLocal跨线程问题
"ThreadLocal在多线程环境中的使用与局限性"ThreadLocal是Java中一个非常有用的工具类,它提供了线程局部变量,每个线程都有独立的副本,不会互相干扰。然而,在复杂的处理流程中
java 中ThreadLocal 的正确用法
* 等等ThreadLocal 的注意事项在使用 ThreadLocal 时,我们需要注意以下几点:* ThreadLocal 变量应该是 private static 的,以避免外部访问和修改。


Web 开发
