异常捕捉

疯狂de攻城狮 2014-08-15 06:09:45
[2014-08-15 17:40:23] [com.founder.ec.dec.ga.service.impl.QServiceImpl] [com.founder.ec.dec.ga.service.impl.QServiceImpl] [INFO] org.springframework.dao.DuplicateKeyException: SqlMapClient operation; SQL [];
--- The error occurred in config/ibatis-config-maps.xml.
--- The error occurred while applying a parameter map.
--- Check the Count.addProduct-InlineParameterMap.
--- Check the statement (update failed).
--- Cause: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: 违反唯一约束条件 (FEC.PK_GA_COUNT_DATE_PRODUCT)
; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:

最近在开发过程中遇到一个这样的情况,在程序运行之前,我知道在插入数据时会有违反主键约束异常,而我要做的是在异常发生时捕捉它,然后对发生异常的数据进行更新,请问应该怎么处理?
...全文
114 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
save4me 2014-08-16
  • 打赏
  • 举报
回复
参考: Java Code Examples for java.sql.SQLIntegrityConstraintViolationException

public void unPost(PaymentIn payment) throws Exception {
	try {
		this.em
				.createQuery(
						"delete from AccDoc t "
								+ " where t.docUuid = :invoiceUuid")
				.setParameter("invoiceUuid", payment.getUuid())
				.executeUpdate();
		payment.setPosted(false);
		this.em.merge(payment);
	} catch (Exception e) {
		if (e.getCause() != null
				&& e.getCause() instanceof SQLIntegrityConstraintViolationException) {
			throw new RuntimeException(
					"Cannot unpost document `"
							+ payment.getCode()
							+ "`. The corresponding accounting document is already posted to great ledger.");
		} else {
			throw e;
		}
	}
}

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧