spring-事务管理

151 阅读3分钟

说明

  1. 编程式事务:需要程序员自己写commit,rollback
  2. 声明式事务:spring已经通过某些类给我们封装事务的处理,只需要拿来用
  3. 如果要使用声明事务,只需要在spring的xml文件中进行配置就行

步骤

  1. 配置主配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- beans spring配置文件的根标签 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    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">
	<!-- 1.先配置连接池:spring-jdbc基于连接池技术实现连接数据库 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3308/mybatis?useUnicode=true&amp;characterEncoding=UTF-8"></property>
		<property name="user" value="root"></property>
		<property name="password" value="123456"></property>
		<property name="initialPoolSize" value="5"></property>
		<property name="maxPoolSize" value="10"></property>
		<property name="maxStatements" value="20"></property>
	</bean>
	<!-- 2.创建JdbcTemplate -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 3.dao -->
	<bean id="dao" class="com.dyr.dao.StudentImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>
	<!-- 4.service -->
	<bean id="service" class="com.dyr.service.StudentService">
		<property name="dao" ref="dao"></property>
	</bean>
	<!-- **************事务管理配置************* -->
	<!-- 1.配置事务管理器:管理事务,它自己知道什么时候开启事务,什么时候提交,什么时候回滚 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 管理哪个数据库 -->
		<property name="dataSource"	ref="dataSource"></property>
	</bean>
		<!-- 2.配置哪些方法需要添加事务管理 -->
		<aop:config >
			<aop:pointcut expression="execution(* com.dyr.service.StudentService.*(..))
			or execution(* com.dyr.dao.*.*(..))
			" id="pt"/>
			<!-- 配置通知 
			-->
			<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
		</aop:config>
	<!-- 3.配置事务的通知 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<!-- 指定哪些方法需要配置声明式事务 -->
		<tx:attributes>
			<tx:method name="insert*"/>
			<!-- <tx:method name="all*" read-only="true"/> -->
			<!-- <tx:method name="insert*" propagation="REQUIRED"/> --><!-- 事务传播机制 -->
			<!-- 
			<tx:method name="insert*" propagation="REQUIRES_NEW"/> 事务传播特性 -->
			 
		</tx:attributes>
	</tx:advice>
	
	
	<!-- 通过aop:config找到哪些方法需要添加事务(),然后通过txadvice利用txManager给
		对应的方法添加事务
	 -->
	
</beans>

注解方式实现

  1. 配置
<!-- **************事务管理配置************* -->
	<!-- 1.配置事务管理器:管理事务,它自己知道什么时候开启事务,什么时候提交,什么时候回滚 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 管理哪个数据库 -->
		<property name="dataSource"	ref="dataSource"></property>
	</bean>
	<!-- 开启注解扫描 component、service、controller,将类加入IOC容器-->
	<context:component-scan base-package="com.dyr.annotation"></context:component-scan>
	<!-- 开启事务注解 -->
	<tx:annotation-driven transaction-manager="txManager"/>

不用配置管理哪些方法和配置事务的通知,但要开起注解扫描,并且开启事务注解

  1. 其他注解与之前相同,这时对要管理的方法:添加注解
@Override
	@Transactional(
			readOnly=true,
			propagation=Propagation.REQUIRED,
			isolation=Isolation.DEFAULT
			)
	public List<Student> all() {
		
		return dao.all();
	}

	@Override
	@Transactional(
			propagation=Propagation.REQUIRED,
			isolation=Isolation.DEFAULT)
	public void insert(Student student) {
		dao.insert(student);
		System.out.println(1/0);
		dao.insert(student);
	}