Spring declarative transactions

In the commonly used SSH framework, Spring often configures two transaction managers, HibernateTemplate and JdbcTemplate.

1. Configure the data source; inject Dao and transaction manager
sessionFactory inject DAO
DAO inject
the proxy object of Service Service inject
the implementation class of action Service, which is service A field in the proxy object.

Declarative transaction composition: The transaction manager injects transaction notifications, and transaction notifications and aspects are combined to form a declarative transaction.


------------The following is excerpted from Baidu Library -----------------------
Spring declarative transaction summary

Beginners learn spring, note summary:
1 : For a certain class, you can use the proxy class to manage transactions:
<!-- The following is a method for declarative transaction management: proxy class -->

<!--

Description, manage the transaction by setting the proxy class, to specify what the target class is, set the proxy target class to true
 
(1) The agent class manages transactions, which requires aopalliance.jar and cglib*.jar
 

(2) The proxy class can be set on the DAO class or on the class of the service layer, depending on the requirements

(3) After the proxy class is set, it is a subclass of the target class. When calling the bean of the target class, you need to call the bean of the proxy class

, see this program for understanding

For example: if the proxy class is a proxy for the DAO class, the ref of the attribute messageDao of messageSvc should be messageDaoProxy

 (4) In transaction management, if a runtime exception occurs: RuntimeException() throws an exception, rolls back, and does not write data.

Database, if a common exception occurs, Exception() will throw an exception, will not roll back, and still write to the database

-->

<bean id="messageSvcProxy" class="org.springframework.transaction.interceptor.TransactionPro

xyFactoryBean">

<property name="transactionManager" ref="transactionManager"></property>

<property name="target">

  <ref local="messageSvc" />

</property>
<property name="proxyTargetClass" value="true"></property>

<property name="transactionAttributes">
<props>
   <prop key="do*">PROPAGATION_REQUIRED</prop>
   <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

2. For multiple classes, AOP can be used to manage transactions

<!--

Another method of declarative transaction management is used below: AOP method, the above method can only manage one class, this method

Can manage transactions in batches

Note: You need to add two jar packages under the aspectJ folder

 -->

<!--

Configure the propagation characteristics of transactions

 -->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

 <tx:attributes>

  <tx:method name="do*" propagation="REQUIRED" />

  <tx:method name="*" read-only="true" />

 </tx:attributes>

</tx:advice>

<!--
Which methods of which classes participate in transactions
 -->

<aop:config>

 <aop:pointcut id="allManagerMethod"
    expression="execution(* com.guo.dao.*.*(..))" />

<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />

</aop:config>

3. The third method of declarative transaction management: interceptors
<!--
The third approach to declarative transaction management: interceptors
 -->

<bean id="transactionInterceptor"  class="org.springframework.transaction.interceptor.TransactionInterceptor">

  <property name="transactionManager" ref="transactionManager" />
<!--
Configure Transaction Properties
 -->
<property name="transactionAttributes">

<props>

    <prop key="do*">PROPAGATION_REQUIRED</prop>
   <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
 </props>
</property>

</bean>

<bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

<property name="beanNames">

<list>
<value>*Dao</value>
</list>
</property>      

<property name="interceptorNames">

<list>
 <value>transactionInterceptor</value>
</list>
</property>

</bean>
    

 

In addition, there are:

annotation

full label method, see another

word

file, or website:



http://lvqionghua.blog.163.com/blog/static/1852774201152234023736

/

 

4.

Expression description

 

* com.guo.dao.*. *(..)

: the first

*

means any return value, the second

*

means any class, and the third

*

means

any method: see description below except for the return type pattern ( ret-type-pattern

 

in the code snippet above ), name pattern and parameter pattern, all parts are optional . The return type pattern determines that the return type of the method must in turn match a join point. The most frequent return type is * , which means match any return type. A fully qualified type name will only match methods that return the given type. The name pattern matches the method name . You can use the * wildcard for all or part of the naming pattern .

















































The parameter pattern is a little more complicated

:

( ) matches



a method that takes no parameters, and (..) matches a method that takes any number of parameters (zero or more). The pattern (*) matches a method that accepts an argument of any type. The pattern (*,String) matches a method , the first of which can be of any type and the second of which must be of type String . Some examples of common pointcut expressions are given below. Execution of any public method: execution(public * *(..))   Execution of any method starting with "set" : execution(* set*(..))   Execution of any method of the AccountService interface: execution (* com.xyz.service.AccountService.*(..))   Execution of any method defined in the service package:



















































 







 













 













 













 

execution(* com.xyz.service.*.*(..)) 

 Execution of any method



defined in the

service

package or subpackage:

 

execution(* com.xyz.service..*.*(..)) 



 

5.

Note:

 

Runtime exceptions will be rolled back, common exceptions will not be rolled back, and can still be written to the database



Guess you like

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