<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 开启注解处理器 --> <context:annotation-config/> <!-- 定义使用C3P0连接池的数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 指定连接数据库的JDBC驱动 --> <property name="driverClass"> <value>com.mysql.jdbc.Driver</value> </property> <!-- 连接数据库所用的URL --> <property name="jdbcUrl"> <value>jdbc:mysql://localhost:3306/db_user?useUnicode=true&characterEncoding=gbk</value> </property> <!-- 连接数据库的用户名 --> <property name="user"> <value>root</value> </property> <!-- 连接数据库的密码 --> <property name="password"> <value>111111</value> </property> <!-- 设置数据库连接池的最大连接数 --> <property name="maxPoolSize"> <value>20</value> </property> <!-- 设置数据库连接池的最小连接数 --> <property name="minPoolSize"> <value>2</value> </property> <!-- 设置数据库连接池的初始化连接数 --> <property name="initialPoolSize"> <value>2</value> </property> <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 --> <property name="maxIdleTime"> <value>20</value> </property> </bean> <!-- 注册一个JDBC数据源事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 基于AOP技术的事务管理实现 --> <aop:config> <!-- 定义一个事务切入点,拦截test.spring.dao.impl.UserDaoImpl中的所有方法 --> <aop:pointcut id="transactionPointcut" expression="execution(* test.spring.dao.impl.UserDaoImpl.*(..))"/> <!-- 引用txAdvice事务通知 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/> </aop:config> <!-- 定义一个事务通知txAdvice --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 所有以load开头的方法声明为不需要事务 --> <tx:method name="load*" read-only="true" propagation="NOT_SUPPORTED"/> <!-- 其它所有方法声明为默认的REQUIRED类型的事务传播方式 --> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 注册一个JdbcTemplate实例名称为jdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="dataSource"/> </bean> <!-- 注册一个UserDaoImpl实例名称为dao --> <bean id="dao" class="test.spring.dao.impl.UserDaoImpl"/> <!-- 注册一个UserServiceImpl实例名称为service --> <bean id="service" class="test.spring.service.impl.UserServiceImpl"/> <!-- 注册一个UserAction实例名称为userAction --> <bean id="userAction" class="test.spring.action.UserAction"/> </beans>
直接看代码
参考主题:关于spring声明式事务管理异常处理的测试和小结 http://www.iteye.com/topic/34867
<!--
mysql 默认引擎不支持事务,引擎为:MyISAM
修改成:InnoDB
mysql默认的数据库引擎是MyISAM,不支持事务和外键,也可使用支持事务和外键的InnoDB。
查看当前数据库的所支持的数据库引擎以及默认数据库引擎
show engines;更改方式1:修改配置文件my.ini
我将my-small.ini另存为my.ini,在[mysqld]最后添加为上default-storage-engine=InnoDB,重启服务,数据库默认的引擎修改为InnoDB
更改方式2:在建表的时候指定或者建完表修改
--建表的时候指定
create table mytbl(
id int primary key,
name varchar(50)
)type=MyISAM;
--建完表后修改
alter table mytbl2 type = InnoDB;
--查看修改结果(mytest为表所在的database名字)
show table status from mytest;
-->
Spring的事务管理默认只对出现运行期异常(java.lang.RuntimeException及其子类)进行回滚。
如果一个方法抛出Exception或者Checked异常,Spring事务管理默认不进行回滚。
二、改变默认方式
在@Transaction注解中定义noRollbackFor和RollbackFor指定某种异常是否回滚。
@Transaction(noRollbackFor=RuntimeException.class)
@Transaction(RollbackFor=Exception.class)
这样就改变了默认的事务处理方式。
三、启示
这就要求我们在自定义异常的时候,让自定义的异常继承自RuntimeException,这样抛出的时候才会被Spring默认的事务处理准确处理。
关于异常的分类一下详细介绍: