Hibernate, Spring, @Transactional - surround with try/catch?

The Key feature used by Spring is Exception Translation rely on exception translation to generate exceptions your client layer understands and use try / catch there only, if possible.

The Key feature used by Spring is Exception Translation. Rely on exception translation to generate exceptions your client layer understands and use try / catch there only, if possible.

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.

Exceptions should never be swallowed like that. That way the proxy has no chance to detect the exception and roll back the transaction. I hope you mean something like this: catch(SomeException exc) { throw new IllegalStateException(exc); } – Sean Patrick Floyd Mar 21 at 14:39 @Sean that's just pseudocode.

I'd hope he wouldn't swallow an exception unless he had a good reason to do so. – Robby Pond Mar 21 at 14:43 that's why I'm asking, not downvoting. You should make stuff like that clear, there are many newbies around – Sean Patrick Floyd Mar 21 at 14:50 Thanks for answering.

The second pseudocode was just what I mentioned im my question. I did not mean to catch an exception IN a transactional method, but to surround the call of a transactional-annotated method.So the answer is not clear to me: I CAN do it that way, but I should not? How can I inform an user if a transaction is rolled back?

– nano7 Mar 21 at 14:50 You can use exception translation as mentioned in Sean's answer to generate a standard exception (in spring the DataAccessException or whatever) and use an @ExceptionHandler to catch the exception and inform the user. I updated my answer to show an example. – Robby Pond Mar 21 at 15:01.

Thanks for answering me. I read through the link (spring documentation) and I found the following: "However, the DAO throws plain bernateException (which is unchecked, so does not have to be declared or caught), which means that callers can only treat exceptions as generally fatal - unless they want to depend on bernate's own exception hierarchy. Catching specific causes such as an optimistic locking failure is not possible without tying the caller to the implementation strategy.

This trade off might be acceptable to applications that are strongly bernate-based and/or do not need any special exception treatment. " My DAO is based on Plain so if I understand it correct, my DAO only throws plain They are unchecked and do not have to be declared or caught. If something goes wrong, with @Transactional the whole operation is rolled URL1 make sure that everything works as I expected it to work, I have to tie my DAO even closer to my application code.

There I can check for example if an object is given back or not.(if null - else) I could also catch the exception, log it and inform the user that something did go wrong and that his transaction did not work. So at this point, Im still thinking, that - depending on the transaction: if I can work with a result, everything is ok - if not, I can inform the user. When the transaction is not specified to give back a result, I can use try/catch for catching a But is the Transaction still rolled back then?

I thought, catching an bernateException avoids Rolling Back of the transaction. I still do not know what to do. :-( Beside of this unfortunately I do not understand what MVC Exception Handling (@ExceptionHandler) has to do with this.

There was a table of handled exceptions, but I did not find the Or do you think it will work with this: @ExceptionHandler( Classs)? Also you said you would not recommend to handle exceptions this way.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions