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 标签 调用事务
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值