was not registered for synchronization because synchronization is not active JDBC Connection


Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19e35973] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7b51e83e] will not be managed by Spring
==>  Preparing: update fpinfo SET rz_stauts = ?, fp_status = ? where fpdm = ? and fphm = ? 
==> Parameters: 1(Integer), 0(Integer), 037021700107(String), 12673513(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19e35973]
原因:重复扫描的问题

解决: applicationContext.xml只扫描service层和dao层,springMvc.xml只扫描controller层。

    <!-- ②自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->
    <context:component-scan base-package="com.qdhtxx">
 
        <!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />-->
    </context:component-scan>
    <context:component-scan base-package="com.qdhtxx">
       <!----><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>
这样配置,事物能够正常使用。在一个service方法内,对数据库进行对此操作,如果在操作过程中出现异常,则可以全部回滚。

声明式事务管理有两种实现方式:基于xml配置文件的方式;另一个实在业务方法上进行@Transaction注解,将事务规则应用到业务逻辑中。

基于xml配置文件的方式:

    <!-- 开启事务注解驱动 -->
    <tx:annotation-driven />
    
    <!-- 定义事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>
 
    <!-- 配置事务特性 ,配置add、delete和update开始的方法,事务传播特性为required -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="init*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
 
    <!-- 配置那些类的方法进行事务管理 -->
    <aop:config>
        <aop:pointcut id="allManagerMethod" expression="execution (* com.qdhtxx.service..*.*(..))" />
        <!--<aop:pointcut id="allManagerMethod" expression="execution (* com.qdhtxx.service.*.impl.*.*(..))" />-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
    </aop:config>
业务方法上进行@Transaction注解:

    <!-- 定义事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>
然后在方法上加@Transaction注解注解。
原文地址:https://www.cnblogs.com/hzcya1995/p/13317444.html