Spring:事务

Spring事务管理

Spring针对不同的dao层框架,提供接口不同的事务实现类

因为在不同平台,操作事务的代码各不相同,Spring提供了一个接口,PlatformTransactionManager接口,接口的实现类有:

DataSourceTransactionManager

HibernateTransitionmanager

TransactionManager(核心)

准备

第一步:导包

 

第二步:引入约束,下面约束包括了Spring所有约束

<?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">
	
</beans>

基于XML配置实现事务管理

第一步:配置事务管理器

第二步:配置事务增强

<tx:advice id="txAdvice" transaction-manager="transactionManager">  
<tx:attributes>  
            <!-- 以方法为单位,指定方法应用什么事务属性 -->
            <!-- isolation:隔离级别 -->
            <!-- propagation:传播行为 -->
            <!-- read-only:是否只读 -->
            <tx:method name="save*"   propagation="REQUIRED" />
            <tx:method name="add*"    propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="del*"    propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="destroy*" propagation="REQUIRED" />
            <tx:method name="*" propagation="SUPPORTS" read-only="true" />  
</tx:attributes>  
</tx:advice>

Propagation_XXX,事务的传播行为

  • 保证同一个事务中

PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 

PROPAGATION_SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。 

PROPAGATION_MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。

  • 保证没有在同一个事务中  

PROPAGATION_REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。

PROPAGATION_NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 

PROPAGATION_NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。 

  •  

PROPAGATION_NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。

第三步:配置切面

需求:小王转账100给小马

  • 小王少100
  • 小马多100

dao

public class OrderDao {

	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public void lessMoney() {
		String sql = "update ord set money=money-? where id=?";
		jdbcTemplate.update(sql, 100, 1);
	}

	public void moreMoney() {
		String sql = "update ord set money=money+? where id=?";
		jdbcTemplate.update(sql, 100, 2);
	}
}

service

public class OrderService {
	private OrderDao orderDao;

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public void accountMoney() {
		orderDao.lessMoney();
		orderDao.moreMoney();
	}
}

applicationContext.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: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">

    
	<context:property-placeholder location="classpath:db.properties"/>
        
	<!-- 配置c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClass" value="${jdbc.driverClass}" ></property>
       <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
       <property name="user" value="${jdbc.user}" ></property>
       <property name="password" value="${jdbc.password}" ></property>
   </bean>
	
	<!-- 第一步 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入dataSource -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 第二步 配置事务增强 -->
	<tx:advice id="txadvice" transaction-manager="transactionManager">
		<!-- 做事务操作 -->
		<tx:attributes>
			<!-- 设置进行事务操作的方法匹配规则  -->
			<tx:method name="account*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 第三步 配置切面 -->
	<aop:config>
		<!-- 切入点 -->
		<aop:pointcut expression="execution(* com.feicui.pojo.OrderService.*(..))" id="pointcut1"/>
		<!-- 切面 -->
		<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
	</aop:config>
	
	<bean id="orderService" class="com.feicui.pojo.OrderService">
		<property name="orderDao" ref="orderDao"></property>
	</bean>
	<bean id="orderDao" class="com.feicui.pojo.OrderDao">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
</beans>

db.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///cc1
jdbc.user=root
jdbc.password=666

log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:\mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout

测试类

@Test
public void T1() {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    OrderService service = (OrderService) context.getBean("orderService");
    service.accountMoney();
}

基于注解实现事务管理

第一步:配置事务管理器

第二步:配置事务注解

applicationContext.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: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">

	<context:property-placeholder location="classpath:db.properties"/>
        
	<!-- 配置c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClass" value="${jdbc.driverClass}" ></property>
       <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
       <property name="user" value="${jdbc.user}" ></property>
       <property name="password" value="${jdbc.password}" ></property>
   </bean>
	
	<!-- 第一步 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入dataSource -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 第二步 配置事务注解 -->
	<!-- transaction-manager的值与事务管理器id值一致 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<bean id="orderService" class="com.feicui.pojo.OrderService">
		<property name="orderDao" ref="orderDao"></property>
	</bean>
	<bean id="orderDao" class="com.feicui.pojo.OrderDao">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>	
</beans>

第三步:在service类上面添加注解@Transactional

service

@Transactional
public class OrderService {
    ...
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值