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

Spring学习总结9(基于Hibernate的事务管理)

2011-04-17 17:45 786 查看
基于注解形式的事务管理

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/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 />
<context:component-scan base-package="com.bjsxt" />

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>

<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.bjsxt.model.User</value>
<value>com.bjsxt.model.Log</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<!-- 声明事务的管理bean ->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 指明事务由annotation来驱动 -->
<tx:annotation-driven transaction-manager="txManager"/>

</beans>


代码示例

类名上也可以写事务注解,但是优先级低于方法上的事务注解

@Transactional(readOnly = true)
public class DefaultFooService implements FooService {

@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void updateFoo(Foo foo) {
// do something

}
}


@Transactional 注解的属性
属性类型描述
propagation枚举型:Propagation可选的传播性设置 默认为required
isolation枚举型:Isolation可选的隔离性级别(默认值:ISOLATION_DEFAULT)
readOnly布尔型读写型事务 只读型事务(当查询时设为只读会比不设要快)
timeoutint型(以秒为单位)事务超时
rollbackFor一组 Class 类的实例,必须是Throwable 的子类一组异常类,遇到时 必须 进行回滚。默认情况下checked exceptions不进行回滚,仅unchecked exceptions(即RuntimeException的子类)才进行事务回滚。
rollbackForClassname一组 Class 类的名字,必须是Throwable的子类一组异常类名,遇到时 必须 进行回滚
noRollbackFor一组 Class 类的实例,必须是Throwable 的子类一组异常类,遇到时 必须不 回滚。
noRollbackForClassname一组 Class 类的名字,必须是Throwable 的子类一组异常类,遇到时 必须不 回滚
事务传播特性详解:

1.required



如果在执行该方法之前,已经打开了一个事务,会将当前方法加入到之前的事务之中。如果没有,则自己新开一个事务。

2.requiredsNew



如果在执行该方法之前,已经打开了一个事务,会挂起之前的事务,自己新开一个事务。当自己的事务执行完之后,再进行之前的事务。

3.mandatory

在调用方法之前,必须已经打开了一个事务,否则将会抛出异常

4.nested

如果在执行方法前,已经打开了一个事务,则再开一个新事务内嵌在之前的事务之中。

nested与required的区别:nested注解的方法,如果事务回滚不会影响到之前的事务

nested与requiredsNew的区别:nested注解的方法,如果之前的事务,在nested已经执行完之后,事务进行了回滚,也会让nested里的事务进行回滚

5.never

如果在执行该方法之前,已经打开了一个事务,则抛出异常

6.not_supported

注解not_supported的方法不支持事务。如果在执行该方法之前,已经打开了一个事务,将之前的事务挂起,执行完该方法之后,在继续进行之前的事务

7.supported

如果在执行该方法之前,已经打开了一个事务,支持之前的事务,如果没有打开事务,则也不打开新的事务。即与调用supported注解的方法的事务保持一致,它有我就有,它无我就无

基于XML的事务管理(常用)

将上面XML中的<tx:annotation-driven transaction-manager="txManager"/>给去掉,增加如下的配置

<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.bjsxt.service..*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="getUser" read-only="true" />
<tx:method name="add*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐