Knowledge sharing: how to control JDBC transactions through spring framework?

Time:2021-3-4

How spring controls JDBC transactions

  • Through XML configuration
  • Implemented by annotation

Case practice

XML object configuration declaration

1) . modify XML namespace

xmlns:tx=”http://www.springframework.org/schema/tx”
http://www.springframework.or…
http://www.springframework.or…

2) . AOP agent

<aop:aspectj-autoproxy /><!– aop –>

3) . configure transaction manager

<! — transaction manager definition — >
<bean id=”txManager”
class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<property name=”dataSource” ref=”dataSource”></property>
</bean>

4) . configure transaction related notifications

Generally speaking, the method of adding, deleting and modifying propagation=RequiredFor query methods, use read only=“true”

Examples are as follows:

<! — configure notification — >
<!–
Property of TX: Method:

  • Name is required, which represents the method name (business method name) associated with the transaction attribute, and refines the entry point. Tongpei

The (*) character can be used to specify a batch of methods associated with the same transaction property.
For example: ‘get’‘、’handle‘on * event’ and so on
Propagation is not required. The default value is required
Represents transaction propagation behavior, including
REQUIRED,SUPPORTS,MANDATORY,REQUIRES_NEW,NOT_SUPPORTED,NEVER,NESTED
Isolation is not a required default value, default
Represents the transaction isolation level (the isolation level of the database)
Timeout is not a required default – 1 (never timeout)
Represents the time (in seconds) that the transaction timed out
Read only is not required. False is not read-only by default
Indicates whether the transaction is read-only
Roll back for is not required
Represents the exception (s) that will be triggered for rollback; separated by commas.
For example: ‘ com.foo.MyBusinessException ,ServletException’
No roll back for is not required
Represents an exception (s) that is not triggered for rollback; separated by commas.
For example: ‘ com.foo.MyBusinessException ,ServletException’
Any runtimeException will trigger a transaction rollback
–>
<tx:advice id=”txAdvice” transaction-manager=”txManager”>
<tx:attributes>
<! — all service methods starting with save are added to the transaction — >
<tx:method name=”save*” propagation=”REQUIRED” />
<! – all service methods starting with del are added to the transaction — >
<tx:method name=”del*” propagation=”REQUIRED” />
<! – all service methods starting with update are added to the transaction — >
<tx:method name=”update*” propagation=”REQUIRED” />
<! – match the service method starting with query to read-only mode — >
<tx:method name=”query*” read-only=”true” />
</tx:attributes>
</tx:advice>

Introduction to business communication behavior
@Transactional(propagation=Propagation.REQUIRED)
If there is a transaction, add it. If not, create a new one (by default)
@Transactional(propagation=Propagation.NOT_SUPPORTED)
The container does not open transactions for this method
@Transactional(propagation=Propagation.REQUIRES_NEW)
No matter whether there is a transaction or not, a new transaction is created. The original transaction is suspended, the new transaction is executed, and the old transaction continues to be executed
@Transactional(propagation=Propagation.MANDATORY)
It must be executed in an existing transaction, otherwise an exception will be thrown
@Transactional(propagation=Propagation.NEVER)
Must be executed in a transaction that does not exist, otherwise an exception (and Propagation.MANDATORY (opposite)
@Transactional(propagation=Propagation.SUPPORTS)
If other beans call this method and declare transactions in other beans, use transactions. If other beans do not declare transactions, do not use transactions

5) . configure AOP (pointcut, notification)

<! — AOP section definition — >
<aop:config>
<aop:pointcut expression=”execution( com.xxx.service...*(..) )”id=”cut” />
<aop:advisor advice-ref=”txAdvice” pointcut-ref=”cut” />
</aop:config>

Annotation method of spring transaction management

1) . configure transaction manager

<! — spring annotated transaction Declaration — >
<! — transaction manager definition — >
<bean id=”txManager”
class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<property name=”dataSource” ref=”dataSource”></property>
</bean>

2) . configure annotation support

<tx:annotation-driven transaction-manager=”txManager”/>

3) On the. Service method, add annotation to the method that needs to add transaction

@Override
@Transactional(propagation=Propagation.REQUIRED)
public void saveUser(String userName,String userPwd){
User user1=new User();
user1.setUserName(userName);
user1.setUserPwd(userPwd);
userDao.saveUser(user1);
userDao.delUserById(2);
}

Note: by default, spring transactions are rolled back only when an uncapped runtimeexcetpion occurs. Spring AOP exception capture principle: the intercepted method needs to throw an exception explicitly and can’t go through any processing, so that the AOP agent can catch the exception of the method and roll back. By default, AOP only catches the exception of runtimeException, but it can catch specific exception and roll back through configuration. In other words, try catch is not used in the service method or in the Finally, throw new runtimeexcetpion () is added in catch, so that the program can be caught by AOP and rolled back when it is abnormal

extend

Four characteristics of transaction (namely: acid)

Atomicity: the smallest unit of business control, the smallest unit of concurrency control, [indivisible]

Consistency: the data in the database should be consistent before and after the transaction is executed. (for example, in the process of transfer, the data must be consistent after account operation) — > ensure the integrity (correctness) of the data

Isolation: the execution between transactions should be isolated from each other. (multiple roles must operate the unified record without any interference). Of course, it is impossible to have no impact. In order to minimize the impact level, the isolation level is used to limit it

  • READ_UNCOMMITTED
  • READ_COMMITTED
  • REPEATABLE_READ
  • SERIALIZABLE

Durability: after the transaction is committed, the change of data in the database is permanent

Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]