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


How spring controls JDBC transactions

  • Through XML configuration
  • Implemented by annotation

Case practice

XML object configuration declaration

1) . modify XML namespace


2) . AOP agent

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

3) . configure transaction manager

<! — transaction manager definition — >
<bean id=”txManager”
<property name=”dataSource” ref=”dataSource”></property>

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
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: ‘ ,ServletException’
No roll back for is not required
Represents an exception (s) that is not triggered for rollback; separated by commas.
For example: ‘ ,ServletException’
Any runtimeException will trigger a transaction rollback
<tx:advice id=”txAdvice” transaction-manager=”txManager”>
<! — 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” />

Introduction to business communication behavior
If there is a transaction, add it. If not, create a new one (by default)
The container does not open transactions for this method
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
It must be executed in an existing transaction, otherwise an exception will be thrown
Must be executed in a transaction that does not exist, otherwise an exception (and Propagation.MANDATORY (opposite)
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:pointcut expression=”execution(*(..) )”id=”cut” />
<aop:advisor advice-ref=”txAdvice” pointcut-ref=”cut” />

Annotation method of spring transaction management

1) . configure transaction manager

<! — spring annotated transaction Declaration — >
<! — transaction manager definition — >
<bean id=”txManager”
<property name=”dataSource” ref=”dataSource”></property>

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

public void saveUser(String userName,String userPwd){
User user1=new User();

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


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


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 […]