0

Im working on developing a webapplication with Spring Framework 3.0.5 and Hibernate Framework 3.6 and Ive got some questions to it. I use Transaction Management with Annotations. (@Transactional) and my DAO is written on plain Hibernate 3 API.

1) How can I notice that a transaction is rolled back? (I mean, I do have to catch exceptions? or maybe check after, if everything worked? surrounding @transactional-methods with try-catch doesnt seem like a good thing to me)

2) Is it correct that transaction management with @transactional does not work (not rollback) when I catch the exception? (when I surround the call of a transactional-method with try-catch)

3) Is it possible to use Hibernate WITHOUT using transactions? so I could use Spring with Hibernate but without Transaction Management?

4) If its not possible to use Hibernate without using transactions, this means that my database management system always has to support transactional engines (like InnoDB). I cant use a myISAM table together with hibernate then? That would be a big disadvantage of hibernate then!?

thank you :-)

1 Answer 1

3
  1. Spring will automatically rollback a transaction if a runtime exception is thrown from a method annotated with @Transactional. You usually don't care if it's rolled back or not, because the exception will propagate up to the call stack. If you really need to know if the transaction is rolled back, use TransactionAspectSupport.currentTransactionStatus().isRollbackOnly().

  2. No. If you catch a runtime exception thrown from a transactional method, the transaction used to run this method is rolled back.

  3. No. You need transactions to use Hibernate, and this is a good thing. Doing database work without transactions is a recipe for disasters.

  4. Yes, you need a transactional engine.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! I'd just like to understand why people usually don't care if a transaction is rolled back or not. For example, when a user wants to register and it does not work, the transaction is rolled back. But since I won't be aware of it, I won't inform the user. I don't think thats a good strategy. I will take a look at "TransactionAspectSupport.currentTransactionStatus().isRollbackOnly()", as you recommended. Id also like to know why there is an article on hibernates webpage which describes how to access data without transaction, if its not possible to use hibernate without transactions :-)
This is the article I mentioned. community.jboss.org/wiki/… Maybe I misunderstood something...? Thanks so far! :-)
@nano7: If there is no exception, then the transaction hasn't rolled back. If there is an exception, the transaction is rolled back and the exception will propagate, until it's handled in the presentation layer, where the user can be informed. No need to test for rollback if there's no exception.
Really(!) sorry for reasking again. Im somehow a newbie. You said the exception thrown by the @transactional-annotated method will propagate up to the call stack. And I can handle it somewhere then. How do I get this exception if not with try/catch? maybe with @ExceptionHandlers? really sorry for asking, but I simply do not really understand it. do you have an example or a good tutorial, I would be very grateful!
@nano7 You can catch the exception at the controller layer. In a well designed system, your transactions should start at the service layer. When an exception is thrown the controller catches it and redirects the view to an error page, else proceeds to render a success page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.