关闭

spring代理iBATIS时事务不能回滚

标签: springibatisencodingclassdaoexception
946人阅读 评论(0) 收藏 举报

kkkk如题!

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

applicationContext.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans  
  4.   
  5.     xmlns="http://www.springframework.org/schema/beans"  
  6.   
  7.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  8.   
  9.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  10.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  11.         <property name="location"><value>classpath:jdbc.properties</value></property>  
  12.     </bean>  
  13.   
  14.   
  15.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">  
  16.         <property name="url"><value>${jdbc.url}</value></property>  
  17.         <property name="driverClassName"><value>${jdbc.driver}</value></property>  
  18.         <property name="username"><value>${jdbc.username}</value></property>  
  19.         <property name="password"><value>${jdbc.password}</value></property>      
  20.         <property name="defaultAutoCommit" value="false"></property>  
  21.     </bean>  
  22.   
  23.   
  24.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  25.         <property name="dataSource" ref="dataSource"/>  
  26.     </bean>  
  27.   
  28.   
  29.     <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor" >  
  30.         <property name="transactionManager" ref="transactionManager" />  
  31.         <property name="transactionAttributes">  
  32.             <props>  
  33.                 <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
  34.                 <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>  
  35.             </props>  
  36.         </property>  
  37.     </bean>  
  38.   
  39.   
  40.     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  41.         <property name="beanNames"> <value>*Service</value></property>  
  42.         <property name="interceptorNames"><value>transactionInterceptor</value></property>  
  43.     </bean>  
  44.   
  45.     <bean id="sqlMapClientFactory" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  46.         <property name="configLocation"><value>classpath:sqlMapConfig.xml</value></property>  
  47.         <property name="dataSource" ref="dataSource"></property>  
  48.         <property name="lobHandler">  
  49.             <bean class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>  
  50.         </property>  
  51.     </bean>  
  52.   
  53.     <bean id="sqlMapTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">  
  54.         <property name="sqlMapClient" ref="sqlMapClientFactory" />  
  55.         <property name="exceptionTranslator">  
  56.             <bean class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">  
  57.                 <property name="dataSource" ref="dataSource" />  
  58.             </bean>  
  59.         </property>  
  60.     </bean>  
  61. </beans>  

 daoContext.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  5.     <bean id="employeeDao" class="mypackage.dao.impl.EmployeeDaoImpl">  
  6.         <property name="sqlMapTemplate">  
  7.             <ref bean="sqlMapTemplate" />  
  8.         </property>  
  9.     </bean>  
  10. </beans>  

 serviceContext.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  5.     <bean id="employeeService" class="mypackage.service.impl.EmployeeServiceImpl">  
  6.         <property name="employeeDao"><ref bean="employeeDao" /></property>  
  7.     </bean>  
  8. </beans>    

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

分享到:  
评论
3 楼 workman93 2008-07-23  
Spring代理需要一个RuntimeException才可以回滚。
2 楼 Allen 2008-06-18  
Where is ur pattern matching beanName(s) declared? 

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

Regards.
1 楼 jier007 2008-06-17  
<p>EmployeeDaoImpl.java</p>
<pre name='code' class='java'>package mypackage.dao.impl;

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;
}
}</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>
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14253次
    • 积分:240
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:18篇
    • 译文:0篇
    • 评论:3条
    最新评论