NutzCN Logo
问答 Tran.exec(new Atom(){});抛出异常事务未回滚
发布于 2949天前 作者 qq_48b77d9d 3129 次浏览 复制 上一个帖子 下一个帖子
标签: 事务

数据库版本:mysql5.6, 表皆为InnoDB,事务操作内容:售卖信息提交,同步库存表和财务流水表。

	public String save(@Param("..") final SundryIncome sundryIncome, final HttpSession session, final String saveType, final String receiptAwlId) {
		if (sundryIncome == null) {
			return "0";
		}
		try {
			Trans.exec(Connection.TRANSACTION_SERIALIZABLE, new Atom() {

				public void run() {

					Manager manager = (Manager) session.getAttribute(UserSession.USER_SESSION);

					StudentInfo stuinfo = dao.fetch(StudentInfo.class, sundryIncome.getStudentUuid());
					SundryIncome _f0 = getLastedSundryIncome(manager.getSchoolUuid());// 最近一条sundryIncome,取得dateSequenceId

					String dateSequence = null;
					if (_f0 == null) {
						dateSequence = "";
					} else {
						dateSequence = _f0.getDateSequence();
					}
					DateSequenceGenerator g = new DateSequenceGenerator();
					// 生成新的日期串号
					String next_dateSequence = g.getNextDateSequence(DateSequenceGenerator.PREFIX_TYPE_SUNDRYFEE, dateSequence);// 日期串号前缀为:ZF

					if (stuinfo == null) {
						TeacherInfo teainfo = dao.fetch(TeacherInfo.class, sundryIncome.getStudentUuid());
						if (teainfo == null) {// 如果学生信息没有,则去教师信息表查,可能是自销
							throw new NullPointerException("不存在的学生信息或教师信息,请重新录入!!");
						} else {
							sundryIncome.setTeacherUuid(teainfo.getUuid());
							sundryIncome.setTeacherName(teainfo.getName());
							sundryIncome.setMemo(sundryIncome.getMemo() + "[自销]");
							sundryIncome.setClassUuid("-NONE-");
							sundryIncome.setClassName("-NONE-");
						}

					} else {
						sundryIncome.setTeacherUuid(stuinfo.getTeacherUuid());
						sundryIncome.setTeacherName(stuinfo.getTeacherName());
						sundryIncome.setClassUuid(stuinfo.getClassUuid());
						sundryIncome.setClassName(stuinfo.getClassName());
					}

					sundryIncome.setDateSequence(next_dateSequence);// 新的日期串号

					sundryIncome.setSchoolName(manager.getSchoolName());
					sundryIncome.setSchoolUuid(manager.getSchoolUuid());
					sundryIncome.setAccountant(manager.getRealName());
					sundryIncome.setAccountantUuid(manager.getRealNameUuid());
					sundryIncome.setCreateTime(new Date(new java.util.Date().getTime()));

					// savetype==1:出票
					if ("1".equals(saveType)) {
						sundryIncome.setIsIssued((byte) 1);// 是否出票,值为1,表示已出票
						sundryIncome.setIssuedDate(new Date(new java.util.Date().getTime()));
						ReceiptBox rbox = new ReceiptBox();
						String uuid = null;
						if (StringUtils.isBlank(receiptAwlId)) {
							uuid = UUID.randomUUID().toString();
						} else {
							// 出票并打印,这时发送来一个receiptAwlId,uuid无须自动生成
							uuid = receiptAwlId;
						}
						rbox.setAwlId(uuid);
						sundryIncome.setReceiptAwlId(uuid);
						rbox.setTransactionType(CONST.RECEIPT_TRANSACTION_SUNDRY_TYPE);// 票据注明:杂费收入
						ReceiptBox lastReceiptBox = getLastedReceiptBox(manager.getSchoolUuid());
						String receiptBox_dateSequence = (lastReceiptBox == null) ? "" : lastReceiptBox.getDateSequence();
						String receiptBox_next_dateSequence = g.getNextDateSequence(DateSequenceGenerator.PREFIX_TYPE_TUITIONFEE, receiptBox_dateSequence);
						rbox.setDateSequence(receiptBox_next_dateSequence);
						// sundryIncome.setReceiptBoxDateSequence(receiptBox_next_dateSequence);
						rbox.setAccountant(manager.getRealName());
						rbox.setAccountantUuid(manager.getRealNameUuid());
						rbox.setStudentName(sundryIncome.getStudentName());
						rbox.setStudentUuid(sundryIncome.getStudentUuid());
						rbox.setSchoolName(manager.getSchoolName());
						rbox.setSchoolUuid(manager.getSchoolUuid());
						rbox.setAmount(sundryIncome.getAmount());// 票据总额
						rbox.setMemo(sundryIncome.getMemo());
						List<SundryIncome> sundryincomes = new ArrayList<SundryIncome>(1);
						sundryincomes.add(sundryIncome);
						rbox.setJsonContent(Json.toJson(sundryincomes));// 把票据项目作为json字串存储
						Long paramTime = new java.util.Date().getTime();
						rbox.setCreateTime(new Date(paramTime));
						rbox.setIssuedReceiptDate(new java.sql.Date(paramTime));// 出票日期
						rbox.setIssuedReceiptTime(new java.sql.Time(paramTime));// 出票时间
						dao.insert(rbox);// 存储票据信息
					}

					dao.insert(sundryIncome);// 保存杂费数据

					// >>>>>>>>>>>>>>>库存要减
					Inventory inventoryItem = dao.fetch(Inventory.class, sundryIncome.getInventoryId());
					if (inventoryItem != null) {
						Double nowQuantity = inventoryItem.getQuantity();// 现在的库存量
						Double saleQuantity = sundryIncome.getTotal();// 正在售出的数量
						BigDecimal _b_nowQuantity = new BigDecimal(nowQuantity);// 现在的库存量bigDecimal类型
						BigDecimal _b_saleQuantity = new BigDecimal(saleQuantity);// 正在售出的数量bigDecimal类型

						Double newQuantity = _b_nowQuantity.subtract(_b_saleQuantity).doubleValue();// 现在的库存减去售出的数量=新的库存量
						if (newQuantity < 0) {
							try {
								throw new Exception("库存不足");
							} catch (Exception e) {
								e.printStackTrace();
							}
						}

						inventoryItem.setQuantity(newQuantity);// 更新库存量
						inventoryItem.setUpdateTime(new Date(new java.util.Date().getTime()));// 库存本项目更新日期

						dao.update(inventoryItem);
					} else {
						try {
							throw new Exception("没有找到相应的库存!SundryIncomeAction.save(...)");
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

					// >>>>>>>>>>>>>>>>同步数据到t_journalaccount
					if (sundryIncome.getIsSyn() == 1) {
						try {
							synToJournalAccount(manager, sundryIncome);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			});

		} catch (Exception e) {
			return e.toString();
		}
		return "1";
	}
6 回复

把异常贴一次

而且说一下是那句话抛出异常

if (newQuantity < 0) {
							try {
								throw new Exception("库存不足");
							} catch (Exception e) {
								e.printStackTrace();
							}
						}

异常信息为:java.lang.Exception: 库存不足
	at com.binfenjuxian.amigo.actionlogic.SundryIncomeAction$1.run(SundryIncomeAction.java:329)
	at org.nutz.trans.Trans.exec(Trans.java:174)
	at com.binfenjuxian.amigo.actionlogic.SundryIncomeAction.save(SundryIncomeAction.java:232)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.integration.shiro.NutShiroProcessor.process(NutShiroProcessor.java:51)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:198)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
java.lang.Exception: 库存不足
	at com.binfenjuxian.amigo.actionlogic.SundryIncomeAction$1.run(SundryIncomeAction.java:329)
	at org.nutz.trans.Trans.exec(Trans.java:174)
	at com.binfenjuxian.amigo.actionlogic.SundryIncomeAction.save(SundryIncomeAction.java:232)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.integration.shiro.NutShiroProcessor.process(NutShiroProcessor.java:51)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:198)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

我晕, 你得抛出Trans.exec

@wendal 嗯 嗯, throw RuntimeException OK了:)

添加回复
请先登陆
回到顶部