08_Sping-JDBC操作-事务处理

jar包

mysql-connector-java-5.1.11.jar:MySQL驱动包
druid-1.0.15.jar
spring-jdbc-4.1.2.RELEASE.jar:支持JDBC
spring-tx-4.1.2.RELEASE.jar: 支持事务

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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
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/jee http://www.springframework.org/schema/jee/spring-jee.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
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
		
<!-- 在web环境下,一定要手动配置 -->
<context:annotation-config />

<context:property-placeholder location="classpath:db.properties"/>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
	<property name="driverClassName" value="${db.driver}"/>
	<property name="url" value="${db.url}"/>
	<property name="username" value="${db.username}"/>
	<property name="password" value="${db.password}"/>
</bean>


<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
</bean>

<tx:advice id="advice" transaction-manager="transactionManager">
	<tx:attributes>
		<tx:method name="list*" read-only="true"/>
		<tx:method name="get*" read-only="true"/>
		<tx:method name="query*" read-only="true"/>
		<tx:method name="*"/>
	</tx:attributes>
</tx:advice>

<aop:config>
	<aop:pointcut expression="execution(* com.haoxiaohuo.service.*Service.*(..))" id="pointCut"/>
	<aop:advisor advice-ref="advice" pointcut-ref="pointCut"/>
</aop:config>
</beans>

使用JdbcTemplate

Spring提供了一个名为的模板类JdbcTemplate,可以轻松使用SQL关系数据库和JDBC

Spring对事务的支持

Spring的事务管理主要包括3个接口:

TransactionDefinition:封装事务的隔离级别,超时时间,是否为只读事务和传播规则等事务属性,可通过XML配置具体信息。
PlatformTransactionManager:根据TransactionDefinition提供的事务属性配置信息,创建事务。
TransactionStatus:封装了事务的具体运行状态。比如,是否是新开启事务,是否已经提交事务,设置当前事务为rollback-only等。

Spring支持编程式事务管理和声明式事务管理。

编程式事务管理:事务和业务代码耦合度太高。
声明式事务管理:侵入性小,把事务从业务代码中抽离出来,提供维护性。

事务方法的属性细节

在这里插入图片描述

事务方法属性:

1,name:匹配到的方法模式,支持通配符,如save*,表示所有以save开头的方法;
2,read-only:如果为true,开启一个只读事务,只读事务的性能较高,但是不能再只读事务中,使用DML;查询操作就应该设置为只读事务.
3,isolation:代表数据库事务隔离级别(就使用默认)
DEFAULT:让spring使用数据库默认的事务隔离级别;
其他:spring模拟;
4,no-rollback-for: 如果遇到的异常是匹配的异常类型,就不回滚事务;
5,rollback-for:如果遇到的异常是指定匹配的异常类型,才回滚事务;
spring默认情况下,spring只会去回滚RuntimeException及其子类,不会回滚Exception和Thowable
6,propagation:事务的传播方式(当一个方法已经在一个开启的事务当中了,应该怎么处理自身的事务)
1,REQUIRED(默认的传播属性):如果当前方法运行在一个没有事务环境的情况下,则开启一个新的事务,如果当前方法运行在一个已经开启了的事务里面,把自己加入到开启的那个事务中 
2,REQUIRES_NEW:不管当前方法是否运行在一个事务空间之内,都要开启自己的事务

注解xml配置

<!-- 标签解析器 -->
<context:annotation-config />
<!-- 组件解析器 -->
<context:component-scan base-package="com.haoxiaohuo.pro"/>
<!-- Transactional解析器 -->
<tx:anotation-driver transaction-manager="transactionManager"/>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
</bean>

@Transactional 标签 调用事务

猜你喜欢

转载自blog.csdn.net/qq_33238150/article/details/82832959