In applicationContext.xml transaction configuration file does not work, can not control the abnormal rollback

First, the blogger in learning to integrate ssm framework, met a wonderful problem is to control affairs, wrote in does not work in applicationContext.xml file, in the transaction control method, even if there has been an exception, but the transaction does not roll back the pit stands to reason that, we configured affairs, in an exception occurs, the abnormal operation period is captured into our framework, will make a rollback operation for us, but it is not, such as Bo Lord wrote a simple transfer transaction, the first account of the money deducted, but an exception has occurred but we have found in the database after running deducting money, money is still detained,

Bloggers have tried most of the online method, he has said the only reason, does not solve the real problem, the following talk about my own solution

I was the operational configuration of declarative transaction management on the springmvc.xml file, you discover the magic of the transaction control can be achieved, and that is when the transfer abnormal operation rollback can be achieved;

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       HTTP: // www.springframework.org/schema/beans/spring-beans.xsd 
       HTTP: // www.springframework.org/schema/context 
       HTTP: // the WWW .springframework.org / Schema / context / the Spring-context.xsd 
        HTTP: // www.springframework.org/schema/aop 
        HTTP: // www.springframework.org/schema/aop/spring-aop.xsd 
        HTTP: // www.springframework.org/schema/tx 
        HTTP: // www.springframework.org/schema/tx/spring-tx.xsd ">
 
    <- turn scan can be configured to not scan controller ->!
    <context:component-scan base-package="com.song">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <!--配置数据库的连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="jdbc:mysql:///song?serverTimezone=GMT%2B8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    </bean>

    <!- Configuration sqlsessionfactory plant -> 
    <-! Scan Interface Configuration ->
    </ the bean>
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
        <Property name = "the dataSource" REF = "the dataSource"> </ Property>

    <the bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" ID = "mapperScannerConfigurer"> 
        <Property name = "basePackage" value = "com.song.dao"> </ Property> 
    </ the bean> 

    <! - originally written in this document, and can not achieve control of affairs -> 
    <! - configure declarative transaction Manager -> 
    <bean the above mentioned id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <Property name = "dataSource" ref = "dataSource"> </ Property> 
    </ bean> 


    <- - Notes configuration affairs!> 
    <- <tx:! annotation-Driven transaction-Manager = "transactionManager"> < / tx: Annotation-Driven> -> 

    <- configuration Services ->!
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="transfer" propagation="REQUIRED" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!--织入事务-->
    <aop:config>
        <aop:pointcut id="txpoint" expression="execution(* com.song.service.impl.*ServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txadvice" pointcut-ref="txpoint"></aop:advisor>
    </aop:config>

</beans>

The approach is three big transactions on the file to springmvc.xml

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd 
        HTTP: // www.springframework.org/schema/tx 
        HTTP: // www.springframework.org/schema/tx/spring-tx.xsd 
        " >
 
    < ! - scan configuration -> 
    <context: scan-Component Base -package = " com.song " > 
        <context: the include filter-type = " Annotation " expression The = " org.springframework.stereotype.Controller " /> 
    </ context: Component-Scan> 

    <- mvc opening spring support (the open configuration map and adapters) ->! 
    <mvc: Annotation-Driven> </ mvc:annotation-driven>

    <! - mapper configuration view -> 
    <the bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver " ID = " the InternalResourceViewResolver " > 
        <Property name = " suffix " value = " .jsp " > </ Property> 
        <-! this little leverage must pay attention to -> 
        <Property name = " prefix " value = " / the WEB-INF / Page / " > </ Property> 
    </ bean> 

    <!- Configure release static resources in two ways: 
    using the springmvc framework analysis: 
    If the configuration is not to map the path DispatcherServlet/ Time, requests for static resources will eventually be handled by the default configuration of tomcat, it does not affect normal access to static resources. 
    If the mapping is configured path DispatcherServlet / time will override the default configuration of default tomcat, so need to be configured in springmvc file, static resource release.
    -> 
    <- <MVC:! Mapping Resources = " / JS / ** " LOCATION = " / JS / " > </ MVC: Resources> -> 

    <MVC: default -servlet-Handler default -servlet-name = " default " > </ MVC: default -servlet-Handler> 

    ! <- interceptor configured to intercept -> 
    <MVC: interceptors> 
        <MVC: interceptor> 
            <MVC:"/>
            <bean class="com.song.interceptor.MyInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>


    <!--配置声明式事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>


    <!--注解配置事务-->
   <!-- <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>-->

    <!--配置事务-->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="transfer" propagation="REQUIRED" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!--织入事务-->
    <aop:config>
        <aop:pointcut id="txpoint" expression="execution(* com.song.service.impl.*ServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txadvice" pointcut-ref="txpoint"></aop:advisor>
    </aop:config>


</beans>

This would solve the problem if there are other solutions but also welcome, we learn together

 

ps: a recording error

Error creating bean with name 'accountController': Unsatisfied dependency expressed through field 'accountService'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'accountServiceImpl' is expected to be of type 'com.song.service.impl.AccountServiceImpl' but was actually of type 'com.sun.proxy.$Proxy24'

 

This error type is implanted without the use of an interface service layer bean object when the controller layer, the type of realization of the class

Guess you like

Origin www.cnblogs.com/yifachen/p/12056899.html