8.6.3 使用@Transactional

8.6.3 使用@Transactional

Spring还允许将事务配置放在Java类中定义,这需要借助于@Transactional注解,@Transactional注解既可用于修饰Spring Bean类,也可用于修饰Bean类中的某个方法。

  • 如果使用@Transactional修饰Bean类,则表明这些事务设置对整个Bean类起作用;
  • 如果使用@Transactional修饰Bean类的某个方法,则表明这些事务设置只对该方法有效。

使用@Transactional时可指定如下属性。

属性 描述
isolation 用于指定事务的隔离级别。默认为底层事务的隔离级别。
noRollbackFor 指定遇到特定异常时强制不回滚事务。
noRollbackForClassName 指定遇到特定的多个异常时强制不回滚事务。该属性值可以指定多个异常类名
propagation 指定事务传播行为
readOnly 指定事务是否只读
rollbackFor 指定遇到特定异常时强制回滚事务。
rollbackForClassName 指定遇到特定的多个异常时强制回滚事务。该属性值可以指定多个异常类名。
timeout 指定事务的超时时长。

根据上面的解释不难看出,其实该注解所指定的属性与<tx:advice.>元素中所指定的事务属性基本上是对应的,它们的意义也基本相似。

程序示例

1
2
3
4
5
6
7
8
9
10
11
12
13
E:\workspace_QingLiangJiJavaEEQiYeYingYongShiZhang5\Transactional
├─data.sql
└─src\
├─beans.xml
├─lee\
│ └─SpringTest.java
└─org\
└─crazyit\
└─app\
└─dao\
├─impl\
│ └─NewsDaoImpl.java
└─NewsDao.java

下面使用@Transactional修饰需要添加事务的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
public class NewsDaoImpl implements NewsDao
{
private DataSource ds;
public void setDs(DataSource ds)
{
this.ds = ds;
}
@Transactional(
propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT,
timeout = 5)
public void insert(String title, String content)
{
JdbcTemplate jt = new JdbcTemplate(ds);
jt.update("insert into news_inf" + " values(null , ? , ?)", title,
content);
// 两次插入的数据违反唯一键约束
jt.update("insert into news_inf" + " values(null , ? , ?)", title,
content);
// 如果没有事务控制,则第一条记录可以被插入
// 如果增加事务控制,将发现第一条记录也插不进去。
}
}

上面Bean类中的insert()方法使用了 @Transactional修饰,表明该方法具有事务性。仅使用这个注解修饰还不够,还需要让Spring根据注解来配置事务代理,所以还需要在Spring配置文件中增加如下配置片段。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 定义数据源Bean,使用C3P0数据源实现,并注入数据源的必要信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://localhost/spring?useSSL=true"
p:user="root"
p:password="root"
p:maxPoolSize="40"
p:minPoolSize="2"
p:initialPoolSize="2"
p:maxIdleTime="30"/>

<!-- 配置一个业务逻辑Bean -->
<bean id="newsDao" class="org.crazyit.app.dao.impl.NewsDaoImpl"
p:ds-ref="dataSource"/>

<!-- 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager 类 -->
<!-- 该类实现PlatformTransactionManager接口,是针对采用数据源连接的特定实现-->
<!-- 配置DataSourceTransactionManager时需要依注入DataSource的引用 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<!-- 根据Annotation来生成事务代理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>