求助,spring3 注解事务 回滚失败
<?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:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url"> <value>jdbc:mysql://localhost:3306/hterp_2011?useUnicode=true&characterEncoding=UTF-8 </value> </property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.blooming.impl" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> --> </props> </property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="messages/messages" /> <property name="cacheSeconds" value="0" /> </bean> <context:component-scan base-package="com.blooming.impl.*" /> <context:component-scan base-package="org.springframework.samples.mvc.basic.account"></context:component-scan> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <mvc:annotation-driven /> <mvc:interceptors> <bean class="com.blooming.web.handler.UserInterceptorAdapter" /> </mvc:interceptors> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="mediaTypes"> <map> <entry key="html" value="text/html" /> <entry key="json" value="application/json" /> </map> </property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="1000000" /> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/classes/views/" /> <property name="suffix" value=".jsp" /> </bean> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/></beans>
@Transactional(propagation=Propagation.REQUIRED) public boolean saveDoodToStorage(GoodsArrivalForm form, Pager pager, Model model) {// DefaultTransactionDefinition def = new DefaultTransactionDefinition(); // TransactionStatus status = transactionManager.getTransaction(def); /** * *这块为零星入库,需要跟原有的系统的采购入库集成,下面分为几步 * *StorageListService 先创建入库单 * *然后是入库单细目 StorageListDetailService * *接着是库存成本,就是记录库存中的每个商品的数量,价钱 GoodsCostService * *然后创建商品细目,记录一些入库单号,出库单号之类的 GoodsDetailService * *以上全部成功后,修改商品购买记录 * */ boolean succ = false;// try { int goodsno = 0; // 入库的商品数量 if(ObjectUtil.isNotEmpty(form.getGoodsno())){ goodsno=form.getGoodsno(); }else{ if (ObjectUtil.isNotEmpty(form.getCode())) { if (CollectionsUtil.notEmpty(form.getCode())) { goodsno = form.getCode().length; } } } if(goodsno<=0){ return false; } GoodsRecord gr = null;// 商品购买记录 if (ObjectUtil.isNotEmpty(form.getGrid())) { gr = grservice.find(form.getGrid()); } if (ObjectUtil.isEmpty(gr)) { return false; } Storage storage = null; if (ObjectUtil.isNotEmpty(form.getStoid())) { storage = stoservice.find(form.getStoid()); } if (ObjectUtil.isEmpty(storage)) { return false; } StorageList storageList = new StorageList(); Date createDate = new Date(); storageList.setCode(codeService.createNewCode(CODE)); storageList.setCreateDate(createDate); storageList.setCreateUser(form.getUser().getChinaname()); storageList.setAuditStatus(1);// 零星入库为已审核状态 storageList.setAboutTicket(form.getGrid() == null ? "" : form .getGrid().toString());// 相关单据号,这块为商品购买记录id storageList.setAboutTicketType(5);// 5为零星入库 succ = slservice.saveOrUpdate(storageList); if (succ == false) { return false; }// throw new RuntimeException("aaaaaaa"); List<StorageList> stolist = slservice.findByItem(storageList); if (ObjectUtil.isNotEmpty(stolist)) { if (stolist.size() > 0) { StorageList newsto = stolist.get(0); /** * 如果报销单保存成功,接下来为保存报销单细目 */ StorageListDetail stoderail = new StorageListDetail(); stoderail.setStorageList(newsto); stoderail.setGoods(gr.getGoodsId()); stoderail.setGoodsNo(Float.valueOf(goodsno)); stoderail.setCost(gr.getPrice()); stoderail.setCreateDate(createDate); stoderail.setStorageCode(storage.getCode()); stoderail.setGoodsUnitId(1); succ = sldeservice.saveOrUpdate(stoderail); if (succ == false) { return false; } /** * 下面为保存具体的每个商品的入库条码信息 也就是商品细目 */ for (int i = 0; i < goodsno; i++) { GoodsDetail gd = new GoodsDetail(); gd.setCode("100010"); gd.setGoodsId(gr.getGoodsId().getId()); if(gr.getGoodsId().isEqualcode()==true){ gd.setGoodsCode(form.getCode()[0]); }else{ gd.setGoodsCode(form.getCode()[i]); } gd.setStorage(storage); gd.setEnterDate(createDate); gd.setStatus(1);// 因为是零星入库,直接就是在库状态,跳过审核 gd.setStorageInCode(newsto.getCode()); succ = gdservice.saveOrUpdate(gd); if (succ == false) { return false; } } /** *接下来为保存库存成本 * *这块有待商榷,是否需要下次入库的时候,是修改原来的数据,还是新添加数据 */ GoodsCost goodscost = new GoodsCost(); goodscost.setGoods(gr.getGoodsId()); goodscost.setStorage(storage); goodscost.setStockNo(gr.getNum().floatValue());// 采购数量 goodscost.setStorageNo(Float.valueOf(goodsno));// 库存数量 goodscost.setNo(Float.valueOf(goodsno)); goodscost.setTotalMoney(gr.getPrice() * goodsno); succ = goodscostservice.saveOrUpdate(goodscost); if (succ == false) { return false; } /** * 需要保存的数据全部成功后,修改商品购买记录的入库信息 */ Integer goodsnum = gr.getNum(); Integer no = goodsno + gr.getInstoreNum();// 已入库数量加上这次入库的数量 if (no < goodsnum) { /** * 如果已入库数量加上这次入库的数量,小于总体的采购数量 则状态为部分入库 */ gr.setInstoreStatus(2); } else { gr.setInstoreStatus(3); } gr.setInstoreNum(no); succ = grservice.saveOrUpdate(gr); if (succ == false) { return false; } } } throw new RuntimeException("aaaaaaa");// } catch (Exception e) {// // TODO: handle exception// throw e;// } // return succ; }
<!-- spring 管理事务 --> <bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- =======================advice====================================== --> <tx:advice id="txAdvice" transaction-manager="myTxManager"> <!-- 声明事务规则 --> <tx:attributes> <tx:method name="find*" propagation="REQUIRED" read-only="false" /> <tx:method name="test*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="reg*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="*" propagation="SUPPORTS" read-only="false" /> </tx:attributes> </tx:advice> <!-- ============================aop===================================== --> <aop:config> <!-- 定义切面 --> <aop:pointcut id="oaServiceMethods" expression="execution(* com.soft.struts2.serviceimpl.*.*(..))" /> <!-- 将事务通知和切面组合 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="oaServiceMethods" /> </aop:config>
[解决办法]