Transactions can guarantee the same service in multiple write operations on the data of all succeed, or all fail, in order to ensure data security.
Therefore, a business has more than once add / delete / change operations, the transaction must be used, for example, in a business operation Update 2 times, 1 times or Delete operation Update plus 1, or even 3 times the Update operation Wait.
After using SpringJDBC, if you need to use a transaction to ensure data security, just add `@ Transactional` before a business method can be annotated.
In SpringJDBC, the transaction operation is approximately:
Open transaction: the begin the try { perform data operations, increase / change / delete commits the transaction: the commit } the catch (a RuntimeException E) { roll back the transaction: ROLLBACK }
That is, when the framework of the implementation of business by way of a transaction, the transaction will open, if the normal execution is completed, the transaction is committed, if the implementation process is captured `RuntimeException`, will be automatically rolled back.
`@ Transactional` annotations can also be added prior to business class, it means that the business class in all business methods are a way to execute a transaction, usually do not recommend doing this, unnecessary transactions have a negative impact on the QPS.
You can also configure `rollbackFor` in the annotation to determine which exception will automatically roll back the transaction capture, such as:
@Transactional(rollbackFor=ServiceException.class)
Summary: If a business involves multiple add / delete / change, add `@ Transactional` comment before business methods, and, in the course of the operation as an error, you must throw` RuntimeException` abnormal or descendant class .