spring 事务 不回滚
spring 配置文件 如下:
<?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>-->
<!--<!– 本地开发环境配置 –>-->
<!--<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"/>
<!–<property name="namingStrategy">–>
<!–<bean class="org.hibernate.cfg.DefaultNamingStrategy"/>–>
<!–</property>–>
<!–<property name="hibernateProperties">–>
<!–<props>–>
<!–<prop key="hibernate.dialect">${hibernate.dialect}</prop>–>
<!–<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>–>
<!–<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>–>
<!–<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>–>
<!–<prop key="hibernate.cache.provider_configuration_file_resource_path">–>
<!–ehcache/ehcache-hibernate-local.xml–>
<!–</prop>–>
<!–</props>–>
<!–</property>–>
<property name="packagesToScan" value="com.intel.store.*"/>
</bean>-->
<!--<!– 事务管理器配置,单数据源事务 –>-->
<!--<bean id="transactionManager"-->
<!--class="org.springframework.orm.hibernate3.HibernateTransactionManager">-->
<!--<property name="sessionFactory" ref="sessionFactory"/>-->
<!--</bean>-->
<!--<!– 使用annotation定义事务 –>-->
<!--<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>
测试代码:
@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);
}
在测试代码中,抛出异常后,插入的记录无法回滚! spring?事务?无法回滚
[解决办法]
什么不回滚 你就没有配置对
[解决办法]
<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>
记得配置这个,你没配,那个@Transactional(propagation = Propagation.REQUIRED)
是起不了作用的