一、使用TransactionTemplate配置事务:
<!--ioc扫描器--> <context:component-scan base-package="com.qf"></context:component-scan> <!--aop注解开启--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!--JdbcTemplate--> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${db.user}"></property> <property name="password" value="${db.password}"></property> <property name="driverClass" value="${db.driverClass}"></property> <property name="jdbcUrl" value="${db.jdbcUrl}"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务管理组件 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置事务管理--> <bean id="template" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager" /> </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="transfer" propagation="REQUIRED" /> <!-- SUPPORTS:如果有事务,则在事务中执行;如果没有事务,则不会开启事物 --> <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:pointcut id="txPointCut" expression="execution(* com.qf.service.AccountService.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config>
二、注解配置:
<!--ioc扫描器--> <context:component-scan base-package="com.qf"></context:component-scan> <!--aop注解开启--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!--JdbcTemplate--> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${db.user}"></property> <property name="password" value="${db.password}"></property> <property name="driverClass" value="${db.driverClass}"></property> <property name="jdbcUrl" value="${db.jdbcUrl}"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务管理组件 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置注解事务开启 对标注@Transactional注解的Bean进行加工处理,以织入事物管理切面 --> <tx:annotation-driven transaction-manager="transactionManager" />
注解:@Transactional:
1.使用注意点:
1)只能作用于public方法;
2)事务作用类下的线程不受事务影响;
解决:在线程中调用实现了事务的service方法;
2.事务隔离级别:
1)Transactional(isolation = Isolation.READ_UNCOMMITTED):读取未提交数据(会出现脏读, 不可重复读) 基本不使用;
2)Transactional(isolation = Isolation.READ_COMMITTED):读取已提交数据(会出现不可重复读和幻读);
3)Transactional(isolation = Isolation.REPEATABLE_READ):可重复读(会出现幻读);
4)Transactional(isolation = Isolation.SERIALIZABLE):串行化;
@Transactional属性:
属性 |
类型 |
描述 |
value |
String |
可选的限定描述符,指定使用的事务管理器 |
propagation |
enum: Propagation |
可选的事务传播行为设置 |
isolation |
enum: Isolation |
可选的事务隔离级别设置 |
readOnly |
boolean |
读写或只读事务,默认读写 |
timeout |
int (in seconds granularity) |
事务超时时间设置 |
rollbackFor |
Class对象数组,必须继承自Throwable |
导致事务回滚的异常类数组 |
rollbackForClassName |
类名数组,必须继承自Throwable |
导致事务回滚的异常类名字数组 |
noRollbackFor |
Class对象数组,必须继承自Throwable |
不会导致事务回滚的异常类数组 |
noRollbackForClassName |
类名数组,必须继承自Throwable |
不会导致事务回滚的异常类名字数组 |