单独使用hibernate处理事务
本来只用hibernate开发,从而可以省了DAO层实现数据库访问和跨数据库,也可以对代码进行更好的封装,当我们web中单独使用hibernate时,我们需要单独的处理hibernate的事务,我是使用filter来对事务进行控制的:
单独使用hibernate使用filter进行事务控制:
01
public
class
HibernateSessionFilter
implements
Filter {
02
03 public void destroy() {
04
05 }
06
07 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
08 ServletException {
09 Session session = HibernateUtils.openSession();
10 Transaction tx = null ;
11 try {
12 tx = session.beginTransaction();
13 chain.doFilter(request, response);
14 tx.commit();
15 } catch (Exception e) {
16 if (tx != null ) {
17 tx.rollback();
18 }
19 throw new RuntimeException(e);
20 } finally {
21 HibernateUtils.closeAndRemoveSession();
22 }
23 }
24
25 public void init(FilterConfig arg0) throws ServletException {
26 }
27
28 }
02
03 public void destroy() {
04
05 }
06
07 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
08 ServletException {
09 Session session = HibernateUtils.openSession();
10 Transaction tx = null ;
11 try {
12 tx = session.beginTransaction();
13 chain.doFilter(request, response);
14 tx.commit();
15 } catch (Exception e) {
16 if (tx != null ) {
17 tx.rollback();
18 }
19 throw new RuntimeException(e);
20 } finally {
21 HibernateUtils.closeAndRemoveSession();
22 }
23 }
24
25 public void init(FilterConfig arg0) throws ServletException {
26 }
27
28 }
web.xml
01
<
filter
>
02 < filter - name > hibernateSessionFilter </ filter - name >
03 < filter - class > syx.jpkc.filter.HibernateSessionFilter </ filter - class >
04 </ filter >
05 < filter - mapping >
06 < filter - name > hibernateSessionFilter </ filter - name >
07 < url - pattern >* .syx </ url - pattern >
08 < url - pattern >* .jsp </ url - pattern >
09 < url - pattern >* .eve </ url - pattern >
10 </ filter - mapping >
02 < filter - name > hibernateSessionFilter </ filter - name >
03 < filter - class > syx.jpkc.filter.HibernateSessionFilter </ filter - class >
04 </ filter >
05 < filter - mapping >
06 < filter - name > hibernateSessionFilter </ filter - name >
07 < url - pattern >* .syx </ url - pattern >
08 < url - pattern >* .jsp </ url - pattern >
09 < url - pattern >* .eve </ url - pattern >
10 </ filter - mapping >
我主要在servlet(*.syx,*.eve)和jsp页面(没用struts)需要和数据库操作,所以需要使用事务处理。
上面我们还用到了一个 HibernateUtils的小工具类,主要为了获取Session对象和一点优化:
HibernateUitls.java
01
public
class
HibernateUtils {
02 private static Map < Thread, Session > sessionMap;
03 private static SessionFactory sessionFactory;
04 static {
05 sessionMap = new HashMap < Thread, Session > ();
06 sessionFactory = new Configuration().configure().buildSessionFactory();
07 }
08
09 /**
10 * can only use in web filter, beause it should remove and clear resources
11 * @return
12 */
13 public static Session openSession() {
14 System.out.println(Thread.currentThread().getStackTrace()[ 1 ] + " run in " + new Date());
15 Session session = sessionMap.get(Thread.currentThread());
16 if (session == null ) {
17 session = sessionFactory.openSession();
18 sessionMap.put(Thread.currentThread(), session);
19 }
20 return session;
21 }
22 public static Session getCurrentSession() {
23 return sessionMap.get(Thread.currentThread());
24 }
25
26 public static void closeAndRemoveSession() {
27 System.out.println(Thread.currentThread().getStackTrace()[ 1 ] + " run in " + new Date()); //
28 Session session = sessionMap.remove(Thread.currentThread());
29 if (session != null ) {
30 session.close();
31 }
32 }
33 }
01 public class HibernateUtils {
02 private static Map < Thread, Session > sessionMap;
03 private static SessionFactory sessionFactory;
04 static {
05 sessionMap = new HashMap < Thread, Session > ();
06 sessionFactory = new Configuration().configure().buildSessionFactory();
07 }
08
09 /**
10 * can only use in web filter, beause it should remove and clear resources
11 * @return
12 */
13 public static Session openSession() {
14 System.out.println(Thread.currentThread().getStackTrace()[ 1 ] + " run in " + new Date());
15 Session session = sessionMap.get(Thread.currentThread());
16 if (session == null ) {
17 session = sessionFactory.openSession();
18 sessionMap.put(Thread.currentThread(), session);
19 }
20 return session;
21 }
22 public static Session getCurrentSession() {
23 return sessionMap.get(Thread.currentThread());
24 }
25
26 public static void closeAndRemoveSession() {
27 System.out.println(Thread.currentThread().getStackTrace()[ 1 ] + " run in " + new Date()); //
28 Session session = sessionMap.remove(Thread.currentThread());
29 if (session != null ) {
30 session.close();
31 }
32 }
33 }
02 private static Map < Thread, Session > sessionMap;
03 private static SessionFactory sessionFactory;
04 static {
05 sessionMap = new HashMap < Thread, Session > ();
06 sessionFactory = new Configuration().configure().buildSessionFactory();
07 }
08
09 /**
10 * can only use in web filter, beause it should remove and clear resources
11 * @return
12 */
13 public static Session openSession() {
14 System.out.println(Thread.currentThread().getStackTrace()[ 1 ] + " run in " + new Date());
15 Session session = sessionMap.get(Thread.currentThread());
16 if (session == null ) {
17 session = sessionFactory.openSession();
18 sessionMap.put(Thread.currentThread(), session);
19 }
20 return session;
21 }
22 public static Session getCurrentSession() {
23 return sessionMap.get(Thread.currentThread());
24 }
25
26 public static void closeAndRemoveSession() {
27 System.out.println(Thread.currentThread().getStackTrace()[ 1 ] + " run in " + new Date()); //
28 Session session = sessionMap.remove(Thread.currentThread());
29 if (session != null ) {
30 session.close();
31 }
32 }
33 }
01 public class HibernateUtils {
02 private static Map < Thread, Session > sessionMap;
03 private static SessionFactory sessionFactory;
04 static {
05 sessionMap = new HashMap < Thread, Session > ();
06 sessionFactory = new Configuration().configure().buildSessionFactory();
07 }
08
09 /**
10 * can only use in web filter, beause it should remove and clear resources
11 * @return
12 */
13 public static Session openSession() {
14 System.out.println(Thread.currentThread().getStackTrace()[ 1 ] + " run in " + new Date());
15 Session session = sessionMap.get(Thread.currentThread());
16 if (session == null ) {
17 session = sessionFactory.openSession();
18 sessionMap.put(Thread.currentThread(), session);
19 }
20 return session;
21 }
22 public static Session getCurrentSession() {
23 return sessionMap.get(Thread.currentThread());
24 }
25
26 public static void closeAndRemoveSession() {
27 System.out.println(Thread.currentThread().getStackTrace()[ 1 ] + " run in " + new Date()); //
28 Session session = sessionMap.remove(Thread.currentThread());
29 if (session != null ) {
30 session.close();
31 }
32 }
33 }
本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/wws5201985/735587,如需转载请自行联系原作者