首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

:spring aop事务配置 出错不回滚

2012-08-17 
求救:spring aop事务配置出错不回滚事务配置文件:?xml version1.0 encodingUTF-8 ?!-- 该文件用

求救:spring aop事务配置 出错不回滚
事务配置文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<!-- Hibernate事务管理器 -->
<bean id="transactionManagerHibernate" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<!-- 默认超时时间
<property name="defaultTimeout" value="1000" /> -->

</bean>


<!-- 事务管理处理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManagerHibernate">
<tx:attributes>
<!-- 对切面(aop:config)拦截的所有实体类的所有方法按一定规则进行事务管理 -->
<!-- 对所有方法使用SERIALIZABLE(序列化)隔离级别,对所有(继承自)Excception的异常进行回滚 timeout="1000000000"-->

  <tx:method name="find*" read-only="true"/>
  <tx:method name="get*" read-only="true"/>
  <tx:method name="paginate*" read-only="true" />
  <tx:method name="search*" read-only="true"/>
  <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
  <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
  <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
  <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
  <tx:method name="remove*" propagation="REQUIRED" rollback-for="Exception"/>
  <tx:method name="start*" propagation="REQUIRED" rollback-for="Exception"/>
  <tx:method name="approve*" propagation="REQUIRED" rollback-for="Exception"/>
  <tx:method name="disapprove*" propagation="REQUIRED" rollback-for="Exception"/>
  <tx:method name="submit*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>

<!-- 事务管理切面 -->
<aop:config>
<!-- 拦截com.dbw.ghr.persistence.service.impl包及其子包下的所有实体类的所有方法,无论任何作用域(public,private) -->
<!-- 拦截com.dbw.ghr.workflow.impl包及其子包下的所有实体类的所有方法,无论任何作用域(public,private) 
|| execution(* com.dbwen.ehr.workflow.impl.*.*(..))
-->
<aop:pointcut id="serviceMethod" expression="execution(* com.dbwen.ehr.*.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>
</beans>


代码如下:
DAOImpl:
package com.dbw.core.dao.hibernate;
public abstract class DAOImpl extends HibernateDaoSupport implements IDAO{
   
/**


* 执行update ,delete ,insert sql语句
* map 条件
*/
public int executeSql(final String sql,final Map map){
return (Integer)getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException { 
SQLQuery query = session.createSQLQuery(sql); 
if(map!=null){
Set<String> keySet = map.keySet();
for(String key: keySet){
query.setParameter(key, map.get(key));
}
}
return query.executeUpdate(); 

}); 
}
}

OrganizationExpandDAO:
package com.dbwen.ehr.structure.dao.hibernate
public class OrganizationExpandDAO extends DAOImpl implements OrganizationExpandIntf{
  public int executeInsertSql(String tablename,Map map,String otherSql){
String sql="insert into "+tablename ;
if(map!=null){
String values = "";
String params = "";
Set<String> keySet = map.keySet();
for(String key: keySet){
values += key+",";
params += ":"+key+",";
}
sql+="("+values.substring(0,values.length()-1)+")values("+params.substring(0,params.length()-1)+")";
}else if(otherSql!=null){
sql+=" "+otherSql;
}
return this.executeSql(sql,map);
}
}

OrganizationServiceImpl:
public class OrganizationServiceImpl extends ServiceImpl implements IOrganizationService {
public String saveCopyOrg(String[] orgids,String[] chooseOrgids){
  .....
.....
this.orgExpandDAO.executeInsertSql("organization", map, null); //执行成功

this.orgExpandDAO.executeInsertSql("B01", mapB01, null); //执行未成功,报异常,事务没有回滚,第一步数据已经保存到数据库中}
}


struts Action中调用saveCopyOrg方法

如上红色部分描述,不知道问题出在哪里 事务没回滚。

[解决办法]

探讨

executeInsertSql这个方法没有向外面抛异常呀,你在里面是不是给捕捉了?
在saveCopyOrg方法里应该要捕捉异常或是向上抛出异常的。事务回滚是要靠异常来触发的。
配置没看出什么问题,你在executeInsertSql这个方法里把异常抛出看看。

[解决办法]
service层不要捕获异常

热点排行