利用Thread-Specific Storage撰寫一個HibernateUtil
HibernateSessionUtil.java
import java.io.Serializable;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
public class HibernateSessionUtil implements Serializable
{
public static final ThreadLocal tLocalsess = new ThreadLocal();
public static final ThreadLocal tLocaltx = new ThreadLocal();
/*
* getting the thread-safe session for using
*/
public static Session currentSession(){
Session session = (Session) tLocalsess.get();
//open a new one, if none can be found.
try{
if (session == null){
session = openSession();
tLocalsess.set(session);
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
return session;
}
/*
* closing the thread-safe session
*/
public static void closeSession(){
Session session = (Session) tLocalsess.get();
tLocalsess.set(null);
try{
if (session != null && session.isOpen()){
session.close();
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
/*
* begin the transaction
*/
public static void beginTransaction(){
Transaction tx = (Transaction) tLocaltx.get();
try{
if (tx == null){
tx = currentSession().beginTransaction();
tLocaltx.set(tx);
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
/*
* close the transaction
*/
public static void commitTransaction(){
Transaction tx = (Transaction) tLocaltx.get();
try{
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
tLocaltx.set(null);
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
/*
* for rollbacking
*/
public static void rollbackTransaction(){
Transaction tx = (Transaction) tLocaltx.get();
try{
tLocaltx.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){
tx.rollback();
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
private static Session openSession() throws HibernateException{
return getSessionFactory().openSession();
}
private static SessionFactory getSessionFactory() throws HibernateException{
return SingletonSessionFactory.getInstance();
}
}
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
public class HibernateSessionUtil implements Serializable
{
public static final ThreadLocal tLocalsess = new ThreadLocal();
public static final ThreadLocal tLocaltx = new ThreadLocal();
/*
* getting the thread-safe session for using
*/
public static Session currentSession(){
Session session = (Session) tLocalsess.get();
//open a new one, if none can be found.
try{
if (session == null){
session = openSession();
tLocalsess.set(session);
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
return session;
}
/*
* closing the thread-safe session
*/
public static void closeSession(){
Session session = (Session) tLocalsess.get();
tLocalsess.set(null);
try{
if (session != null && session.isOpen()){
session.close();
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
/*
* begin the transaction
*/
public static void beginTransaction(){
Transaction tx = (Transaction) tLocaltx.get();
try{
if (tx == null){
tx = currentSession().beginTransaction();
tLocaltx.set(tx);
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
/*
* close the transaction
*/
public static void commitTransaction(){
Transaction tx = (Transaction) tLocaltx.get();
try{
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
tLocaltx.set(null);
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
/*
* for rollbacking
*/
public static void rollbackTransaction(){
Transaction tx = (Transaction) tLocaltx.get();
try{
tLocaltx.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){
tx.rollback();
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
private static Session openSession() throws HibernateException{
return getSessionFactory().openSession();
}
private static SessionFactory getSessionFactory() throws HibernateException{
return SingletonSessionFactory.getInstance();
}
}
filter中的程式碼如下
HibernateSessionCloser.java
public class HibernateSessionCloser implements Filter{
protected FilterConfig filterConfig = null;
public void init(FilterConfig filterConfig)throws ServletException{
this.filterConfig = filterConfig;
}
public void destroy(){
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
try{
chain.doFilter(request, response);
}
finally{
try{
HibernateSessionUtil.commitTransaction();
}catch (InfrastructureException e){
HibernateSessionUtil.rollbackTransaction();
}finally{
HibernateSessionUtil.closeSession();
}
}
}
}
protected FilterConfig filterConfig = null;
public void init(FilterConfig filterConfig)throws ServletException{
this.filterConfig = filterConfig;
}
public void destroy(){
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
try{
chain.doFilter(request, response);
}
finally{
try{
HibernateSessionUtil.commitTransaction();
}catch (InfrastructureException e){
HibernateSessionUtil.rollbackTransaction();
}finally{
HibernateSessionUtil.closeSession();
}
}
}
}
然後在操作資料庫之前加上
HibernateSessionUtil.beginTransaction();
HibernateSessionUtil.currentSession();//取得Session
HibernateSessionUtil.currentSession();//取得Session