在Spring& SQL中同时处理Controller,Service和DAO Layer的异常的最佳实践是什么?过冬 [英] What are the best practices to handle exception at Controller, Service and DAO Layer simultaneously in Spring & Hibernate

查看:226
本文介绍了在Spring& SQL中同时处理Controller,Service和DAO Layer的异常的最佳实践是什么?过冬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Spring 3.2& Hibernate 3.6,可以解释如何处理Sping MVC& Hibernate ...我只是分享示例代码。

控制器层

  public Integer saveEployee(HttpServletRequest req,HttpServletResponse res){
Employee empObj = new Employee();
empObj.setName(req.getParameter(empName));
......................
.................... ..
Integer empId = materService.saveEmployee(empObj);
返回empId;
}

服务层

  public Integer saveEmployee(Employee empObj){
return masterDao.saveEmployee(empObj);

DAO图层

  public Integer saveEmployee(Employee empObj){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Integer empId = session.save(empObj);
tx.commit();
session.close();
返回empId;

$ / code>




  1. 现在假设发生任何异常DAO层,同时像d / b一样保存empObj,或连接失败,或者发生其他类型的休眠异常,如 ConstraintViolationException IntegrityConstraintViolationException


  2. 如果有像 NullPointerException 之类的java异常的机会或任何用户定义的异常等待在Controller层处理。

  3. 最佳做法控制器,服务和DAO层

    解决方案

    根据我在新项目中的经验, b
    $ b


    1. 您不应该在DAO层处理异常。
      原因:通常我们在服务层放置@Transactional注解。因此我们需要在服务层回滚事务。如果您在DAO处理异常,那么它将不会回滚。
      请按照下面的链接了解为什么我们应该在服务层放置@Transaction。

      应该在哪里@ Transactional放置服务层或DAO


    2. 你不应该在服务层处理异常。
      原因:由于服务层为数据库操作执行多个DAO,并且如果任何DAO失败,我们需要回滚事务。如果我们在服务中处理异常,那么我们可能不会回滚事务。
      有回滚事务的手动方法,但不推荐。
      TransactionAspectSupport.currentTransactionStatus()。setRollbackOnly();

    3. $ b 因此,处理控制层的事务。

      I am working on Spring 3.2 & Hibernate 3.6, can any one explain how to handle exception in Sping MVC & Hibernate...i just sharing sample code.

      Controller Layer

      public Integer saveEployee(HttpServletRequest req, HttpServletResponse res){
          Employee empObj = new Employee();
          empObj.setName(req.getParameter("empName"));
          ......................
          ......................
          Integer empId = materService.saveEmployee(empObj);
          return empId; 
      }
      

      Service Layer

      public Integer saveEmployee(Employee empObj){
          return masterDao.saveEmployee(empObj);
      }
      

      DAO Layer

      public Integer saveEmployee(Employee empObj){
          Session session = sessionFactory.openSession();
          Transaction tx = session.beginTransaction();
          Integer empId = session.save(empObj);
          tx.commit();
          session.close();
          return empId;
      }
      

      1. Now suppose any exception occurred at DAO layer while saving the empObj like d/b got down or connection failed or any other kind of hibernate exception occurred like ConstraintViolationException or IntegrityConstraintViolationException etc.

      2. If there is chances of java exception like NullPointerException or any user defined exception etc to be handle at Controller layer.

      So what are the best practices or how to handle the exception at Controller, Service and DAO Layer simultaneously.

      解决方案

      As per my experience during new project,

      1. You should not handle exception at DAO layer. Reason: Normally we place @Transactional annotation at Service Layer. hence we need to rollback transaction at service layer. If you handle exception at DAO then it will not rollback. Follow below link to know why we should place @Transaction at Service Layer.
        Where should "@Transactional" be place Service Layer or DAO
      2. You should not handle exception at Service Layer. Reason: As service layer execute multiple DAO for DB operation and we need to rollback transaction if any DAO fails. If we handle exception in service then we may not rollback transaction. There is manual approach for rollback transaction but it is not recommend. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

      So handle transaction in controller layer.

      这篇关于在Spring& SQL中同时处理Controller,Service和DAO Layer的异常的最佳实践是什么?过冬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆