V. Spring BOOT Transaction

Source: Internet
Author: User

Spring Boot uses transactions very simply, first using annotations @EnableTransactionManagement turn on transaction support, and then add annotations @Transactional to the service methods that access the database.

About 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 default Datasourcetransactionmanager instance. If you add a SPRING-BOOT-STARTER-DATA-JPA dependency, the framework will inject the Jpatransactionmanager instance by default.

You can add the following method to the startup class, and the debug test will know which implementation class is automatically injected into the Platformtransactionmanager interface.

@EnableTransactionManagement//start annotation transaction management, equivalent to the way XML is configured <tx:annotation-driven/>@ Springbootapplicationpublic  Class profiledemoapplication { @Bean public Object testbean (Platformtransactionmanager Platformtransactionmanager) {System.out.println (return new Object ();} public static void main (string[] args) {Springapplication.run (profiledemoapplication.class, args);}}    

These springboot for us to do automatically, these are not transparent to us, if your project is relatively large, add more persistent dependency, we will still choose to specify which transaction manager to use.
The code is as follows:

@EnableTransactionManagement@SpringBootApplicationPublicClassprofiledemoapplication {Where the DataSource framework will automatically inject us @Bean public platformtransactionmanager txmanager (DataSource DataSource) {return new Datasourcetransactionmanager (DataSource); }  @Bean public Object testBean ( Platformtransactionmanager Platformtransactionmanager) {System.out.println (return new Object ();} public static void main (string[] args) {Springapplication.run (profiledemoapplication.class, args);}}    

In the spring container, we manually annotated that the @bean will be loaded first, and the framework will not re-instantiate the other Platformtransactionmanager implementation classes.

Then in the service, the method that is @Transactional annotated will support the transaction. If the annotation is on a class, all methods of the entire class support the transaction by default.

For more than one transaction manager to handle in the same project, see the following example to see the comments in the code.

@EnableTransactionManagementTurn on annotation transaction management, equivalent to <tx:annotation-driven in an XML configuration file/>@SpringBootApplicationPublicClassProfiledemoapplicationImplementsTransactionmanagementconfigurer {@Resource (name="TxManager2")Private Platformtransactionmanager TxManager2;Creating the transaction manager 1@Bean (name ="TxManager1")Public PlatformtransactionmanagerTxmanager (DataSource DataSource) {ReturnNew Datasourcetransactionmanager (DataSource); }//Create transaction manager 2  @Bean (name =  " TxManager2 ") public platformtransactionmanager txManager2 ( Entitymanagerfactory Factory) {return new Jpatransactionmanager (Factory); The //implements the interface Transactionmanagementconfigurer method, whose return value represents the transaction manager  @Override public platformtransactionmanager  Annotationdriventransactionmanager () {return TxManager2;} public static void main (string[] args) {Springapplication.run (profiledemoapplication.class, args);}}    
@ComponentPublicClassDevsendmessageImplementssendmessage {//use value to specify which transaction manager to use  @Transactional (Value= "TxManager1")  @Override public void send () {System.out.println (//in the presence of multiple transaction managers, if you specify //by using value, the default method of use Annotationdriventransactionmanager () returns the transaction manager  @Transactional public void send2 () { System.out.println ( ">>>>>>>>dev Send2 () <<<<< <<< "); }}

If there is more than one Platformtransactionmanager instance in the spring container, and no interface is implemented Transactionmanagementconfigurer the default value is specified, we use annotations on the method @ When transactional, it must be specified with value, and if not specified, an exception is thrown.

Implements the interface transactionmanagementconfigurer specified for the system to provide default transaction management.

For some systems, in order to avoid unnecessary problems, the business must explicitly specify @Transactional value values. It is not recommended to implement interface Transactionmanagementconfigurer, so the console will explicitly throw exceptions, and developers will not forget to actively specify them.

V. Spring BOOT Transaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.