您如何处理和解析JPA持久性异常以向用户提供有意义的消息 [英] how do you handle and parse JPA persistence exceptions to provide meaningfull message to user

查看:140
本文介绍了您如何处理和解析JPA持久性异常以向用户提供有意义的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JPA还是陌生的,并且想在处理JPA的持久性异常时找到最佳实践,例如说用户可以解决 的唯一约束违规.关于如何编写JPA应用程序,有无数的示例,但是对于如何处理由它们引发的异常却几乎一无所知. :/

I am fairly new to JPA and want to find best practices when handling persistence exceptions from JPA for things like say, unique constraint violations which can be addressed by the user. There are tons of examples on how to write JPA apps, but almost nothing on how to hand exceptions kicked out by them. :/

例如,注册用户时,该人输入系统已在使用中的电子邮件地址,并且违反了约束条件:

For example registering a user, the person enters an email address that is already in active use by the system and gets a constraint violation:

try {
     em.persist(credentials);
} catch (javax.persistence.PersistenceException ex) {

在添加重复的电子邮件时会产生此错误:

which produces this error when a duplicate email is added:

WARNING: SQL Error: 0, SQLState: 23505
SEVERE: ERROR: duplicate key value violates unique constraint "EMAIL_UQ_IDX"
  Detail: Key (email)=(testuser@xyz.com) already exists.

如何获得有意义的答案给用户?例如,类似:糟糕,好像有人已经在使用该电子邮件地址,您确定您之前没有注册吗?是否有内置的工具可以解析此内容,或者我是否需要对(可能是一系列)if语句中的异常消息运行正则表达式?

How can I get a meaningful answer back to the user? For example something like: Oops looks like someone is using that email address already, are you sure you haven't registered before? Is there a facility built in to parse this or will I need to run regexes against the exception message in a (possibly a series of) if statement(s)?

那如果它被捕获在业务层上呢?将其提升到表示层的最佳实践是什么...就像我之前说过的那样,以便可以为用户提供一个'不错'的消息

And what about if it is caught on the business tier... what are best practices for kicking it up to the presentation tier... like I said before, so that a 'nice' message can be provided for the user.

为清楚起见添加: 正是这样,人们知道,我曾经,曾经并且仍在研究所有不同类型的持久性异常,这是我一直在做的一些研究,但我并未包括在上面的"try语句"示例中:

Added for clarity: Just so people know, I had, have, and am still looking at all the different types of persistence exceptions, and here is some of the research I've been doing I didn't include with the "try statement" example I included above:

try {
     em.persist(credentials);
     } catch (javax.persistence.PersistenceException ex) {
         System.out.println("EXCEPTION CLASS NAME: " + ex.getClass().getName().toString());
         System.out.println("THROWABLE CLASS NAME: " + ex.getCause().getClass().getName().toString());
                Throwable th = ex.getCause();
         System.out.println("THROWABLE INFO: " + th.getCause().toString());
         Logger.getLogger(CredentialsControllerImpl.class
              .getName()).log(Level.INFO, "Credentials Controller "
                  + "persistence exception "
                      + "EXCEPTION STRING: {0}", ex.toString());
         Logger.getLogger(CredentialsControllerImpl.class
              .getName()).log(Level.INFO, "Credentials Controller "
                  + "persistence exception "
                      + "THROWABLE MESSAGE: {0}", th.getMessage());
         Logger.getLogger(CredentialsControllerImpl.class
              .getName()).log(Level.INFO, "Credentials Controller "
                  + "persistence exceptions "
                      + "THROWABLE STRING: {0}", th.toString());
     }

:)

推荐答案

您通常不使用低级异常来做到这一点.

You typically don't use the low-level exceptions to do that.

相反,您显式检查电子邮件是否可用(使用查询),并仅在电子邮件不存在时才保留该电子邮件.

Instead, you explicitely check that the email is available (using a query), and perist the email only if it doesn't exist.

当然,如果两个线程并行执行相同的检查,则可能会出现争用情况,但是这种情况非常少见,并且存在数据库约束来保证唯一性.

Sure, there could be a race condition if two threads do the same check in parallel, but it will be extremely rare, and the database constraint is there to guarantee the uniqueness.

这篇关于您如何处理和解析JPA持久性异常以向用户提供有意义的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆