Spring事務管理

事務的特性:
原子性:事務不可分割
一致性:事務執行先後數據完整性保持一致
隔離性:一個事務的執行不該該受到其餘事務的干擾
持久性:一旦事務結束,數據就持久到數據庫java

 

Spring的事務管理分兩類spring

編程式事務:本身寫代碼數據庫

申明式事務:(AOP原理)XML方式的聲明式事務、註解方式的聲明式事務express

 

下面的demo都是以轉帳爲例編程

下面是編程式事務的代碼微信

DAO層:ide

public interface AccountDao { public void outMoney(String from, Double money); public void inMoney(String to, Double money); }
import org.springframework.jdbc.core.support.JdbcDaoSupport; public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { @Override public void outMoney(String from, Double money) { this.getJdbcTemplate().update("update account set money = money - ? where name  = ?", money, from); } @Override public void inMoney(String to, Double money) { this.getJdbcTemplate().update("update account set money = money + ? where name  = ?", money, to); } }

服務層:測試

public interface AccountService { public void transfer(String from, String to, Double money); }
import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } //注入事務管理的模板
    private TransactionTemplate transactionTemplate; public void setTransactionTemplate(TransactionTemplate transactionTemplate) { this.transactionTemplate = transactionTemplate; } @Override public void transfer(String from, String to, Double money) { transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) { accountDao.outMoney(from, money); // int i = 1/0;
 accountDao.inMoney(to, money); } }); } }

配置文件tx.xmlthis

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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">

    <bean id="accountService" class="com.jinke.tx.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <!--注入事務管理的模板-->
        <property name="transactionTemplate" ref="transactionTemplate"/>
    </bean>

    <bean id="accountDao" class="com.jinke.tx.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置C3P0鏈接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--配置Spring的JDBC的模板-->
    <!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>-->

    <!--配置平臺事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置事務管理的模板-->
    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>
</beans>

測試類url

import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:tx.xml") public class demo { @Resource(name = "accountService") private AccountService accountService; @Test public void demo() { accountService.transfer("大傻", "dbcp", 1000d); } }

 

接下來是聲明式事務

XML方式

DAO層

public interface AccountDao { public void outMoney(String from, Double money); public void inMoney(String to, Double money); }
import org.springframework.jdbc.core.support.JdbcDaoSupport; public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { @Override public void outMoney(String from, Double money) { this.getJdbcTemplate().update("update account set money = money - ? where name  = ?", money, from); } @Override public void inMoney(String to, Double money) { this.getJdbcTemplate().update("update account set money = money + ? where name  = ?", money, to); } }

服務層

public interface AccountService { public void transfer(String from, String to, Double money); }
public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void transfer(String from, String to, Double money) { accountDao.outMoney(from, money); int i = 1 / 0; accountDao.inMoney(to, money); } }

配置文件tx2.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx-4.3.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.xsd">

    <bean id="accountService" class="com.jinke.tx2.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <bean id="accountDao" class="com.jinke.tx2.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置C3P0鏈接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--配置Spring的JDBC的模板-->
    <!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>-->

    <!--配置事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置事務的加強-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--事務管理的規則-->
            <!-- <tx:method name="save" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="find" read-only="true"/>-->
            <tx:method name="*" propagation="REQUIRED" read-only="false" timeout="-1"/>
        </tx:attributes>
    </tx:advice>

    <!--aop的配置-->
    <aop:config>
        <aop:pointcut id="pointcut1" expression="execution(* com.jinke.tx2.AccountServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
    </aop:config>

</beans>

IntelliJ自動生成的約束有點問題,須要本身修改

測試類

import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:tx2.xml") public class demo { @Resource(name = "accountService") private AccountService accountService; @Test public void demo() { accountService.transfer("大傻", "dbcp", 1000d); } }

 

註解方式

DAO層

public interface AccountDao { public void outMoney(String from, Double money); public void inMoney(String to, Double money); }
import org.springframework.jdbc.core.support.JdbcDaoSupport; public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { @Override public void outMoney(String from, Double money) { this.getJdbcTemplate().update("update account set money = money - ? where name  = ?", money, from); } @Override public void inMoney(String to, Double money) { this.getJdbcTemplate().update("update account set money = money + ? where name  = ?", money, to); } }

服務層

public interface AccountService { public void transfer(String from, String to, Double money); }

 

import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED) public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void transfer(String from, String to, Double money) { accountDao.outMoney(from, money); // int i = 1 / 0;
 accountDao.inMoney(to, money); } }

配置文件tx3.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="accountService" class="com.jinke.tx3.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <bean id="accountDao" class="com.jinke.tx3.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置C3P0鏈接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--配置事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--開啓註解事務-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

測試

import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:tx3.xml") public class demo { @Resource(name = "accountService") private AccountService accountService; @Test public void demo() { accountService.transfer("大傻", "dbcp", 1000d); } }

歡迎關注個人微信公衆號:安卓圈

相關文章
相關標籤/搜索