疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

hibernate 与spring的事务处理


 

  原本只用hibernate开发,可节省DAO层实现数据库访问和数据库,也可以更好的代码封装,当我们使用WEB在使用hibernate的时候,我们需要分开处理hibernate事务,我是使用过滤器来控制事务:

  单独使用的过滤器用于控制:

  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 }

  web.xml

  01

  02 hibernateSessionFilter

  03 syx.jpkc.filter.HibernateSessionFilter

  04

  05

  06 hibernateSessionFilter

  07 *.syx

  08 *.jsp

  09 *.eve

  10

  我主要在servlet(*.syx,*.eve)和jsp页面(没用struts)需要和数据库操作,所以需要使用事务处理。

  上面我们还用到了一个 HibernateUtils的小工具类,主要为了获取Session对象和一点优化:

  HibernateUitls.java

  01 public class HibernateUtils {

  02 private static Map sessionMap;

  03 private static SessionFactory sessionFactory;

  04 static {

  05 sessionMap = new HashMap();

  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 sessionMap;

  03 private static SessionFactory sessionFactory;

  04 static {

  05 sessionMap = new HashMap();

  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 }