spring代理iBATIS时事务不能回滚
kkkk如题!
废话不多说了,直接上配置,配置如下,各位看了便知!
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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 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" ref="dataSource"/></bean><bean id="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 ref="dataSource"></property><property name="lobHandler"><bean ref="sqlMapClientFactory" /><property name="exceptionTranslator"><bean 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" /></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" /></property></bean></beans>
在EmployeeServiceImpl中故意让程序抛出了exception,但是事务仍然提交了:-(
}public void setSqlMapTemplate(SqlMapClientTemplate sqlMapTemplate)
{
this.sqlMapTemplate = sqlMapTemplate;
}
public SqlMapClientTemplate getSqlMapTemplate()
{
return sqlMapTemplate;
}
}</pre>
<p>? EmployeeServiceImpl.java</p>
<pre name='code' class='java'>package mypackage.service.impl;
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;
}
}</pre>
?
Regards. 3 楼 workman93 2008-07-23 Spring代理需要一个RuntimeException才可以回滚。