把事务放在service层,项目的目录结构是  
    +com
        +owner
              +dao
                  OwnerDao.java
              +service
                      OwnerService.java        
        +param
              +bp
                 +dao
                     BpDao.java
                 +service
                     BpService.javaspring +ibatis 下 配置aop 事务的 代码如下: <bean id="transactionManager_Apprsal"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource_apprsal"></property>
</bean>
<tx:advice id="txAdvice_Apprsal"  transaction-manager="transactionManager_Apprsal">
<tx:attributes>
<tx:method name="login" propagation="REQUIRED" />
<tx:method name="signOut" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="copy*" propagation="REQUIRED"/>
<tx:method name="getSerial" propagation="REQUIRED"/>
 <tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allDAO_Apprsal"
expression="execution(* com.easepal..service.*Service.*(..))" />
<aop:advisor advice-ref="txAdvice_Apprsal" pointcut-ref="allDAO_Apprsal" />
</aop:config>但是这样配置的 不成功 

解决方案 »

  1.   

    楼主DAO层的sqlmapclient是怎么注入的,要生效你必需用spring的sqlmapclient,
    SqlMapClientFactoryBean这个类是工厂类,不能够让ibatis自己加载<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
            <property name="configLocation" value="/WEB-INF/sqlmap-config.xml"/> <!-- iBatis sqlmap config 文件位置 -->
            <property name="dataSource" ref="dataSource"/> <!-- 在SpringFramework配置文件中使用的数据源 -->
        </bean>
      

  2.   

    有使用这个 ,就是这个没有配置好  expression="execution(* com.easepal..service.*Service.*(..))" />  事务配置再dao层就没有问题 配置在dao层时   expression="execution(* com.easepal..dao.*Dao.*(..))" />
      

  3.   

    有点疑惑dao层一般是单一的事物性操作看不出来事物是否生效的,麻烦贴下完整的配置文件看看...
      

  4.   

    可以 
    <bean id="propertyConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
    <value>classpath:init.properties</value>
    </property>
    </bean>
    <!-- ************************************************************************-->
    <!-- ////////////////////////数据库(dbcp:apprasal)连接池配置*********************-->
    <bean id="dataSource_apprsal"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName"
    value="${jdbc.driverClassName}">
    </property>
    <property name="url" value="${jdbcapprsal.url}"></property>
    <property name="username" value="${jdbcapprsal.username}"></property>
    <property name="password" value="${jdbcapprsal.password}"></property>
    <property name="maxActive" value="100"></property>
    <property name="maxIdle" value="30"></property>
    <property name="maxWait" value="1000"></property>
    <property name="defaultAutoCommit" value="false"></property>
    <property name="removeAbandoned" value="true"></property>
    <property name="removeAbandonedTimeout" value="60"></property>
    <property name="logAbandoned" value="true"></property>
    </bean>
    <!-- sql map client 配置 -->
     <bean id="sqlMapClient_apprsal"
    class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation"
    value="classpath:/ibatis-sql-map-config-easepal.xml" />
    <property name="dataSource" ref="dataSource_apprsal" />
    </bean>
    <!-- ************************************************************************-->
    <!-- //////////////////////ibatis事务代理配置///////////////////////////////-->
    <!-- ************************************************************************-->

    <!-- 配置哪些类哪些方法使用事务 -->
    <bean id="transactionManager_Apprsal"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource_apprsal"></property>
    </bean>
    <tx:advice id="txAdvice_Apprsal"  transaction-manager="transactionManager_Apprsal">
    <tx:attributes>
    <tx:method name="login" propagation="REQUIRED" />
    <tx:method name="signOut" propagation="REQUIRED" />
    <tx:method name="create*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="save*" propagation="REQUIRED"/>
    <tx:method name="insert*" propagation="REQUIRED"/>
    <tx:method name="delete*" propagation="REQUIRED"/>
    <tx:method name="add*" propagation="REQUIRED"/>
    <tx:method name="copy*" propagation="REQUIRED"/>
    <tx:method name="getSerial" propagation="REQUIRED"/>
    <!-- tx:method name="*" read-only="true" /-->
    </tx:attributes>
    </tx:advice>
    <aop:config>
    <aop:pointcut id="allDAO_Apprsal"
    expression="execution(* *..service.*Serivce.*(..))" />
    <aop:advisor advice-ref="txAdvice_Apprsal" pointcut-ref="allDAO_Apprsal" />
    </aop:config>
    <!-- ************************************************************************-->
    <!-- //////////////////////  DAO    /////////////////////////////////////////-->
    <!-- ************************************************************************-->
    <bean id="daoUserValidate" 
    class="com.succeed.apprsal.user.dao.impl.UserDaoImpl">
    <property name="sqlMapClient">
    <ref bean="sqlMapClient_apprsal" />
    </property>
    </bean>
      

  5.   

    怎么配置文件里没有service类的配置信息...没有它无法实现拦截的...
      

  6.   

    在加上 service 类 的<!-- ************************************************************************-->
    <!-- //////////////////////////登录用户检查与会话检查/////////////////////////-->
    <!-- ************************************************************************-->

    <bean id="ValidateUserInterceptor" class="com.succeed.apprsal.service.ValidateUserInterceptor">
    <property name="userDao" ref="daoUserValidate"></property>
    <property name="sessionDao" ref="daoSessionValidate"></property>
    </bean>    <!-- ************************************************************************-->
    <!-- //////////////////////CXF Services//////////////////////////////////////-->
    <!-- ************************************************************************-->

    <cxf:bus>
    <cxf:features>
    <cxf:logging />
    </cxf:features>
    <!-- sk added 2010-07-27  
    <cxf:inInterceptors>
                <ref bean="ValidateUserInterceptor"/>
            </cxf:inInterceptors>
           -->
    </cxf:bus>

    <!-- 用户与会话管理 -->
    <jaxws:endpoint xmlns:tns="http://succeed.com/apprsal/user/"
    id="UserService" implementor="#Impl_UserService"
    endpointName="tns:UserPort" serviceName="tns:UserService" 
    address="/UserService"/>
      <bean id="Impl_UserService" class="com.succeed.apprsal.user.service.impl.UserServiceImpl">
    <property name="userDao" ref="daoUser"></property>
    <property name="sessionDao" ref="daoSession"></property>
    </bean>

    <jaxws:endpoint xmlns:tns="http://easepal.com/param/"
    id="ParamService" implementor="#Impl_ParamService"
    endpointName="tns:ParamPort" serviceName="tns:ParamService" 
    address="/ParamService"/>
      <bean id="Impl_ParamService" class="com.easepal.param.service.impl.ParamServiceImpl">
    <property name="bpDao" ref="daoParamBp"></property>
    <property name="tempDao" ref="daoParamTemp"></property>
    <property name="userDao" ref="daoEuser"></property>
    </bean>
      

  7.   

    就我的理解整体配置没有什么问题,但这个切入点
    expression="execution(* com.easepal..service.*Service.*(..))" 
    和你给com.succeed.apprsal.service.ValidateUserInterceptor
    com.succeed.apprsal.user.service.impl.UserServiceImpl
    com.easepal.param.service.impl.ParamServiceImpl
    这个都不匹配吧....
      

  8.   

    就是切入点啊,一开始我就写了项目目录结构,直接配置com.easepal.param.service.impl.ParamServiceImpl  这个service  的事务是可以的,但是配置的
    expression="execution(* com.easepal..service.*Service.*(..))"  这个要符合所以  service  类的事务,可以不用考虑  com.succeed.apprsal.user.service.impl.UserServiceImpl
      

  9.   

    因为我是在 ParamServiceImpl 这个service下调用 两个 dao  
      

  10.   

    那你的问题就可以定位在切入点的匹配上了
    expression="execution(* com.easepal..service.*Service.*(..))"这个匹配不了
    com.easepal.param.service.impl.ParamServiceImpl
    我猜测这样可以匹配你的所有service
    expression="execution(* com.easepal..service.impl.*Service*.*(..))"
    参考下http://hotpepper.iteye.com/blog/141350 这个吧
      

  11.   

    <aop:config>
    <aop:pointcut id="serviceOperation"
    expression="execution(* com.xyh.*.*.dao.*.*(..))" />
    <aop:advisor pointcut-ref="serviceOperation"
    advice-ref="txAdvice" />
    </aop:config>
      

  12.   

    <!-- 为SqlMap绑定数据源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="configLocation" value="classpath:ibatis/mybatis-config.xml" />
    <property name="dataSource" ref="dataSource" />
    </bean>
      

  13.   

    <bean id="jimUserMapper" class="org.mybatis.spring.MapperFactoryBean">
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    <property name="mapperInterface" value="com.hy.portal.product.dao.UserManageMapper" />
    </bean>
      

  14.   

    expression="execution(* com.easepal..service.impl.*Service*.*(..))"正解
      

  15.   

    我改成这种 expression="execution(* com.easepal..service.impl.*ServiceImpl.*(..))"
    方式 ,数据都没有进入到数据库中,我觉得是事务没有提交 ,这个事务管理都配置好了 ,应该会自动提交吧 ,为什么数据没有插入到数据库中呢 
      

  16.   

    难道  expression="execution(* com.easepal..service.impl.*ServiceImpl.*(..))" 这个和
    com.easepal.param.service.impl.ParamServiceImpl 这个类不匹配吗?
      

  17.   

    1、你确认无插入数据库,系统也无报错?(排除是否回滚了)
    2、可能是数据库连接池的原因
    <property name="defaultAutoCommit" value="false"></property>
    删除此行看看
      

  18.   

    太佩服你了 ,你能解释下什么原因吗?因为数据库连接池在把事务放在dao层时都是没有问题 的 
      

  19.   

    我想再问下 ,再service层下调用 两个dao层的数据插入的方法 ,现在我测试事务回滚 ,第一个dao 层插入数据库成功的 ,第二个插入数据库是不成功的 ,为什么事务没有回滚?
    这个业务逻辑层的代码 public String saveParam(EuserComm euserComm, Bp[] bp, Temp[] temps, Bo[] bo) {
    if(bp==null||bp.length==0){
    throw new RuntimeException("RC002");
    }
    BpPojo[] bpList=new BpPojo[bp.length];
    for(int i=0;i<bp.length;i++){
    BpPojo bpPojo=new BpPojo();
    Bp bpObj=bp[i];
    bpPojo.setBpId(UUIDGenerator.getUUID());
    bpPojo.setRegisterId(bpObj.getRegisterId());
    bpPojo.setUserName(userDao.getUserNameByRegId(bpObj.getRegisterId()));
    bpPojo.setMeasureTime(StringUtil.dateFormat("yyyyMMddHHmm", bpObj.getMeasureTime()));
    bpPojo.setProductNo(euserComm.getProductNo());
    bpPojo.setSendTime(StringUtil.dateFormat("yyyyMMddHHmm", euserComm.getSendTime()));
    bpPojo.setSp(bpObj.getSp());
    bpPojo.setDp(bpObj.getDp());
    int pp=bp[i].getSp()-bp[i].getDp();
    bpPojo.setPp(pp);
    bpList[i]=bpPojo;
    }
    bpDao.saveBp(bpList);

    if(temps==null||temps.length==0){
    throw new RuntimeException("RC002");
    }
    TempPojo[] tempList=new TempPojo[temps.length];
    String userName=userDao.getUserNameByRegId(temps[0].getRegisterId());
    for(int i=0;i<temps.length;i++){
    TempPojo tempPojo=new TempPojo();
    Temp temp=temps[i];
    tempPojo.setTempId(UUIDGenerator.getUUID());
    tempPojo.setRegisterId(temp.getRegisterId());
    tempPojo.setProductNo(euserComm.getProductNo());
    tempPojo.setSendTime(StringUtil.dateFormat("yyyyMMddHHmm",euserComm.getSendTime()));
    tempPojo.setUserName(userName);
    tempPojo.setMeasureTime(StringUtil.dateFormat("yyyyMMddHHmm",temp.getMeasureTime()));
    tempPojo.setTempValue(Float.parseFloat(temp.getTempValue()));
    tempList[i]=tempPojo;
    }
    tempDao.saveTemp(tempList);

    return "RC001";
    }
      

  20.   

    试着抛出SQLException异常,不要用RuntimeException
      

  21.   

    但是这个不是数据库异常,只是 逻辑上的判断 ,判断 Bp[] bp, Temp[] temps, Bo[] bo  这个三个参数是否为null  或者 长度 为0 ,若是这样这个事务就是失败 的 ,这样的能用 SQLExceptio 这个异常吗?
      

  22.   

    1、你是出现什么情况它无回滚?抛出的什么异常?
    2、尝试修改下<tx:method name="save*" propagation="REQUIRED"/>
    <tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable"/>
    对所有异常都回滚
      

  23.   

    不行 ,还是没有回滚 ,不知道这个和 webservice 是否有关系 ,因为我是java 客户端测试  webservice 的 ,上面那些都是用java 开发的一个 web service 的 
      

  24.   

    没有关系的,dao里面你有做异常捕获么?贴下回滚不了时报的异常,还有dao里面saveBp()这个方法看看