Issue
I had tried to terminate that by myself, but i can't. I need to save objects in multiple relative tables of my database and it must be in one single transaction. I am using Servlets, JSP, JDBC. (already have the dao layer and service layer) As we know, the transactions always must to be in the service layer.
When I was using Spring MVC, I always used this annotation in services:
@Transactional
and had options for TransactionManager in my spring.xml Now I need to do the same with servlets. Can anyone help me with some small example for servlets transactions or maybe somebody have suggestive thoughts for this?
Solution
You have different ways to manage transactions at JDBC level.
The simplest way, is a filter: you open a transaction at the beginning or the request processing and commit (or rollback) it at the end. It is as little invasive as possible in other layers, but you cannot have the transaction demarcation at the service layer.
At the opposite, you can add code to explicitely create and commit transactions in all (relevant) service methods. You can put real code in common methods to limit code duplication, but you will have to consistently modify all your service layer.
An alternate way, as you have an existant service layer, would be to mimic Spring and use proxies around your service classes. The proxies would open create transaction, call the real method and commit the transaction. IMHO, it would still be a little invasive method with little code duplication.
My choice would be to use method 1 for very simple use cases or prototyping and method 3 for more serious ones - but this is just my opinion.
Answered By - Serge Ballesta
Answer Checked By - David Marino (JavaFixing Volunteer)