您的位置:首页 > 编程语言 > Java开发

Spring v3.0.2 Learning Note 10 - Annotation-based Transaction Management

2010-10-25 11:14 399 查看
基于注解的事务管理

配置数据源:

<?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: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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
// 占位符的设置

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

<property name="driverClassName" value="${driverClassName}" />

<property name="url" value="${url}" />

<property name="username" value="${username}" />

<property name="password" value="${password}" />

<!-- 连接池启动时的初始值 -->

<property name="initialSize" value="${initialSize}" />

<!-- 连接池的最大值 -->

<property name="maxActive" value="${maxActive}" />

<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->

<property name="maxIdle" value="${maxIdle}" />

<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->

<property name="minIdle" value="${minIdle}" />

</bean>

<bean id="txManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

<tx:annotation-driven transaction-manager="txManager" />

<bean id="personService" class="com.spring.test.manager.impl.PersonManager">

<property name="dataSource" ref="dataSource" />

</bean>

</beans>

jdbc.properties内容:
driverClassName=org.gjt.mm.mysql.Driver

url=jdbc/:mysql/://localhost/:3306/myproj?useUnicode/=true&
characterEncoding/=UTF-8

username=root

password=123456

initialSize=1

maxActive=500

maxIdle=2

minIdle=1
注意,如果直接在spring的XML配置文件中配置URL,其写法为(注意&的写法)
<property name="url" value="jdbc:mysql://localhost:3306/myproj?useUnicode=true&
characterEncoding=UTF-8"/>

配置事务,需要在xml配置文件中引入声明事务的tx命名空间,支持注解的方式和XML配置方式。

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
注解方式用法

<bean id="txManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

<!-- 指定采用@Transactional 注解的方式使用事务 -->

<tx:annotation-driven transaction-manager="txManager" />

注意:@Transactional只能用于public方法上。如果用在private、protected,系统不会抱错,但是配置的事务设置将失效。如果一定要在非public方法上用这个注解,需要引入AspectJ.

基本用法

@Service @Transactional

public class PersonDao implements IPersonDao {

//.......

}

Spring事务管理的传播属性

REQUESTED:如果方法运行时,已经处在一个事务中,那么加入到该事务中来,否则为自己创建一个新事务。

NOT_SUPPORTED:声明方法不需要事务。如果方法没有关联到一个事务,容器不会为它开启事务。如果方法在一个事务中被调用,该事物会被挂起,在方法调用结束后,原先的事务恢复运行。

REQUEST_NEW:不管是否存在事务,业务方法总会为自己发起一个新的事务。如果方法运行在一个事务中,则原有事务挂起,新的事务创建,直到方法执行结束,新事务才算结束,原先的事务才会恢复执行。

MANDATORY:指定业务方法只能在一个已经存在的事务中执行,业务方法不能发起自己的事务。如果业务方法在没有事务的环境下调用,容器就会抛出异常。

SUPPORTS:如果业务方法在某个事务范围内被调用,则方法称为该事物的一部分。如果业务方法在事务范围外被调用,则方法在没有事务的环境下执行。

NEVER:指定业务方法不能再事务范围内执行。如果业务方法在某个事务中执行,容器会抛出例外,如果业务方法没有关联到任何事务,才能正常执行。

NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中,如果没有活动事务,则按REQUESTED属性执行。它使用了一个独立的事务,这个事务拥有多个可以回滚的保存点。内部事务的回滚不会影响到外部事务。

Spring容器在默认状态下,碰到unchecked-exception会回滚,checked-exception则不会


public void delete(Integer personid) {

jdbcTemplate.update("delete from person where id=?", new Object[]{personid},

new int[]{java.sql.Types.INTEGER});

throw new RuntimeException("This is unchecked-exception.");

}

运行此方法,默认状态下,数据库中的数据不会删除,spring容器会回滚事务。

public void delete(Integer personid) throws Exception {

jdbcTemplate.update("delete from person where id=?", new Object[]{personid},

new int[]{java.sql.Types.INTEGER});

throw new Exception("This is checked-exception.");

}

运行此方法,默认状态下,数据库中的数据会被删除,spring容器不会回滚事务。

修改默认情况,指定事务中碰到某个checked-exception,spring容器会回滚事务。

@Transactional(rollbackFor=Exception.class)
public void delete(Integer personid) throws Exception {

jdbcTemplate.update("delete from person where id=?", new Object[]{personid},

new int[]{java.sql.Types.INTEGER});

throw new Exception("This is checked-exception.");

}运行此方法,spring容器会回滚事务。

同理,也可修改属性,使得事务中碰到unchecked-exception不回滚事务。

@Transactional(noRollbackFor=RuntimeException.class)
public void delete(Integer personid) {

jdbcTemplate.update("delete from person where id=?", new Object[]{personid},

new int[]{java.sql.Types.INTEGER});

throw new RuntimeException("This is unchecked-exception.");

}

注解中指定传播属性

默认状态下,数据库事务的传播属性为REQUIRED。

可以按如下方法明确指定传播属性。

@Transactional(propagation=Propagation.NOT_SUPPORTED)

public List<Person> getPersons() {

return (List<Person>)jdbcTemplate.query("select * from person", new PersonRowMapper());

}

隔离级别

数据库系统提供了四种隔离级别,不同的隔离级别采用不同的锁类型来实现。大多数据库系统默认的隔离级别为Read Committed。

Read Uncommitted:读未提交的数据,会出现脏读,不可重复读和幻读

Read Committed:读已提交的数据,会出现不可重复读和幻读

Repeatable Read:可重复读,会出现幻读

Serializable:最高隔离级别,效率也最低,不会出现以上的脏读,不可重复读和幻读。

脏读:一个事务读到另一个未提交事务的更新数据。

不可重复读:在同一个事物中,多次读取同一数据返回的结果不同。即后续读取可以读到另一个事务已提交的更新事务。

幻读:一个事务读到另一个事务已提交的insert数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: