ITeye博客

spring代理iBATIS时事务不能回滚

jier007 · 2008-06-17 20:57 · 企业架构 · Markdown

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,但是事务仍然提交了:-(

评论(3)

  1. #3 楼 · workman93 · 2008-07-23 14:34

    Spring代理需要一个RuntimeException才可以回滚。

  2. #2 楼 · Allen · 2008-06-18 10:26

    Where is ur pattern matching beanName(s) declared?

    I can only find id declaration of those "*Service" beans...

    Regards.

  3. #1 楼 · jier007 · 2008-06-17 21:02

    import org.springframework.orm.ibatis.SqlMapClientTemplate;

    import mypackage.dao.EmployeeDao; import mypackage.pojo.Employee;

    public class EmployeeDaoImpl implements EmployeeDao { private SqlMapClientTemplate sqlMapTemplate;

    public void saveEmployee(Employee emp)
    {
    	sqlMapTemplate.insert("employee.save" emp);
    }
    
    public void setSqlMapTemplate(SqlMapClientTemplate sqlMapTemplate)
    {
    	this.sqlMapTemplate = sqlMapTemplate;
    }
    
    public SqlMapClientTemplate getSqlMapTemplate()
    {
    	return sqlMapTemplate;
    }
    

    }

    import mypackage.dao.EmployeeDao; import mypackage.pojo.Employee; import mypackage.service.EmployeeService;

    public class EmployeeServiceImpl implements EmployeeService { private EmployeeDao dao;

    public void saveEmp(Employee emp)
    {
    	dao.saveEmployee(emp);
    	Integer.parseInt("回滚回滚回滚吧!!!");
    }
    
    public void setEmployeeDao(EmployeeDao dao)
    {
    	this.dao = dao;
    }
    

    }

评论功能维护中,当前仅展示历史评论。