Spring1.x 的声明式事务使用TransactionProxyFactoryBean配置策略简单易懂,但是配置起来极为繁琐:每个目标Bean都需要额外配置一个TransactionProxyFactoryBean代理,这种方式将导致配置文件急剧增加。
Spring 2.x 的XML Schema方式提供了更简洁的事务配置策略,Spring2.x提供了tx命名空间来配置事务管理,tx命名空间下提供了<tx:advice.../> 元素来配置事务增强处理,一旦使用该元素配置了事务增强处理,就可直接使用<aop:advisor.../> 元素启用自动代理了。
NewsDao.java :
public interface NewsDao { public void insert(Integer id,String title,String content); }NewsDaoImpl.java :
import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; public class NewsDaoImpl implements NewsDao{ private DataSource ds; public void setDs(DataSource ds) { this.ds = ds; } @Override public void insert(Integer id, String title, String content) { JdbcTemplate jt=new JdbcTemplate(ds); jt.update("insert into news values(?,?,?)",new Object[]{id,title,content}); jt.update("insert into news values(?,?,?)",new Object[]{id,title,content}); } }bean.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 定义数据源Bean --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="user" value="scott"/> <property name="password" value="tiger"/> <property name="maxPoolSize" value="40"/> <property name="minPoolSize" value="1"/> <property name="initialPoolSize" value="1"/> <property name="maxIdleTime" value="20"/> </bean> <!-- 配置一个业务逻辑Bean --> <bean id="newsDao" class="com.bean.NewsDaoImpl"> <property name="ds" ref="dataSource"/> </bean> <!-- 配置JDBC数据源的局部事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务增强处理Bean,指定事务管理器 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 所有以'get'开头的方法是只读的 --> <tx:method name="get*" read-only="true"/> <!-- 其他方法使用默认的事务处理 --> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- AOP配置的元素 --> <aop:config> <aop:pointcut id="myPointcut" expression="execution(* com.bean.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/> </aop:config> </beans>Test.java :
public class Test { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml"); NewsDao dao=(NewsDao) ctx.getBean("newsDao"); dao.insert(1,"夺冠","绿衫军夺冠"); } }运行程序,控制台输出:
可见,事务已经自动启动了。两条记录是一个事务,第二条记录插入失败,导致第一条记录也被回滚。
配置 <tx:advice.../> 元素时只需指定一个transaction-manager属性,该属性的默认值是“transactionManager”。除了transaction-manager属性之外,还需要配置一个attributes子元素,该子元素里又可包含多个method子元素,每个<method.../>子元素为一批方法指定所需的事务语义,包括事务传播属性、事务隔离属性、事务超时属性、只读事务、对指定异常回滚,对指定异常不回滚等。
配置method子元素时可以指定如下几个属性:
属性 | 说明 |
name | 必选属性,与该事务语义关联的方法名。该属性支持使用通配符,例如get*,handle*等。 |
propagation | 指定事务传播行为,该属性值可为Propagation枚举类的任一枚举值,默认为Propagation_REQUIRED。 |
isolation | 指定事务隔离级别。该属性值可为Isolation枚举类的任一枚举值,默认为Isolation_DEFAULT。 |
timeout | 指定事务超时的时间(以秒为单位)。指定-1意味着不超时,默认值为-1。 |
read-only | 指定事务是否只读。默认为false。 |
rollback-for | 指定触发事务回滚的异常类,可指定多个异常类,以英文逗号隔开。 |
no-rollback-for | 指定不触发事务回滚的异常类,可指定多个异常类,以英文逗号隔开。 |