Spring configuration transaction

There are two types of transaction configurations in Spring: 1. Annotation-based; 2. XML-based file

1. Based on annotations

Configuration process:

1. Spring configuration file configuration needs to be added:

<!-- Configure Transaction Manager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource" /><!-- ref: import data source -->
</bean>
<!-- The value of the startup transaction annotation transaction-manager must be the same as the id of the bean above -->
<tx:annotation-driven transaction-manager="transactionManager"/>

2. Add the @Transactional annotation to the method that needs to use the transaction.

Attributes of the @Transactional annotation:

1) The propagation behavior of things: propagation, that is, how to use the thing when the current thing method is called by another thing method. The default value is REQUIRED, that is, the thing that uses the called method.

2) The isolation level of the transaction: isolation: specifies the isolation level of the transaction. The most commonly used value is: READ_COMMITTED, read to commit.

3) Transaction rollback:

    (1) noRollbackFor, noRollbackForClassName: Usually, the default value can be taken without rolling back this exception.

    (2) rollbackFor, rollbackForClassName: rollback this exception Usually, the default value can be taken.

4) Read-only transaction: readOnly, which can only read data, can help database engine optimization. If it is read-only, it should be set: readOnly=true

5) Forced rollback time: timeout: specifies the time of forced rollback, in seconds. If the method executes for 5 seconds, and the property is set to 2 seconds, if it reaches 2 seconds, the method has not been executed yet, the The transaction will also force a rollback of this method.

It is written as follows:

/**
	 * Things Notes
	 * Use propagation to specify the propagation behavior of things, that is, how to use things when the current thing method is called by another thing method. The default value is REQUIRED, that is, the thing that uses the calling method
	 * REQUIRES_NEW: Using own thing, the thing that called the thing method is suspended.
	 * isolation: Specify the isolation level of the transaction, the most commonly used value is: READ_COMMITTED, read to commit
	 * noRollbackFor: Do not roll back this exception. Usually, the default value can be taken.
	 * rollbackFor: rollback this exception
	 * readOnly: read-only transaction, can only read data, can help database engine optimization. If it is read-only, it should be set: readOnly=true
	 * timeout: Specify the time for forced rollback, in seconds. If the method executes for 5 seconds, and the property is set to 2 seconds, if it reaches 2 seconds, the method has not been executed, and the transaction will also execute the method. Do a forced rollback.
	 */
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED,
			noRollbackFor={UserException.class},noRollbackForClassName="UserException",
			rollbackFor={UserException.class},rollbackForClassName="UserException",
			readOnly=false,timeout=100)
	public void updatUser(){
		System.out.println("The method that needs to use the transaction");
	}

Usually, when using things, you don't need to set the properties of the @Transactional annotation, just write it directly on the method.


Second, based on the xml file

1. You only need to configure it in the Spring configuration file.

<!-- Configure Transaction Manager -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" /><!-- ref: import data source -->
	</bean>
	<!-- Configure transaction properties, the value of transaction-manager must be the same as the id of the bean above -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- Specify transaction attributes based on method name -->
			<tx:method name="updatUser"
				propagation="REQUIRED" isolation="READ_COMMITTED"
				no-rollback-for="UserException.class" read-only="false"
				rollback-for="UserException.class" timeout="5"/><!-- updatUser: configure the properties of things for this method -->
			<tx:method name="*"/><!-- Represents all methods, all things take the default configuration-->
			<tx:method name="get*" read-only="true"/><!-- Wildcards can be used, all methods that start with get, its transactions are only query-->
		</tx:attributes>
	</tx:advice>
	<!-- Configure transaction pointcuts and associate transaction pointcuts with transaction attributes -->
	<aop:config>
		<!-- Transaction entry point, execution entry point expression -->
		<aop:pointcut expression="execution(* com.qw.service.impl.*.*(..))" id="txPointcut"/>
		<!-- Use aop to associate transaction attributes with transaction pointcuts -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
	</aop:config>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324718100&siteId=291194637