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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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.alibaba.druid.pool.DruidDataSource">
		<property name="url" value="jdbc:mysql://localhost:3306/c30?characterEncoding=utf8"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>

	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 配置事务管理器,需要关联操作数据库的数据源 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<context:component-scan base-package="com.igeekhome.c30"></context:component-scan>
	
	<!-- 开启spring的事务支持的注解驱动,为了让@Transactional发挥作用 -->	
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
package com.igeekhome.c30.service;

import javax.security.auth.login.AccountException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.igeekhome.c30.dao.AccountDao;
import com.igeekhome.c30.dao.GoodDao;
import com.igeekhome.c30.exception.GoodException;

@Service
public class GoodServiceImpl implements GoodService {
	@Autowired
	private AccountDao accountDao;
	@Autowired
	private GoodDao goodDao;
	
	//@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
        //Propagation.REQUIRED 需要事物支持
        @Transactional
	@Override
	public void buy(int uid, int gid) throws Exception {
		double price = goodDao.getPriceById(gid);
		double money = accountDao.getMoneyById(uid);
		
		if(money<price){
			throw new AccountException("余额不足,购买失败");
		}
		
		accountDao.updateMoneyById(uid, price);
		int store = goodDao.getStoreById(gid);
		if(store <= 0){
			throw new GoodException("库存不足,购买失败");
		}
		goodDao.updateStoresById(gid);
	}

}

 

================================================================================ =

 配置版;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop" 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/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配置到spring的容器的 -->
	<!-- 使用的是jdbc所以使用DataSourceTransactionManager -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 事务管理器需要注入一个dataSource -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 第二步:定义通知,通知中要处理的就是事务 -->
	<!-- 配置事务的隔离性和传播特性 -->
	<!-- 相当于一个AOP的切面 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 配置事务的属性定义 -->
		<tx:attributes>
			<!-- 配置具体的方法的事务属性 
				isolation//事务的隔离级别,默认是按数据库的隔离级别来 
				propagation//事务的传播行为,默认是同一个事务 
				timeout="-1":事务的超时时间,默认值使用数据库的超时时间。 
				read-only="false":事务是否只读,默认可读写。 
				rollback-for:遇到哪些异常就回滚,其他的都不回滚 
				no-rollback-for:遇到哪些异常不回滚,其他的都回滚。和上面互斥的 -->
			<tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED"
				timeout="-1" read-only="false" />

			<!-- 支持通配符 要求service中 方法名字必须符合下面的规则 -->
			<tx:method name="save*" />
			<tx:method name="update*" />
			<tx:method name="delete*" />
			<tx:method name="insert*" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="select*" read-only="true" />
			<tx:method name="get*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 第三步:配置切入点,让通知关联切入点,即事务控制业务层的方法 -->
	<aop:config>
		<!-- 切入点 -->
		<aop:pointcut expression="bean(*Service)" id="txPointcut"/>
		<!-- 切面 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
	</aop:config>
	
</beans>

================================================================================ 

配置版2:

read-only="true" 只读数据 不需要事物支持,配置后针对此类操作更快

<?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-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.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">

	<!-- 要读取外部的配置文件可以直接使用 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<context:component-scan base-package="com.igeekhome.c30"></context:component-scan>
	
	<!-- 通过SpringEL获取配置文件中的参数的值 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="url" value="${jdbc_url}"></property>
		<property name="username" value="${jdbc_username}"></property>
		<property name="password" value="${jdbc_password}"></property>
	</bean>
	
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<tx:advice id="advice">
		<tx:attributes>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="read*" read-only="true"/> <!--read-only="true" 只读数据 不需要事物支持,配置后针对此类操作更快 -->
			<tx:method name="find*" read-only="true"/>
			<tx:method name="query*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置Spring的声明式事务 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.igeekhome.c30.service.*.*(..))" id="pointcut"/>
		<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
	</aop:config>
</beans>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_无往而不胜_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值