由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 自己管理session要每次手动释放么
相关主题
请教:怎么把synchronize的method改成用thread 但thread safe呢?折腾了一天,实在是绝望了,请教请教
Suggestion Re: 发现 synchronized 的一个问题NullPointerException 问题
How do I declare a transaction among 2 spring service callsWeblogic 8.1又一个问题---应该是有关JDBC的
anyone saw this on code?jdbc + oracle connection pooling问题
Where does Java Store Static Variable?Singleton
求教:Facade Pattern vs Static Variable大家熟读了Java source code几遍?
JAVA 求解size() method 为什么需要多线程保护?
JAVA 考试题请教ThreadLocal 的一个 use case
相关话题的讨论汇总
话题: session话题: null话题: static
进入Java版参与讨论
1 (共1页)
y***m
发帖数: 7027
1
hiberate的管理在lazy模式等下弄起来挺麻烦,自己管理倒是很方便,但可能存在连接
没有释放的隐患。。。。
import java.io.Serializable;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;
//@Component("hibernateSessionUtil")
public class HibernateSessionUtil implements Serializable {
//Create a thread local variable tLocalsess, for operating session
public static final ThreadLocal tLocalsess = new ThreadLocal();
//Create a thread local variable tLocaltx, used to operate services
public static final ThreadLocal tLocaltx = new ThreadLocal();
//get session
public static Session currentSession(){
//TLocalsess variables from the thread in the current session to obtain
Session session = (Session) tLocalsess.get();
//Determine whether the session is empty, if empty, will create a session
, and pay variable tLocalsess thread
try{
if (session == null){
session = openSession();
tLocalsess.set(session);
}
}catch (HibernateException e){
e.printStackTrace();
}
return session;
}
//close current session
public static void closeSession(){
//TLocalsess variables from the thread in the current session to obtain
Session session = (Session) tLocalsess.get();
//set current tLocalsess as null
tLocalsess.set(null);
try{
//close session
if (session != null && session.isOpen()){
session.close();
}
}catch (HibernateException e){
e.printStackTrace();
}
}
//start transaction
public static void beginTransaction(){
//Variables obtained from the thread of things tLocaltx Transaction
Management Object
Transaction tx = (Transaction) tLocaltx.get();
try{
//If it is empty from the session to create a tx
if (tx == null){
tx = currentSession().beginTransaction();
tLocaltx.set(tx);
}
}catch (HibernateException e){
e.printStackTrace();
}
}
//commit transaction
public static void commitTransaction(){
//get the transaction
Transaction tx = (Transaction) tLocaltx.get();
try{
//if not null, then commit
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
//close current session
closeSession();
tLocaltx.set(null);
}catch (HibernateException e){
e.printStackTrace();
}
}
//rollback the transaction
public static void rollbackTransaction(){
//get tx transaction
Transaction tx = (Transaction) tLocaltx.get();
try{
//Clear the variable
tLocaltx.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){
//rollback the transaction
tx.rollback();
}
}catch (HibernateException e){
e.printStackTrace();
}
}
//private
//get session
private static Session openSession() throws HibernateException{
return getSessionFactory().openSession();
}
//get sessionFactory
private static SessionFactory getSessionFactory() throws
HibernateException{
return HibernateSessionFactory.getSessionFactory();
}
}
t*******e
发帖数: 684
2
google "open session in view"
1 (共1页)
进入Java版参与讨论
相关主题
ThreadLocal 的一个 use caseWhere does Java Store Static Variable?
How to make code thread-safe?应该怎么答?求教:Facade Pattern vs Static Variable
JavaBean variable name standardJAVA 求解
how to solve the problem: the members change by each other .JAVA 考试题请教
请教:怎么把synchronize的method改成用thread 但thread safe呢?折腾了一天,实在是绝望了,请教请教
Suggestion Re: 发现 synchronized 的一个问题NullPointerException 问题
How do I declare a transaction among 2 spring service callsWeblogic 8.1又一个问题---应该是有关JDBC的
anyone saw this on code?jdbc + oracle connection pooling问题
相关话题的讨论汇总
话题: session话题: null话题: static