spring事物配置

1、本人最近开发遇到了这样的情况。spring事物都是按照正常的配置来的。可是事物就是不起作          用。经过一天半的修改,终于弄成功了,现在和大家一起分享一下。
我参考的url分享给大家:http://jinnianshilongnian.iteye.com/blog/1850432

      事物配置需要的包。
aopalliance-1.0.jar
        aspectjweaver-1.7.2.jar

      以及其他的spring的相关包,我用的是jdbcTemplate,没有使用hibernate。

      最容易犯的错误:spring重复扫描配置文件。

 <context:component-scan base-package="com.knowledge.spring.demo.base.service.system">  
     <context:exclude-filter type="annotation" expression="org.springframework.
stereotype.Controller"/>  
</context:component-scan>

<!--  自动扫描且只扫描@Controller mvc:annotation-driven必须有,他是扫描controller的,没有他,controll不可用-->
<mvc:annotation-driven/>
<context:component-scan base-package="com.knowledge.spring.demo.base.controller">
<context:include-filter type="annotation" expression="org.springframework
.stereotype.Controller" />
</context:component-scan>

一开始我将这两个配置放到了一起,都放到了springmvc.xml导致spring在扫描的时候重复扫描来的service,和repository。这样spring的事物配置就失去了作用。后来我将前者放在了applicationContext.xml后者继续留在springmvc.xml成功了!
这两个配置必须这么配置

事务的实现方式有两种如下:

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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"
>
          
    <tx:annotation-driven transaction-manager="transactionManagerJDBCTwo" />
 <tx:advice id="txAdvice" transaction-manager="transactionManagerJDBCTwo">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED"  />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED"  />
<tx:method name="save*" propagation="REQUIRED" />
  <tx:method name="*" propagation="REQUIRED" /> 
</tx:attributes>
</tx:advice> 
<aop:config>
<!-- <aop:pointcut id="know-service" expression="execution(* com.knowledge.spring.demo.base.service.system.service.*(..))" />  -->
<aop:pointcut id="knowService" expression="execution(* com.knowledge.spring.demo.base.service.system.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="knowService" />
</aop:config>
<bean id="transactionManagerJDBCTwo" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
     <property name="dataSource" ref="dataSourceJdbc"></property>  
</bean>
</beans>
2、声明式事务: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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"
>
    <!-- mode="aspectj" 表示使用 aspectj 方式进行事物切入,否则自动以 spring Aop 为基础
         这个参数 与  @Transactional一起用的,所以以前我使用 注解事物分方式不起作用
      -->      
    <bean id="transactionManagerJDBC" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
     <property name="dataSource" ref="dataSourceJdbc"></property>  
</bean>
 <tx:annotation-driven transaction-manager="transactionManagerJDBC" /> 
</beans>
这样只需要在调用事务的方法上加上

@Transactional(isolation = Isolation.SERIALIZABLE,propagation=Propagation.REQUIRES_NEW)即可
isolation :配置的事务的隔离级别。事务有5中隔离级别
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值