Declarative transaction & dirty read & non-repeatable read & phantom read

  • Programmatic transaction: programmers program transaction control code
  • Declarative transaction: The transaction control code has been written by spring. The programmer only needs to declare which methods need transaction control and how to perform transaction control
  • Declarative transactions are all aimed at methods under the ServiceImpl class.
  • Configure which methods need to be managed by spring, generally to manage the methods in the mapper interface
  • Based on advice, the bottom layer will generate advice classes based on methods, start the transaction before the transaction in this class, commit or roll back after the transaction (replaces the function of filter openSeesionInview in mybatis)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        
         <!--代替mybatis中的mybatis.xml的功能  -->
   		     <!--数据源  -->
   		<context:property-placeholder location="classpath:db.properties"/>
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		</bean>
			<!--相当于mabatis中的工厂,spring帮助进行创建工厂 
				 typeAliasesPackage标签相当于mybatis的typeAliases标签,类名可以简写-->
		<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
			
			<property name="dataSource" ref="dataSource"></property> 
        	<property name="typeAliasesPackage" value="cn.wit.pojo"></property>
		</bean>
		
			<!--相当于mappers标签  -->
        <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
			 <property name="basePackage" value="cn.wit.mapper"></property>
			<property name="sqlSessionFactoryBeanName" value="factory"></property>
			<!-- <property name="sqlSessionFactory" ref="factory"></property> -->
		</bean>
        
        
        
        <!--spring进行事务管理的类  -->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        	<property name="dataSource"></property>
        </bean>
        <!--配置声明式事务,本质上是切点的通知  -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
        	<tx:attributes>
        		<tx:method name="ins*"/>
        		<tx:method name="del*"/>
        		<tx:method name="upd*"/>
        		<tx:method name="*" readonly="true"/>
        	</tx:attributes>
        </tx:advice>
        <aop:config>
        	<aop:pointcut expression="execution(* cn.wit.serviceImpl.*.*(..))" id="mypoint"/>
        	<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
        </aop:config>
</beans>

name=" "

  • Which methods need transaction control, support * wildcard

readonly=”boolean”

  • Whether it is a read-only transaction, if it is true, tell the database that the transaction is a read-only transaction. Data optimization will improve performance, so as long as it is a query method, it is recommended to use this data. If it is false (default value), The transaction needs to be submitted. It is recommended to add, delete, modify

propagation control transaction propagation behavior

  • When a method with transaction control is called by another method with transaction control, how do you need to manage the transaction (create a new transaction? Execute in a transaction? Suspend the transaction? Report an exception?)
  • REQUIRED (default): If there is currently a transaction, execute it in the transaction, if there is no transaction currently, create a new transaction.
  • SUPPORTS: If there is currently a transaction, it will be executed in a transaction, if there is no transaction currently, it will be executed in a non-transactional state
  • MANDATORY: It must be executed inside the transaction. If there is a current transaction, it will be executed in the transaction. If there is no transaction, an error will be reported.
  • REQUIRES_NEW: must be executed in a transaction, if there is no transaction currently, create a new transaction, if there is a current transaction, suspend the current transaction.
  • NOT_SUPPORTED: must be executed under non-transactional, if there is no transaction currently, execute normally, if there is a current transaction, suspend the current transaction
  • NEVER: must be executed in a non-transactional state, if there is no transaction currently, execute normally, if there is currently a transaction, an error will be reported
  • NESTED: Must be executed in a transaction state. If there is no transaction, create a new transaction, if there is a transaction currently, create a nested transaction
  • Isolation=”” Transaction isolation level , how to ensure the integrity of the accessed data under multi-threaded or concurrent access.

Transaction isolation level isolation

  • DEFAULT: The default value, the underlying database automatically determines what isolation level should be used
  • READ_UNCOMMITTED: Uncommitted data can be read, dirty reads, non-repetitive reads, and phantom reads may occur
  • READ_COMMITTED: Only the submitted data can be read, no dirty reads will occur
  • REPEATABLE_READ: The read data is locked to prevent other transactions from modifying this data, which can prevent non-repeatable reads. Dirty reads, phantom reads may occur
  • SERIALIZABLE: Queuing operations, adding locks to the entire table. When a transaction is operating data, another transaction waits for the transaction operation to complete before operating this table

Dirty read

Transaction A reads the data that has not yet been committed by transaction B (READ_COMMITTED can be solved by prohibiting read cache)

Non-repeatable

After transaction A reads the data, the data is changed by transaction B (modification operation, REPEATABLE_READ can be solved by locking the corresponding data)

Phantom reading

Transaction A queries data, transaction B adds or deletes (the elimination of phantom reading can only lock one piece of data, and the entire table must be locked SERIALIZABLE)

Transaction rollback rollback-for

  • Need to roll back when something abnormal occurs
  • rollback-for=”Abnormal type fully qualified path”
  • Suggestion: give the attribute value
  • To manually throw an exception, you must give the attribute value.
  • no-rollback-for=””
  • Do not roll back the transaction when something abnormal occurs.

Guess you like

Origin blog.csdn.net/WA_MC/article/details/112678212