Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :-)

like image 412
nano7 Avatar asked Mar 21 '11 14:03

nano7


People also ask

Does @transactional throw exception?

@Transactional only rolls back transactions for unchecked exceptions. For checked exceptions and their subclasses, it commits data. So although an exception is raised here, because it's a checked exception, Spring ignores it and commits the data to the database, making the system inconsistent.

Does @transactional close session?

@Transactional helps you to extend scope of Session . Session is open first time when getCurrentSession() is executed and it is closed when transaction ends and it is flushed before transaction commits. In Spring, If we use getCurrentSession() in non-transactional context we get an exception.

What does @transactional do in hibernate?

The @Transactional annotation is the metadata that specifies the semantics of the transactions on a method. We have two ways to rollback a transaction: declarative and programmatic. In the declarative approach, we annotate the methods with the @Transactional annotation.

Can we use @transactional in service layer?

The @Transactional annotation belongs to the Service layer because it is the Service layer's responsibility to define the transaction boundaries.


2 Answers

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.

like image 133
Robby Pond Avatar answered Sep 28 '22 06:09

Robby Pond


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.

like image 40
Sean Patrick Floyd Avatar answered Sep 28 '22 07:09

Sean Patrick Floyd