ibatis事物管理不起作用

来源:互联网 发布:短信轰炸php源码 编辑:程序博客网 时间:2024/06/08 19:56

由于离职,没啥事,就瞎折腾springMVC+ibatis事物管理问题,结果折腾了好几天,走了不少弯路

事物配置:

 <context:component-scan base-package="com.ideal.spring"  >

<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
 <context:exclude-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice"/>

</context:component-scan>
    <bean id="propertyConfigurer" class="com.ideal.utils.PropertyUtil">
        <property name="locations">
            <list>
                <value>classpath:dbconfig.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />
        <property name="driverClassName" value="${database.driverClassName}" />
    </bean>

    <!-- 具体的sql语句配置 -->
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation">
            <value>classpath:SqlMapConfig.xml</value>
        </property>
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
    </bean>
    <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
        <property name="sqlMapClient" ref="sqlMapClient" />
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>


    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <aop:config>
        <aop:pointcut id="productServiceMethods"
            expression="execution(* com.ideal.spring.service..*.*(..)) " />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
    </aop:config>
     <aop:aspectj-autoproxy proxy-target-class="true" />
    <!-- 事务配置 -->
     <tx:advice id="txAdvice" transaction-manager="txManager">
    
        <tx:attributes>
            <tx:method name="*"   rollback-for="Exception"/>
            <tx:method name="query*" propagation="REQUIRED" read-only="true" />
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />
            <tx:method name="count*" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice>



事物配置注意点

1.rollback-for="Exception"这样的话比如sqlExceotion才会回滚

2.service层的事物要抛出异常,这样spring才能捕获

3.@Transactional注解写在方法上,会起作用,加在class上我也是死活不起作用

4.其次spring扫描注解的问题,千万不要重复扫描

mvc.xml:



    <context:component-scan base-package="com.ideal.spring.controller"   >

<context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice"/>

</context:component-scan>

一定要记住只扫描controller有 ,如果不行加入 use-default-filters="false"



其他IOC容器:application.xml:

 <context:component-scan base-package="com.ideal.spring"  >

<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
 <context:exclude-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice"/>

</context:component-scan>

一定要排除controller



0 0
原创粉丝点击