無論是立即加載還是延遲加載必須要連接數據庫的
好
第一個問題很容易想到
view plaincopy to clipboardprint?
public void doFilter(ServletRequest request
FilterChain filterChain) {
try {
filterChain
} catch (IOException e) {
e
} catch (ServletException e) {
e
} finally {
try {
mitTransaction();
} catch (Exception e) {
HibernateUtil
} finally {
HibernateUtil
}
}
}
public void doFilter(ServletRequest request
FilterChain filterChain) {
try {
filterChain
} catch (IOException e) {
e
} catch (ServletException e) {
e
} finally {
try {
mitTransaction();
} catch (Exception e) {
HibernateUtil
} finally {
HibernateUtil
}
}
}要解決第二個問題我們必須先搞清楚
有關ThreadLocal的更多資料
最後我把相關的代碼發出來
view plaincopy to clipboardprint?
import java
import org
import org
import org
import org
import org
import java
import org
import java
/**
*
* <p>
* Title:Hibernate工具類
* </p>
*
* <p>
* 利用ThreadLocal 綁定 Hibernate 的session
* </p>
*
* @author 孫钰佳
* @version
*/
public class HibernateUtil {
/**
* Loger
*/
private static final Logger logger = Logger
/**
* 存儲hibernate session的ThreadLocal
*/
private static final ThreadLocal sessionThread = new ThreadLocal();
/**
* 存儲事務的ThreadLocal
*/
private static final ThreadLocal transactionThread = new ThreadLocal();
/**
* Hibernate 的 Session工廠
*/
private static SessionFactory sessionFactory = null;
/**
* 初始化SessionFactory
*
* @param file
* Hibernate配置文件
*/
public static void initSessionFactory(File file) {
Configuration config = new Configuration();
nfigure(file);
sessionFactory = config
}
/**
* 獲取當前線程綁定的session
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() {
Session s = (Session) sessionThread
if (s == null) {
s = sessionFactory
sessionThread
} else {
Connection conn = nnection();
try {
if (conn == null || conn
try {
s
} catch (HibernateException e) {
logger
}
s = sessionFactory
sessionThread
}
} catch (SQLException e) {
throw new HibernateException(e);
}
}
return s;
}
/**
* 取得當前session的事務
*
* @return Transaction
*/
public static Transaction transaction() {
Transaction transaction = (Transaction) transactionThread
if (transaction == null) {
transaction = getSession()
transactionThread
}
return transaction;
}
/**
* 提交當前session的事務
*/
public static void commitTransaction() {
Transaction transaction = (Transaction) transactionThread
transactionThread
if (transaction != null)
mit();
}
/**
* 回滾當前session的事務
*/
public static void rollbackTransaction() {
Transaction tx = (Transaction) transactionThread
transactionThread
if (tx != null)
tx
}
/**
* 關閉當前線程使用的session
*/
public static void closeSession() {
Session session = (Session) sessionThread
if (session != null) {
session
session
sessionThread
}
}
}
import java
import org
import org
import org
import org
import org
import java
import org
import java
/**
*
* <p>
* Title:Hibernate工具類
* </p>
*
* <p>
* 利用ThreadLocal 綁定 Hibernate 的session
* </p>
*
* @author 孫钰佳
* @version
*/
public class HibernateUtil {
/**
* Loger
*/
private static final Logger logger = Logger
/**
* 存儲hibernate session的ThreadLocal
*/
private static final ThreadLocal sessionThread = new ThreadLocal();
/**
* 存儲事務的ThreadLocal
*/
private static final ThreadLocal transactionThread = new ThreadLocal();
/**
* Hibernate 的 Session工廠
*/
private static SessionFactory sessionFactory = null;
/**
* 初始化SessionFactory
*
* @param file
* Hibernate配置文件
*/
public static void initSessionFactory(File file) {
Configuration config = new Configuration();
nfigure(file);
sessionFactory = config
}
/**
* 獲取當前線程綁定的session
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() {
Session s = (Session) sessionThread
if (s == null) {
s = sessionFactory
sessionThread
} else {
Connection conn = nnection();
try {
if (conn == null || conn
try {
s
} catch (HibernateException e) {
logger
}
s = sessionFactory
sessionThread
}
} catch (SQLException e) {
throw new HibernateException(e);
}
}
return s;
}
/**
* 取得當前session的事務
*
* @return Transaction
*/
public static Transaction transaction() {
Transaction transaction = (Transaction) transactionThread
if (transaction == null) {
transaction = getSession()
transactionThread
}
return transaction;
}
/**
* 提交當前session的事務
*/
public static void commitTransaction() {
Transaction transaction = (Transaction) transactionThread
transactionThread
if (transaction != null)
mit();
}
/**
* 回滾當前session的事務
*/
public static void rollbackTransaction() {
Transaction tx = (Transaction) transactionThread
transactionThread
if (tx != null)
tx
}
/**
* 關閉當前線程使用的session
*/
public static void closeSession() {
Session session = (Session) sessionThread
if (session != null) {
session
session
sessionThread
}
}
}
下面是一個調用的例子: view plaincopy to clipboardprint?
public static void main(String[] args) throws Exception {
HibernateUtil
Session session = HibernateUtil
HibernateUtil
User u = new User();
u
session
mitTransaction();
HibernateUtil
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28894.html