Hibernate,Spring,@Transactional - 用try / catch包围? [英] Hibernate, Spring, @Transactional - surround with try/catch?
问题描述
@Transactional
Annotation和代码结构提出了一些问题。 - >当我使用 @Transactional
(使用Spring进行事务管理),是否必须使用try / catch来包围 @Transactional
- 注解方法调用它们?
例如,当我得到一个加载,更改和返回的方法,然后我从另一个类调用它时:是否必须包围呼叫与try / catch?也许出了什么问题,没有对象返回,数据库连接失败..我不知道。
到现在为止,我认为 @Transactional
关心所有可能发生的异常,并在发生错误时回滚此事务中的每个操作。
,但如果发生,我必须以某种方式通知用户。当我调用try-block中的transactional-method并且它被回滚时,catch块被激活?
我可以告诉用户出错了。否则,用户可能不会被通知?
或者是否足以检查是否有返回的对象(if / else),那么我不需要try / catch?
Im新的,我想听听其他代码结构如何。谢谢: - )
您可以使用 @ ExceptionHandler 来处理特定的异常,而不是在try-catch块中自己处理。
如果用户想要一个未找到的资源,并且您想发送一个404.
@ResponseStatus(HttpStatus.NOT_FOUND)
public void handleNotFoundException(NotFoundException exc){
//记录一些东西。
}
如果有服务器问题需要发送500
@ExceptionHandler(SomeException.class)
public void handleException(SomeException exc,WebRequest请求,HttpServletResponse响应){
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,对不起,我的服务器坏了);
}
您也应该狭义地处理异常。一般来说,你不应该做 @ExceptionHandler(Exception.class)
,我也相信它按顺序工作,所以如果你处理一般异常,它应该是最后一个方法。
Im working on developing a webapplication with Spring 3 and Hibernate 3.6. Ive got some questions to the @Transactional
Annotation and the structure of the code.
-> When I use @Transactional
(transaction management with Spring), do I have to surround the @Transactional
-annotated methods with try/catch when calling them?
For example, when I got a method which loads, changes and returns then an object and I call it from another class: do I have to surround the call with try/catch? maybe something goes wrong, no object is returned, database connection fails.. I dont know.
until now, I thought @Transactional
cares for all possible occurring exceptions and rolls back every operation in this transaction when an error occurs.
but if it occurs, I must inform the user somehow. when I call the transactional-method in the try-block and it is rolled back, the catch block is activated?
I can tell then the user "something did go wrong". Otherwise the user maybe would not be informed?
Or is it sufficient to check if there is an object returned (if/else), then I dont need try/catch? Im new and I would like to hear how other structure their code. Thank you :-)
Handling exceptions in Spring is really easy with HandlerExceptionResolvers and @ExceptionHandlers. I tend to use @ExceptionHandler exclusively.
You can use an @ExceptionHandler to handle a specific exception instead of handling it yourself in a try-catch block.
If the user wanted a resource that wasn't found and you want to send a 404.
@ExceptionHandler(NotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public void handleNotFoundException(NotFoundException exc) {
// log something.
}
If there was a Server issue where you'd want to send a 500
@ExceptionHandler(SomeException.class)
public void handleException(SomeException exc, WebRequest request, HttpServletResponse response) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Sorry dude, my server broke");
}
You also should narrowly handle the exceptions. In general you shouldn't do @ExceptionHandler(Exception.class)
and I also believe that it works in order, so if you do handle the general Exception it should be the last method in the class.
这篇关于Hibernate,Spring,@Transactional - 用try / catch包围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!