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:aop="http://www.springframework.org/schema/aop"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
			    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
			    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
			    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
			    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

	<description>Spring公共配置 </description>

	<!-- 数据源配置,使用c3p0数据库连接池 -->
	<bean id="systemDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<!-- C3p0 Pool Info -->
		<property name="driverClass" value="${system.jdbc.driver}" />
		<property name="jdbcUrl" value="${system.jdbc.url}" />
		<property name="maxPoolSize" value="${system.c3p0.maxPoolSize}" />
		<property name="minPoolSize" value="${system.c3p0.minPoolSize}" />
		<property name="idleConnectionTestPeriod" value="30" />
		<property name="acquireIncrement" value="0" />
		<property name="maxStatements" value="0" />
		<property name="maxStatementsPerConnection" value="0" />
		<property name="user" value="${system.jdbc.username}" />
		<property name="password" value="${system.jdbc.password}" />
		
	</bean>

	<!-- mybatis sqlsessionfactory -->
	<bean id="systemSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="systemDataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<property name="mapperLocations" value="classpath*:sqlMap/dsp/system/**/*.xml" />
		<property name="plugins">
			<bean class="com.cmcc.common.db.mybatis.page.interceptor.PaginationInterceptor">
				<property name="properties">
					<props>
						<prop key="dialect">${system.jdbc.type}</prop>
					</props>
				</property>
			</bean>
		</property>
	</bean>

	<!-- mybatis mapper scan -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.test.dao.system" />
		<property name="sqlSessionFactoryBeanName" value="systemSqlSessionFactory"></property>
	</bean>

	<!-- 事务管理器配置,单数据源事务 -->
	<bean id="systemTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="systemDataSource" />
	</bean>

	<!-- 使用annotation定义事务 -->
<!-- 	<tx:annotation-driven transaction-manager="systemTransactionManager" /> -->
	<tx:advice id="transactionInterceptor" transaction-manager="systemTransactionManager">
		<tx:attributes>
			<tx:method name="create*" propagation="REQUIRED" rollback-for="Throwable"/>
			<tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable"/>
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" timeout="600" />
			<tx:method name="notify*" propagation="SUPPORTS" read-only="true" timeout="300" />
			<tx:method name="countByImportingContentId" propagation="REQUIRES_NEW" isolation="READ_COMMITTED" timeout="600" rollback-for="Throwable" />
			<tx:method name="batch*" propagation="REQUIRED" isolation="READ_COMMITTED" timeout="1800" rollback-for="Throwable" />
			
		</tx:attributes>
	</tx:advice>

	<!-- 
	execution(* com.test.system.impl..*.*(..))定义在service包和所有子包里的任意类的任意方法的执行
	-->
	<aop:config>
		<aop:advisor advice-ref="transactionInterceptor" pointcut="execution(* com.test.system.impl..*.*(..))" order="40" />
	</aop:config>


</beans> 



在使用annotation定义事务的地方要注意一个地方,比如说如果定义的切入点是create*开头的那么如果配置是

<tx:method name="create*" propagation="REQUIRED" rollback-for="Throwable"/>
是这样配的则会把多个事务都回滚回来



2015-10-30 20:21:01,279 [http-nio-8080-exec-2] DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager]:847 - Initiating transaction rollback
2015-10-30 20:21:01,279 [http-nio-8080-exec-2] DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager]:281 - Rolling back JDBC transaction on Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@5bfd1b70]
2015-10-30 20:21:01,315 [http-nio-8080-exec-2] DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager]:324 - Releasing JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@5bfd1b70] after transaction
2015-10-30 20:21:01,316 [http-nio-8080-exec-2] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils]:327 - Returning JDBC Connection to DataSource


回滚日志Rolling back JDBC transaction on Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@5bfd1b70]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值