?? hibernateutil.java
字號(hào):
package com.kelefa.sms.util;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
public class HibernateUtil
{
public HibernateUtil()
{
}
private static final SessionFactory sessionFactory;
static
{
try {
// Create the SessionFactory
Configuration cfg = new Configuration()
.configure( "/hibernate.cfg.xml" );
sessionFactory = cfg.buildSessionFactory();
}
catch ( Throwable ex ) {
ex.printStackTrace();
throw new ExceptionInInitializerError( ex );
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession()
throws HibernateException
{
Session s = ( Session ) session.get();
// Open a new Session, if this Thread has none yet
if ( s == null ) {
s = sessionFactory.openSession();
session.set( s );
}
return s;
}
/**
* 先不close session,等request結(jié)束前或線程結(jié)束才close,即調(diào)用closeSession2()。
* 修改:
* 1. 添加過(guò)濾器:com.skylink.greenChannel.filter.HibernateSessionFilter,
* 在request結(jié)束前調(diào)用closeSession2()
* 2. 修改com.skylink.greenChannel.job.SmsHandler.execute(),在函數(shù)退出前,
* 即結(jié)束這個(gè)線程前調(diào)用closeSession2()
* @throws HibernateException
*/
public static void closeSession()
throws HibernateException
{
// Session s = ( Session ) session.get();
// session.set( null );
// if ( s != null )
// s.close();
}
public static void closeSession2()
throws HibernateException
{
Session s = ( Session ) session.get();
session.set( null );
if ( s != null ) {
s.close();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -