---
title: "spring代理iBATIS时事务不能回滚"
author: "jier007"
date: "2008-06-17T20:57:37Z"
tags: []
categories: ["企业架构"]
slug: "spring-ibatis"
draft: false
lastmod: "2008-06-17T20:57:37Z"
id: 204816
url: "https://jier007.iteye.com/blog/204816"
---

kkkk如题！

废话不多说了，直接上配置，配置如下，各位看了便知！

applicationContext.xml

```
<?xml version="1.0" encoding="UTF-8"?>

<beans

	xmlns="http://www.springframework.org/schema/beans"

	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-2.0.xsd">
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location"><value>classpath:jdbc.properties</value></property>
	</bean>


	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">
		<property name="url"><value>${jdbc.url}</value></property>
		<property name="driverClassName"><value>${jdbc.driver}</value></property>
		<property name="username"><value>${jdbc.username}</value></property>
		<property name="password"><value>${jdbc.password}</value></property>	
		<property name="defaultAutoCommit" value="false"></property>
	</bean>


	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>


	<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor" >
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
			</props>
		</property>
	</bean>


	<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames"> <value>*Service</value></property>
		<property name="interceptorNames"><value>transactionInterceptor</value></property>
	</bean>

	<bean id="sqlMapClientFactory" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation"><value>classpath:sqlMapConfig.xml</value></property>
		<property name="dataSource" ref="dataSource"></property>
		<property name="lobHandler">
			<bean class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
		</property>
	</bean>

	<bean id="sqlMapTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
		<property name="sqlMapClient" ref="sqlMapClientFactory" />
		<property name="exceptionTranslator">
			<bean class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
				<property name="dataSource" ref="dataSource" />
			</bean>
		</property>
	</bean>
</beans>
```

 daoContext.xml

```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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-2.0.xsd">
	<bean id="employeeDao" class="mypackage.dao.impl.EmployeeDaoImpl">
		<property name="sqlMapTemplate">
			<ref bean="sqlMapTemplate" />
		</property>
	</bean>
</beans>
```

 serviceContext.xml

```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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-2.0.xsd">
	<bean id="employeeService" class="mypackage.service.impl.EmployeeServiceImpl">
		<property name="employeeDao"><ref bean="employeeDao" /></property>
	</bean>
</beans>	
```

在EmployeeServiceImpl中故意让程序抛出了exception，但是事务仍然提交了:-(