首先,先大概介紹下事務:
1.1什麼是事務
事務邏輯上的一組操作,組成這組操作的各個邏輯單元,要麼一起成功,要麼一起失敗。
1.2事務特性
原子性 :強調事務的不可分割
一致性 :事務的執行前後數據完整性保持一致
隔離性 :一個事務執行的過程中 ,不應該受到其他事務的干擾
持久性 :事務一旦結束,數據就持久到庫
1.3如果不考慮隔離性引發安全 性問題:
髒讀 :一個事務讀到了另一個事務的未提交數據。
不可重複讀 :一個事務讀到了另一個事務已經提交的 update的數據導致多次查詢結果不一致。
幻讀 :一個事務讀到了另一個事務已經提交的 insert的數據,導致多次查詢結果不一致。
1.4解決讀問題:設置事務隔離級別
讀未提交:髒讀,不可重複讀,幻讀都有可能發生
讀已提交:避免髒讀,但是不可重複讀和幻讀有可能發生
可重複讀:避免髒讀、不可重複的,但是幻讀有可能發生
串行化:避免以上所有讀問題
Mysql 默認 :可重複讀
Oracle默認 :讀已提交
2.spring事務管理--xml配置aop事務(方法一)
2.1xml配置文件--applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 指定spring讀取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 事務核心管理器,封裝了所有事務操作. 依賴於連接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 事務模板對象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
<property name="transactionManager" ref="transactionManager" ></property>
</bean>
<!-- 配置事務通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<!-- 以方法爲單位,指定方法應用什麼事務屬性
isolation:隔離級別
propagation:傳播行爲
read-only:是否只讀
-->
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 配置織入 -->
<aop:config >
<!-- 配置切點表達式 -->
<aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="txPc"/>
<!-- 配置切面 : 通知+切點
advice-ref:通知的名稱
pointcut-ref:切點的名稱
-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>
<!-- 1.將連接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
<property name="driverClass" value="${jdbc.driverClass}" ></property>
<property name="user" value="${jdbc.user}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
<!-- 2.Dao-->
<bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl" >
<property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 3.Service-->
<bean name="accountService" class="cn.itcast.service.AccountServiceImpl" >
<property name="ad" ref="accountDao" ></property>
<property name="tt" ref="transactionTemplate" ></property>
</bean>
</beans>
指定spring讀取db.properties配置內容:
jdbc.driverClass=Classcom.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.jdbcUrl=jdbc:sqlserver://localhost:1433;databaseName=heimashop
jdbc.user=sa
jdbc.password=****
實例——基本的轉錢業務2.2domain層:(User)
package cn.itcast.domain;
public class User {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.3 dao層--轉賬數據數據庫操作
package cn.itcast.dao;
public interface AccountDao {
void decreaseMoney(Integer money,Integer id);
void increaseMoney(Integer money,Integer id);
}
package cn.itcast.dao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{
@Override
public void decreaseMoney(Integer money, Integer id) {
getJdbcTemplate().update("update t_account set money=money-? where id=?",money,id);
}
@Override
public void increaseMoney(Integer money, Integer id) {
getJdbcTemplate().update("update t_account set money=money+? where id=?",money,id);
}
}
2.4 service層業務—轉賬
package cn.itcast.service;
public interface AccountService {
void transfer( Integer from, Integer to ,Integer money);
}
import cn.itcast.dao.AccountDao;
public class AccountServiceImpl implements AccountService {
private AccountDao ad;
//需要注入
public void setAd(AccountDao ad) {
this.ad = ad;
}
@Override
public void transfer(Integer from, Integer to, Integer money) {
ad.decreaseMoney(money, from);
ad.increaseMoney(money, to);
}
}
2.5 測試
package cn.itcast.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.itcast.service.AccountService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {
@Resource(name="accountService")
private AccountService as;
@Test
public void fun1(){
as.transfer(1, 2, 100);
}
}
3.spring事務管理--aop事務註解配置(方法二)
3.1導包與前面的步驟一樣,此時需要在配置文件中開啓事務註解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 指定spring讀取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 事務核心管理器,封裝了所有事務操作. 依賴於連接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 事務模板對象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
<property name="transactionManager" ref="transactionManager" ></property>
</bean>
<!-- 開啓事務註解管理aop事務 -->
<tx:annotation-driven/>
<!-- 1.將連接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
<property name="driverClass" value="${jdbc.driverClass}" ></property>
<property name="user" value="${jdbc.user}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
<!-- 2.Dao-->
<bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl" >
<property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 3.Service-->
<bean name="accountService" class="cn.itcast.service.AccountServiceImpl" >
<property name="ad" ref="accountDao" ></property>
<property name="tt" ref="transactionTemplate" ></property>
</bean>
</beans>
3.2在service層,AccountServiceImpl中需要加入註解
package cn.itcast.service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.dao.AccountDao;
public class AccountServiceImpl implements AccountService {
private AccountDao ad;
//需要注入
public void setAd(AccountDao ad) {
this.ad = ad;
}
@Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)//加入事務註解
public void transfer(Integer from, Integer to, Integer money) {
ad.decreaseMoney(money, from);
ad.increaseMoney(money, to);
}
}
3.3測試
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.itcast.service.AccountService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class Demo1 {
@Resource(name="accountService")
private AccountService as;
@Test
public void fun1(){
as.transfer(1, 2, 100);
}
}
這就是spring aop事務配置的兩種方法,希望能幫助您!