Use of Spring Boot transactions
Spring Boot is very simple to use transactions. First, use annotation @ EnableTransactionManagement to enable transaction support, and then add annotation @ Transactional to the Service method used to access the database.
For the Transaction Manager, both JPA and JDBC implement the self-interface PlatformTransactionManager. if you add a spring-boot-starter-jdbc dependency, the framework will inject the DataSourceTransactionManager instance by default. If you add a spring-boot-starter-data-jpa dependency, the Framework injects the JpaTransactionManager instance by default.
You can add the following method to the startup class and Debug the test to know which implementation class of the PlatformTransactionManager interface is automatically injected.
@ EnableTransactionManagement // enable annotation transaction management, equivalent to the xml configuration method
@ SpringBootApplicationpublic class ProfiledemoApplication {@ Bean public Object testBean (PlatformTransactionManager platformTransactionManager) {System. out. println (">>>>>>>>>" + platformTransactionManager. getClass (). getName (); return new Object ();} public static void main (String [] args) {SpringApplication. run (ProfiledemoApplication. class, args );}}
SpringBoot is automatically implemented for us, and these are not transparent to us. If your project is relatively large and you add more persistent dependencies, we will still choose to manually specify which Transaction Manager to use.
The Code is as follows:
@ EnableTransactionManagement @ SpringBootApplicationpublic class ProfiledemoApplication {// The dataSource framework automatically injects @ Bean public PlatformTransactionManager txManager (DataSource dataSource) {return new DataSourceTransactionManager (dataSource );} @ Bean public Object testBean (PlatformTransactionManager platformTransactionManager) {System. out. println (">>>>>>>>>" + platformTransactionManager. getClass (). getName (); return new Object ();} public static void main (String [] args) {SpringApplication. run (ProfiledemoApplication. class, args );}}
In the Spring container, the @ Bean annotation will be manually loaded first, and the framework will not re-instantiate other PlatformTransactionManager implementation classes.
Then, in the Service, the @ Transactional annotation method will support transactions. If the annotation is on the class, all methods of the entire class support transactions by default.
For how to handle multiple transaction managers in the same project, see the following example. For details, see the notes in the code.
@ EnableTransactionManagement // enable annotation transaction management, which is equivalent
@ SpringBootApplicationpublic class ProfiledemoApplication implements TransactionManagementConfigurer {@ Resource (name = "txManager2") private PlatformTransactionManager txManager2; // create Transaction Manager 1 @ Bean (name = "txManager1 ") public PlatformTransactionManager txManager (DataSource dataSource) {return new cetcetransactionmanager (dataSource);} // create Transaction Manager 2 @ Bean (name = "txManager2") public PlatformTransactionManager txManager2 (EntityManagerFactory factory) {return new JpaTransactionManager (factory);} // implements the TransactionManagementConfigurer method. The returned value indicates the default Transaction Manager @ Override public PlatformTransactionManager annotationDrivenTransactionManager () when multiple transaction managers exist () {return txManager2;} public static void main (String [] args) {SpringApplication. run (ProfiledemoApplication. class, args );}}
@ Componentpublic class DevSendMessage implements SendMessage {// use value to specify which Transaction Manager to use @ Transactional (value = "txManager1") @ Override public void send () {System. out. println (">>>>>>> Dev Send () <"); send2 ();} // if multiple transaction managers exist and value is specified, // The Transaction Manager @ Transactional public void send2 () {System returned by annotationDrivenTransactionManager () is used by default. out. println (">>>>>>> Dev Send2 () <");}}
Note:
If the Spring container contains multiple PlatformTransactionManager instances and the TransactionManagementConfigurer interface is not implemented to specify the default value, you must specify the value when using the annotation @ Transactional in the method. If this parameter is not specified, an exception is thrown.
When the system needs to provide default transaction management, the implementation interface TransactionManagementConfigurer is specified.
For some systems, in order to avoid unnecessary problems, you must specify the value of @ Transactional in the business. It is not recommended to implement the TransactionManagementConfigurer interface, so that the console will explicitly throw an exception and developers will not forget to manually specify it.