spring中事务管理(1)

<?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-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">

<!-- 装载属性配置文件 -->
<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties">
<!-- <list>
<value>classpath:jdbc.properties</value>
</list>
-->
</property>
</bean>

<!-- 配置数据源 -->
<bean id="dataSource" 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>

<!-- 配置SessionFactory -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>

<!-- 配置事务管理器-->

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="default">
<property name="dataSource" ref="dataSource"/>
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!--
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
-->

<!-- 若方法名不和下边的相同,可以修改。例如:<tx:method name="add*" propagation="REQUIRED"/> -->

<!--
1.事务传播设置是 REQUIRED
2.隔离级别是 DEFAULT
3.事务是 读/写
4.事务超时默认是依赖于事务系统的,或者事务超时没有被支持。
5.任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚
-->

<!--属性的介绍 -->
<!--
propagation属性:事务传播行为REQUIRED
I.PROPAGATION_REQUIRED:如果当前没有事务,就新建一个事务,
如果存在,加入到这个事务中------->常用
I.PROPAGATION_SUPPORTS:支持当前事务,若没有事务,就以非事务的
方式执行。
I.PROPAGATION_MANDATORY:使用当前事务,若没有事务就抛出异常。
I.PROPAGATION_REQUIRES_NEW:新建事务,若当前存在事务,把
事务挂起。
I.PROPAGATION_NOT_SUPPORTS:以非事务方式执行操作,如果当前
存在事务就挂起。
I.PROPAGATION_NEVER:以非事务的方式执行,如果当前存在事务抛出异常。
I.PROPAGATION_NESTED:如果当前存在事务,则在嵌套事务内执行。
若没有事务,则执行PROPAGATION_REQUIRED类似的操作。
isolation属性:事务隔离级别 DEFAULT
rollback-for属性: 将被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'
read-only属性:事务是否只读?false
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="load*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 第一个 * —— 通配 任意返回值类型
第二个 * —— 通配 包com.spring.services下的任意class
第三个 * —— 通配 包com.spring.services下的任意class的任意方法
第四个 .. —— 通配 方法可以有0个或多个参数
-->
<!--配置切入点-->
<aop:config>
<aop:pointcut id="allMethod" expression="execution(* com.spring.services.*.*(..))"/>
<aop:advisor pointcut-ref="allMethod" advice-ref="txAdvice"/>
</aop:config>
</beans>


<!--
注意:导入commons-dbcp.jar
commons-pool.jar
-->[color=violet][/color][color=violet][/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值