Spring transaction management - only records xml part

1. manually configure the way the transaction is

applicationContext.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans   xmlns="http://www.springframework.org/schema/beans" 
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xsi:schemaLocation="http://www.springframework.org/schema/beans 
                              http://www.springframework.org/schema/beans/spring-beans.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context.xsd
                              http://www.springframework.org/schema/aop
                              http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
        
        
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
            <property name="user" value="root"></property>
            <property name="password" value="123456"></property>
        
        </bean>
     
         <bean id="accountDao" class="com.test.spring.aop.tansation.example.dao.AccountDaoImpl">
             <property name="dataSource" ref="dataSource"></property>
         </bean>
         
         <bean id="accountService" class="com.test.spring.aop.tansation.example.service.AccountServiceImpl">
             <property name="accountDao" ref="accountDao"></property>
             <property name="transactionTemplate" ref="transationTemplate"></property>
         </bean>
         
         <!-- 手动方式 -->
         <bean id="transationTemplate" class="org.springframework.transaction.support.TransactionTemplate">
             <property name="transactionManager" ref="txManage"></property>
         </bean>
         
         <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <property name="dataSource" ref="dataSource"></property>
         </bean>
</beans>

 

dao layer (interface class are not written here)

package com.test.spring.aop.tansation.example.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

    public void in(String inner, int money) {
        this.getJdbcTemplate().update("update account set money = money -? where username = ?", money, inner);

    }

    public void out(String outter, int money) {
        this.getJdbcTemplate().update("update account set money = money +? where username = ?", money, outter);
    }

}

 

service layer

package com.test.spring.aop.tansation.example.service;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import com.test.spring.aop.tansation.example.dao.AccountDao;

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;
    private TransactionTemplate transactionTemplate;
    
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }
    
    
    public void tansfer(final String inner, final String outter, final int money) {
        
        //手动方式配置
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                accountDao.out(outter, money);
          // When an exception is encountered, the transaction rollback
int I = 1/0 ; accountDao.in (Inner, Money); } }); } }

 

test

package com.test.spring.aop.tansation.example;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.spring.aop.tansation.example.service.AccountService;

public class TestExample {

    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("com/test/spring/aop/tansation/example/applicationContext.xml");
    
        AccountService as = ctx.getBean("accountService",AccountService.class);
        
        as.tansfer("jack", "peter", 200);
    }
}

 

2. Semi-automatic, using a proxy way

Here only you need to modify the service layer and configure it

 

applicationContext.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans   xmlns="http://www.springframework.org/schema/beans" 
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xsi:schemaLocation="http://www.springframework.org/schema/beans 
                              http://www.springframework.org/schema/beans/spring-beans.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context.xsd
                              http://www.springframework.org/schema/aop
                              http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
        
        
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
            <property name="user" value="root"></property>
            <property name="password" value="123456"></property>
        
        </bean>
     
         <bean id="accountDao" class="com.test.spring.aop.tansation.example.dao.AccountDaoImpl">
             <property name="dataSource" ref="dataSource"></property>
         </bean>
         
         <bean id="accountService" class= "com.test.spring.aop.tansation.example.service.AccountServiceImpl" > 
             < Property name = "AccountDao" REF = "AccountDao" > </ Property > 
             < Property name = "TransactionTemplate" REF = "transationTemplateProxy" > < / property > 
         </ the bean > 
      
         
         <! - semiautomatic -> 
         <! - Acting factory class 
                 parameter 1: Interface 
                 parameter 2: implementation class 
                 parameter 3: transaction Manager 
                 parameter 4: transaction attributes (transaction details) 
                     prop.key: confirm those using transactions
                     prop.value: Transaction details specific parameter information can be found TransactionDefinition class 
                 example 3: when faced with specified exceptions, submitted to still commit the transaction, that the execution will be submitted before the 
                         format: Propagation,Isolation,readOnly,-Exception,+Exception
                         Propagation: propagation behavior 
                         Isolation: isolation level 
                         readOnly: read-only connection is 
                         -Exception: When an exception is encountered, the rollback 
                         + Exception: when an exception is encountered, the author 
                 Example 1: isolation and propagation behavior default level 
                     <prop key = "transfer "> PROPAGATION_REQUIRED, ISOLATION_DEFAULT </ prop > 
                 example 2: read-only, read-only connector, an update operation can not be performed 
                     <prop Key =" Transfer "> of PROPAGATION_REQUIRED, ISOLATION_DEFAULT, readOnly </ prop> 
                     <prop Key =" Transfer "> of PROPAGATION_REQUIRED, ISOLATION_DEFAULT, java.lang.ArithmeticException + </ prop> 
          -> 
         < the bean ID = "transationTemplateProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
             <property name="proxyInterfaces" value="com.test.spring.aop.tansation.example.service.AccountService"></property>
             <property name="target" ref="accountService"></property>
             <property name="transactionManager" ref="txManage"></property>
             <property name="transactionAttributes">
                 <props>
                     <prop key="transfer"></prop>
                 </props>
             </property>
         </bean>
         
         <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <property name="dataSource" ref="dataSource"></property>
         
         </bean>
</beans>

 

service layer

package com.test.spring.aop.tansation.example.service;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import com.test.spring.aop.tansation.example.dao.AccountDao;

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;
    private TransactionTemplate transactionTemplate;
    
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }
    
    
    public void tansfer(final String inner, final String outter, final int money) {
        
        accountDao.out(outter, money);
        int i = 1/0;
        accountDao.in(inner, money);

    }

}

 

3. Using an automated way spring

The long list above configuration, will be very troublesome, now using automated, eliminate the inconvenience above configuration, in addition to configuration changes, the others do not need to be modified

<?xml version="1.0" encoding="UTF-8" ?>
<beans   xmlns="http://www.springframework.org/schema/beans" 
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         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/context
                              http://www.springframework.org/schema/context/spring-context.xsd
                              http://www.springframework.org/schema/aop
                              http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
                              http://www.springframework.org/schema/tx
                              http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
        
        
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
            <property name="user" value="root"></property>
            <property name="password" value="123456"></property>
        
        </bean>
     
         <bean id="accountDao" class="com.test.spring.aop.tansation.example.dao.AccountDaoImpl">
             <property name="dataSource" ref="dataSource"></property>
         </bean>
         
         <bean id="accountService" class="com.test.spring.aop.tansation.example.service.AccountServiceImpl">
             <property name="accountDao" ref="accountDao"></property>
             <!-- <property name="transactionTemplate" ref="transationTemplate"></property> -->
             <property name="transactionTemplate" ref="transationTemplateProxy"></property>
         </bean>

         Automatic<! -
             这里分为3块,Specific process is to first have a transaction manager, and then there is a transaction management notice, the last section of a aop configuration 
          ->
             of the section configuration entry point for transaction management, notification management when faced with matters configuration consistent class when the name, a configuration with consistent behavior, such as read-only, or to submit an exception, etc.
         <! - 1. Transaction Manager -> 
         < the bean ID = "txManage" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > 
             < Property name = "the dataSource" REF = "the dataSource" > </ Property > 
         </ bean > 
         <-! 2. affairs notify 
             tx: method is to specify that method requires the use of a transaction, and details of the transaction by the decision of several properties inside class 
         -> 
         < tx: advice the above mentioned id = "txAdvice" transaction -manager = "txManage" > 
             < TX: Attributes > 
                 <tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
             </tx:attributes>
         </tx:advice>
         <!-- 3.aop切面类 -->
         <aop:config>
             <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.test.spring.aop.tansation.example.service.AccountServiceImpl.*(..))"/>
         </aop:config>
         
</beans>

 

Guess you like

Origin www.cnblogs.com/oscar1987121/p/10960570.html