spring事务学习(转账案例)(二)

时间:2021-06-14 06:13:23

四、通过springAop进行事务管理

继续从第一个无事务操作的项目中进行更改。

只修改applicationContext.xml配置文件,注意设置transaction引用

<?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: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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.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:///spring_transaction"></property>
<property name="user" value="root"></property>
<property name="password" value="hjp123"></property>
</bean>
<!--配置Dao-->
<bean id="accountDao" class="com.hujp.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置Service-->
<bean id="accountService" class="com.hujp.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--事务管理器要从数据源中获得数据库连接进行操作-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务详情
<tx:advice>配置事务详情
id:通知名称
transaction-manager:事务详情最后要应用到平台(管理器)
<tx:method>配置具体的详情
name详情名称,类似<prop key="">
如果是transfer表示指定方法
add* 是以add开头的方法
* 任何方法
propagation传播行为
isolation隔离级别
read-only是否只读
rollback-for类似-Exception回滚
no-rollback-for类似+Exception提交
例如工作中类似配置:增删改查及所有
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transfer" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--使用aop确定切入点
advice-ref通知确定事务详情
pointcut切入点表达式
-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.hujp.service..*.*(..))"></aop:advisor>
</aop:config>
</beans>

applicationContext.xml

五、spring注解式管理事务

事务管理一般是在service层进行管理,所以继续从第一个无事务操作的项目中更改两个文件,一个是applicationContext.xml配置文件,另一个是AccountServiceImpl类

注解中参数方式配置事务详情

<?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: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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.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:///spring_transaction"></property>
<property name="user" value="root"></property>
<property name="password" value="hjp123"></property>
</bean>
<!--配置Dao-->
<bean id="accountDao" class="com.hujp.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置Service-->
<bean id="accountService" class="com.hujp.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--将事务管理器交予spring,注解解析-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

applicationContext.xml

package com.hujp.service.impl;

import com.hujp.dao.AccountDao;
import com.hujp.service.AccountService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; /**
* Created by JiaPeng on 2015/11/4.
*/
//@Transactional//可应用在类上
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
} @Override
//@Transactional//也可应用在方法上
public void transfer(String outUser, String inUser, int money) {
this.accountDao.out(outUser, money);
//模拟断电
//int m=2/0;
this.accountDao.in(inUser, money);
}
}

AccountServiceImpl