问题描述
我正在研究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>
-
现在假设发生任何异常DAO层,同时像d / b一样保存empObj,或连接失败,或者发生其他类型的休眠异常,如
ConstraintViolationException
或IntegrityConstraintViolationException
-
如果有像
NullPointerException
之类的java异常的机会或任何用户定义的异常等待在Controller层处理。 最佳做法控制器,服务和DAO层。 - 您不应该在DAO层处理异常。
原因:通常我们在服务层放置@Transactional注解。因此我们需要在服务层回滚事务。如果您在DAO处理异常,那么它将不会回滚。
请按照下面的链接了解为什么我们应该在服务层放置@Transaction。
- 你不应该在服务层处理异常。
原因:由于服务层为数据库操作执行多个DAO,并且如果任何DAO失败,我们需要回滚事务。如果我们在服务中处理异常,那么我们可能不会回滚事务。
有回滚事务的手动方法,但不推荐。
TransactionAspectSupport.currentTransactionStatus()。setRollbackOnly();
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
orIntegrityConstraintViolationException
etc.If there is chances of java exception like
NullPointerException
or any user defined exception etc to be handle at Controller layer.- 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 - 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();
根据我在新项目中的经验, b
$ b
$ 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;
}
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,
So handle transaction in controller layer.
这篇关于在Spring& SQL中同时处理Controller,Service和DAO Layer的异常的最佳实践是什么?过冬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!