2013年11月4日星期一

spring transaction is not rolled back

spring configuration file as follows:
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
                      http://www.springframework.org/schema/tx/spring-tx-3.0."
       default-lazy-init="false">

    <description>Spring公共配置</description>
    <!--  后台异步请求RSS源的定时器
    <import resource="applicationContext-quartz-cron-local.xml" />
     -->
    <!-- 定义受环境影响易变的变量 -->
    <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="locations">
            <list>
                <!-- 标准配置 -->
                <value>classpath*:/application.properties</value>
                <!-- 集群中节点配置 -->
                <!--<value>classpath*:/application.cluster.properties</value>-->
                <!--&lt;!&ndash; 本地开发环境配置 &ndash;&gt;-->
                <!--<value>classpath*:/application.local.properties</value>-->
                <!-- 服务器生产环境配置 <value>file:/var/xpress/application.server.properties</value> -->
            </list>
        </property>
    </bean>

    <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
    <context:component-scan base-package="com.intel.store"/>

    <!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <!-- Connection Info -->
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!-- Connection Pooling Info -->
        <property name="maxIdle" value="${dbcp.maxIdle}"/>
        <property name="maxActive" value="${dbcp.maxActive}"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="3600000"/>
        <property name="minEvictableIdleTimeMillis" value="3600000"/>
    </bean>

    <!-- 数据源配置,使用应用服务器的数据库连接池 -->
    <!--<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/ExampleDB"
    /> -->

    <!-- Hibernate配置 -->
    <!--<bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        &lt;!&ndash;<property name="namingStrategy">&ndash;&gt;
        &lt;!&ndash;<bean class="org.hibernate.cfg.DefaultNamingStrategy"/>&ndash;&gt;
        &lt;!&ndash;</property>&ndash;&gt;
        &lt;!&ndash;<property name="hibernateProperties">&ndash;&gt;
        &lt;!&ndash;<props>&ndash;&gt;
        &lt;!&ndash;<prop key="hibernate.dialect">${hibernate.dialect}</prop>&ndash;&gt;
        &lt;!&ndash;<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>&ndash;&gt;
        &lt;!&ndash;<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>&ndash;&gt;
        &lt;!&ndash;<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>&ndash;&gt;
        &lt;!&ndash;<prop key="hibernate.cache.provider_configuration_file_resource_path">&ndash;&gt;
        &lt;!&ndash;ehcache/ehcache-hibernate-local.xml&ndash;&gt;
        &lt;!&ndash;</prop>&ndash;&gt;
        &lt;!&ndash;</props>&ndash;&gt;
        &lt;!&ndash;</property>&ndash;&gt;
        <property name="packagesToScan" value="com.intel.store.*"/>
    </bean>-->

    <!--&lt;!&ndash; 事务管理器配置,单数据源事务 &ndash;&gt;-->
    <!--<bean id="transactionManager"-->
    <!--class="org.springframework.orm.hibernate3.HibernateTransactionManager">-->
    <!--<property name="sessionFactory" ref="sessionFactory"/>-->
    <!--</bean>-->


    <!--&lt;!&ndash; 使用annotation定义事务 &ndash;&gt;-->
    <!--<tx:annotation-driven transaction-manager="transactionManager"-->
    <!--proxy-target-class="true"/>-->
    <!--<aop:aspectj-autoproxy/>-->
    <!--
    <bean class="org.springframeword.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
    -->
    <!--  Spring 依赖注入配置信息        -->
    <bean id="toolsDao"
          class="com.intel.store.dao.ToolsDao">
        <property name="basicDataSource" ref="dataSource"/>
    </bean>
    <bean id="imageUploadDao"
          class="com.intel.store.dao.ImageUploadDao">
    </bean>
    <bean id="loginServiceImpl"
          class="com.intel.store.service.LoginServiceImpl">
        <property name="toolsDao" ref="toolsDao"/>
    </bean>

    <bean id="storeServiceImpl"
          class="com.intel.store.service.StoreServiceImpl">
        <property name="toolsDao" ref="toolsDao"/>
    </bean>
    <bean id="imageUploadServiceImpl"
          class="com.intel.store.service.ImageUploadServiceImpl">
        <property name="imageUploadDao" ref="imageUploadDao"/>
    </bean>
    <bean id="vstStoreServiceImpl"
          class="com.intel.store.service.VstStoreServiceImpl">
        <property name="imageUploadDao" ref="imageUploadDao"/>
    </bean>
    <bean id="activityServiceImpl"
          class="com.intel.store.service.ActivityServiceImpl">
        <property name="toolsDao" ref="toolsDao"/>
    </bean>

    <!-- 定时器配置文件 -->
    <import resource="application-quartz.xml" />


    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager"/>

    <!-- a PlatformTransactionManager is still required -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- (this dependency is defined somewhere else) -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>


test code :
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveLoginInfo(String userName, String dateString, String result) {
        String sql = "INSERT INTO prc_mbl_usr_usg (slsprs_id, lgn_dtm, lgn_sts ) VALUES (" + "'" + userName + "'," + "'" + dateString + "'," + "'" + result + "')";
        logger.info(sql);
        toolsDao.insertUtils(sql);
        int m = 1;
        if (m == 1) {
            throw new RuntimeException();
        }
        toolsDao.insertUtils(sql);
    }


In the test code, an exception is thrown , the inserted record can not rollback !
------ Solution ---------------------------------------- ----
what does not roll back you do not configure
------ Solution ---------------------- ----------------------

<context:component-scan base-package="cn.xxx.xxx.service">
<context:include-filter type="regex" expression=".*"/>
<context:exclude-filter type="regex" expression="Abstract.*"/>
</context:component-scan>

remember to configure this , you do not match , the @ Transactional (propagation = Propagation.REQUIRED)
is no effect of
------ For reference only ----------------------------- ----------

spring of document is this configuration :
    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager"/>
 
    <!-- a PlatformTransactionManager is still required -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- (this dependency is defined somewhere else) -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

Please advise about where to configure the wrong ?
------ For reference only -------------------------------------- -
do not know that using a mybaties or give you other examples it yourself facing facie

------ For reference only ----- ----------------------------------
I use the connection pool dbcp and jdbc, useless any orm framework , the part about the transaction configuration is the official document from the spring to find , for you do not understand my case it gives such configurations reply , I expressed some confusion
------ For reference only ---------------------------------------
me a go then you stick out of the configuration file how have Hibernate
------ For reference only ---------------------------- -----------
is your method is not Spring managed
------ For reference only ----------------- ----------------------
SessionFactory configuration and hibernate transaction configurations are commented out
------ For reference only ---------------------------------------
methodological @ Transactional (propagation = Propagation.REQUIRED) what is it ? spring but also how to manage it ?
------ For reference only -------------------------------------- -
already configured this , ah ?
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
    <context:component-scan base-package="com.intel.store"/>

this does is to scan the annotations do ?
------ For reference only -------------------------------------- -
find the cause of the problem , I use the data access method is the connection pool, an attribute of the connection pool configuration is:
<property name="defaultAutoCommit" value="true"/>

that is set automatically submitted become true, when it is first inserted automatically submitted , and the true to false,
affairs are managed by the spring , and then tested , the transaction can be rolled back normal

没有评论:

发表评论