当前位置: 代码迷 >> Java Web开发 >> spring 当service中空指针错误,事务不回滚
  详细解决方案

spring 当service中空指针错误,事务不回滚

热度:2726   发布时间:2013-02-25 21:16:13.0
spring 当service中空指针异常,事务不回滚?
Java code
<!-- 配置事务特性 -->    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <tx:method name="add*" propagation="REQUIRED" />            <tx:method name="del*" propagation="REQUIRED" />            <tx:method name="update*" propagation="REQUIRED" />            <tx:method name="deploy*" propagation="REQUIRED" />            <tx:method name="submit*" propagation="REQUIRED" />            <tx:method name="get*" read-only="true" />        </tx:attributes>    </tx:advice>    <aop:config>        <aop:pointcut id="daoMethods"            expression="execution(* net.hlj.kj.dao.*.*.*(..))" />        <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->        <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />    </aop:config>    <tx:annotation-driven transaction-manager="transactionManager"        proxy-target-class="true" />action public void activeUser(ActionMapping mapping, ActionForm form,            HttpServletRequest request, HttpServletResponse response)            throws Exception {        long id = UtilTool.str2Long(request.getParameter("id"));         TdUser tu = userService.getUserById(id);        if (tu.getState() == 0) {// 判断是否激活            try {                bouns(tu.getId(), tu.getSuperid(), 1);                 TdUser agent = userService.getUserById(tu.getAgentid());                agent.setMb(5d);                 userService.update(agent);                 //激活账户                userService.activeUser(id);                //购买股票                userService.buyStock(tu);                             } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        response.sendRedirect("doUser.do?method=getUserListByAgent");    }

在这地方 userService.buyStock(tu),这里面有空指针异常,但是事务去不回滚,怎么回事呀?

------解决方案--------------------------------------------------------
去掉catch(Exception)从句,〈tx:method〉中添加rollbackFor="Exception"
  相关解决方案