Springboot and MyBatis in the transaction characteristics

Source: Internet
Author: User
Tags aop

1 MyBatis automatically participates in spring transaction management without additional configuration, as long as the Org.mybatis.spring.SqlSessionFactoryBean referenced data source and Datasourcetransactionmanager The referenced data source is consistent, otherwise transaction management will not work.

2 Transaction ISOLATION LEVEL

Isolation level refers to the degree of isolation between several concurrent transactions. Five constants representing the isolation level are defined in the Transactiondefinition interface:

    • Transactiondefinition.isolation_default: This is the default value that represents the default isolation level for using the underlying database. For most databases, this value is usually transactiondefinition.isolation_read_committed.
    • Transactiondefinition.isolation_read_uncommitted: This isolation level indicates that one transaction can read data that has been modified by another transaction but has not yet been committed. This level does not prevent dirty reads, non-repeatable reads, and Phantom reads, so the isolation level is rarely used. For example, PostgreSQL does not actually have this level.
    • Transactiondefinition.isolation_read_committed: This isolation level indicates that a transaction can only read data that has been committed by another transaction. This level prevents dirty reads, which is the recommended value in most cases.
    • Transactiondefinition.isolation_repeatable_read: This isolation level indicates that a transaction can repeatedly execute a query multiple times throughout the process, and that the records returned are the same each time. This level protects against dirty reads and non-repeatable reads.
    • Transactiondefinition.isolation_serializable: All transactions are executed one after the other, so that there is absolutely no possibility of interference between transactions, that is, the level prevents dirty reads, non-repeatable reads, and Phantom reads. However, this will severely affect the performance of the program. This level is not normally used.

3 Transaction propagation behavior

The so-called transaction propagation behavior is that if a transaction context already exists before the current transaction is started, there are several options to specify the execution behavior of a transactional method. The transactiondefinition definition includes the following constants that represent propagation behavior:

    • Transactiondefinition.propagation_required: If a transaction is currently present, the transaction is joined and a new transaction is created if there is no current transaction. This is the default value.
    • Transactiondefinition.propagation_requires_new: Creates a new transaction and suspends the current transaction if a transaction is currently present.
    • Transactiondefinition.propagation_supports: If a transaction is currently present, the transaction is joined, and if no transaction is currently present, it will continue in a non-transactional manner.
    • Transactiondefinition.propagation_not_supported: Runs in a non-transactional manner, suspending the current transaction if a transaction is currently present.
    • Transactiondefinition.propagation_never: Runs in a non-transactional manner and throws an exception if a transaction is currently present.
    • Transactiondefinition.propagation_mandatory: If a transaction is currently present, the transaction is joined and an exception is thrown if there is no current transaction.
    • Transactiondefinition.propagation_nested: If a transaction is currently present, create a transaction to run as a nested transaction for the current transaction, or if there is no current transaction, The value is equivalent to transactiondefinition.propagation_required.

4 Transaction Timeout

The so-called transaction timeout is the maximum time a transaction is allowed to execute, and if the time limit is exceeded but the transaction is not completed, the transactions are automatically rolled back. The value of int in transactiondefinition represents the time-out, in seconds.

The default setting is the timeout value for the underlying transaction system, or none if the underlying database transaction system does not have a timeout value set.

5 read-only transactions are used in situations where the client code is read-only but does not modify the data, and read-only transactions are used for optimizations under specific scenarios, such as when using Hibernate. The default is read-write transactions.

6 @Transactional Annotations

6.1 Properties

@Transactional can be used on interfaces, interface methods, classes, and class methods. When used on a class, all public methods of the class will have transactional properties of that type, and at the method level we can also use the callout to override the class-level definition.

Although @Transactional annotations can be used on interfaces, interface methods, classes, and class methods, Spring does not recommend using this annotation on an interface or interface method, because it takes effect only when using an interface-based proxy. In addition, @Transactional annotations should only be applied to the public method, which is determined by the nature of Spring AOP. If you use @Transactional annotations on protected, private, or default visibility methods, this is ignored and no exceptions are thrown.

By default, only method calls from outside are captured by the AOP proxy, which means that other methods inside the class that call this class do not cause transactional behavior, even if the called method uses @transactional annotations to decorate.

The annotation property on the method overrides the same property on the class annotation.

Description: Many tutorials on the web say that to use transactions need to configure two items, @EnableTransactionManagement and @Transactional, but in the process of using only the need to add @Transactional annotations where the transaction, Also support the transaction. The specific reasons are not yet clear.

Find answers as follows:

The Mybatis-spring-boot-starter 1.3.2 version will automatically introduce JDBC, so it should be injected with Datasourcetransactionmanager. The Springboot default data source is a JDBC-created data source that needs to be configured in the Application.properties configuration file.

II. configuration of database in Mybatis-spring-boot-starter

If you are not using the default data source, you need to inject it into the context of spring, where the datasource configuration is placed in the configuration class and then labeled as the primary data source using @Primary annotations.

Use annotations to inject mapper into the context of spring in a programmatic way. A more convenient way to use annotation scanning. Add @MapperScan annotations on the Springbootapplication class.

Description of the Mybatis.mapper-locations property in the 2.1 application.properties file:

If Mapper.xml and Mapper.class are under the same package and have the same name, spring scans the Mapper.class and automatically scans mapper.xml with the same name and assembles to Mapper.class.

If Mapper.xml and Mapper.class are not under the same package or a different name, you must use the configuration mapperlocations to specify the location of the mapper.xml.

Spring is determined by identifying the value of <mapper namespace= "Com.fan.mapper.UserDao" > Namespace in Mapper.xml to determine the corresponding mapper.class.

2.2 It is important to note that

Mybatis-spring-boot-starter would:

    • AutoDetect an existing DataSource.
    • Would create and register an instance of a sqlsessionfactory passing the DataSource as an input using th E Sqlsessionfactorybean.
    • Would create and register an instance of a sqlsessiontemplate got out of the sqlsessionfactory.
    • Autoscan your mappers, link them to the sqlsessiontemplate and registers them to Spring context so they can be inj ected into your beans.

Therefore: if it is a data source configuration, the transaction is not a problem, because it is a data source used. If more than one data source, then learn to summarize later.

Springboot and MyBatis in the transaction characteristics

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.