<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<!-- 第一个*表示不限制返回值 ..表示子包以及子包下所有的 *表示类 *表示方法括号中表示参数..为所有参数 -->
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.tlj..*.*(..))" />
</aop:config>
</beans>
主要说下切面和传播行为
切面execution(* cn.tlj..*.*(..)) 第一个*表示对于所有的返回值,第一个..表示该包以及该包下的所有类,第二个*表示类,第三个*表示方法(..)表示参数
传播行为的属性
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
">
<!-- 扫描注解 -->
<context:component-scan base-package="lesson04.testm"></context:component-scan>
<!-- 读取properties资源文件 -->
<context:property-placeholder location="classpath:/lesson04/jdbc/jdbcoracle.properties"/>
<!-- ${username}是个关键字 默认获取操作系统用户 -->
<!-- 连接数据库 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${url}"></property>
<property name="username" value="${username1}"></property>
<property name="password" value="${password}"></property>
<property name="driverClassName" value="${driverClass}"></property>
</bean>
<!-- jdbc的模板 可以执行sql语句 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 把连接数据库的bean注入 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理类 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 事务需要操作数据库 要把连接数据库的bean注入 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事物 定义通知 通知的代码 spring已经实现 -->
<!-- 如果上面的id名为 id="transactionManager" 下面的 transaction-manager="transactionManager"可以不写-->
<tx:advice id="myAdvise" transaction-manager="transactionManager">
<tx:attributes>
<!-- 设置事务的传播属性
propagation="REQUIRED" 方法和方法之间存在父子关系
REQUIRED 默认 父方法没有事务创建一个事务 有事务使用父方法当前事务
REQUIRES_NEW 不管父方法是否存在事务 都会新建事务
SUPPORTS 父方法存在事务 使用当前事务 没有事务 使用jdbc的事务(自动提交)
NOT_SUPPORTED 不管父方法中是否存在事务 都不会使用父方法中的事务(挂起事务)
MANDATORY 必须在事务环境中运行 父方法没有事务 抛出异常
No existing transaction found for transaction marked with propagation 'mandatory'
NEVER 父方法中不能存在事务 有事务就抛出异常
设置隔离事务属性
isolation="DEFAULT" 隔离级别
DEFAULT 使用数据库本身的隔离级别 ORACLE(读已提交) MYSQL(可重复读)
READ_UNCOMMITTED spring实现读未提交 (脏读)
READ_COMMITTED spring实现读已提交 (待解决--不可重复读+幻读)
REPEATABLE_READ spring实现可重复读 (待解决--幻读)
SERIALIZABLE spring实现串行化(已解决)
设置回滚事务属性 :spring事务 运行过程中 碰到运行时异常 自动回滚 非运行时异常不会回滚
rollback-for="" 指定会自动回滚的非运行时异常 设值成Exception就可以了 发生任何异常都回滚
no-rollback-for="" 指定某些运行时异常抛出时 不回滚
只读事务属性 (除特定的方法以外其他的业务逻辑方法 都不应该操作事务)
read-only="true"设置只读事务
超时事务属性
timeout=10-30左右 mysql默认10s自动超时 oracle永不超时
-->
<tx:method name="Update*" propagation="REQUIRED" rollback-for="Exception" isolation="DEFAULT" timeout="5"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 定义切点 -->
<aop:config>
<!-- 任意返回值 lesson04.testm.dao包下的所有类、所有方法拦截 -->
<aop:pointcut expression="execution(* lesson04.testm.dao.*.*(..))" id="myPoint"/>
<!-- 把切点和通知绑定 <tx:advice> -->
<aop:advisor advice-ref="myAdvise" pointcut-ref="myPoint"/>
</aop:config>
</beans>