Spring框架之jdbc Template 以及事物

来源:互联网 发布:js实现鼠标特效 编辑:程序博客网 时间:2024/06/05 13:21
jdbcTemplate简介
作为Spring JDBC框架的核心,JDBC模板的设计目的是为不同类型的JDBC操作提供模板方法
每个模板方法都能控制整个过程,并允许覆盖过程中的特定任务,通过这种方式,可以在尽可能
保留灵活性的情况下,将数据库存取的工作量降到最低

导入jdbcTemplate jar包

ojdbc6 或ojdbc14皆可根据版本选择

JdbTemplate常用API

<!-- 配置C3P0数据源 -->
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean
id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:user="${jdbc.user}"
p:password="${jdbc.pwd}"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:initialPoolSize="${jdbc.initPoolSize}"
p:maxPoolSize="${jdbc.maxPoolSize}"/>

事物简介:
事物管理是企业级应用程序开发必不可少的技术,
用来确保数据的完整性和一致性

事物的四个关键属性(ACID)

Spring中的事务管理器
spring的核心事物管理是PaltformTransactionManager它为事物管理封装了一组独立于技术的方
法,无论使用spring的哪种事物管理策略(编程式或声明式),事物管理器都是必须的。

Spring中的事物管理器的不同实现


用事物通知声明式地管理事物实例代码

Sprng支持的事物传播行为

REQUIRES_NEW传播行为
当bookService的purchase方法被另一个事物方法checkout()调用时,它默认会在现有的事物内运行,这个默认的传播行为就是REQUIRED,因此checkout()方法的开始和终止边界内只有一个事物
这个事物只在checkout()方法结束的时候被提交,结果用户一本书都买不了

另一种常见的传播行为i是REQUIRES_NEW,它表示该方法必须启动一个新事物,并在自己的事物内运行,如果有事务在运行,就应该先挂起它

Spring支持事物隔离级别

事物的隔离级别要得到底册数据库引擎的支持,而不是应用程序或者框架的支持
oracle支持的2种事物隔离级别:READ_COMMITED,SERIALIZALE

Mysql支持4种事物隔离级别

在Spring事务通知中,可以在<tx:method>元素中指定隔离级别

设置回滚事物属性
默认情况下只有未检查异常(RuntimeException和Error类型的异常)会倒置事物回滚而受检查
异常不会
Spring事物通知中,可以在<tx:method>元素中指定回滚规则,如果有不止一种异常,用逗号分隔
rollbackFor:遇到时必须进行回滚
noRollbackFor:一组异常类,遇到时必须不回滚

超时和只读属性 如图:

applicationContext-book.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
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/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.jredu.book"></context:component-scan>
<!-- 配置C3P0数据源 -->
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean
id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:user="${jdbc.user}"
p:password="${jdbc.pwd}"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:initialPoolSize="${jdbc.initPoolSize}"
p:maxPoolSize="${jdbc.maxPoolSize}"/>
<!-- 配置事务管理器 -->
<bean
id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<!-- 事务的通知 -->
<tx:advice
id="bookTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="purchase*" propagation="REQUIRED"
isolation="READ_COMMITTED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution (* com.jredu.book.service.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="bookTxAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>