Which data is stored in @Transactional services, especially in the transaction? If I have a controllers layout, services layout, daos and database - why I have to use my services with @Transactional annotation and what data is stored between these layouts? For example, I send some object data and I want it write to database. Well, in the transaction will be stored all this data? But what if I only update some data in database by giving and id of the object? Can you help me understand it?
相关问题
- java.lang.IllegalArgumentException: Cannot set to
- Spring Data MongoDB - lazy access to some fields
- Declaring an explict object dependency in Spring
- NOT DISTINCT query in mySQL
- Decoding body parameters with Spring
相关文章
- java JDK动态代理和cglib动态代理最后获取的代理对象都为null的问题
- org.xml.sax.SAXParseException; lineNumber: 7; colu
- SpringMVC如何把File封装到Map中?
- Spring: controller inheritance using @Controller a
- How to load @Configuration classes from separate J
- Java spring framework - how to set content type?
- Java/Spring MVC: provide request context to child
- Spring 5 Web Reactive - Hot Publishing - How to us
It's not about stored data in transaction. It's about running some operations in one transaction.
Imagine that you create banking system, and you have method for making money transfer. Let's assume that you want to transfer amount of money from accountA to accountB
You can try something like that in controller:
But this approach have some serious problems. Namely what if update operation for accountA succeed but update for accountB fail? Money will disappear. One account lost it but second account didn't get it.
That's why we should make both operations in one transaction in service method something like this:
This method is tagged with @Transactional, meaning that any failure causes the entire operation to roll back to its previous state. So if one of updates fail, other operations on database will be rolled back.