【发布时间】:2018-03-26 17:14:32
【问题描述】:
我查看了许多其他关于不捕获异常的问题(包括Can't catch ConstraintViolationException),但没有一个问题与我相同。
使用 RestEasy 联网并使用 Hibernate 链接 Postgres db。
我有一个对象:
@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"user_id"})})
public class Outcome {
@Id
@GeneratedValue
private Long id;
@OneToOne
private User user;
private String anotherAttribute;
}
db 服务通过 OutcomeResource.saveOutcome 发送一个 Outcome:
@POST
@Consumes("application/json")
@Transactional
public Response saveOutcome(Outcome outcome){
try {
myRepository.save(outcome);
return Response.ok().build();
} catch (Exception e) {
System.out.println("I caught the exception:" + e);
return Response.status(Response.Status.BAD_REQUEST).build();
}
}
然后通过 OutcomeRepository.save 将其保存在数据库中:
public void save(Outcome outcome) {
try {
getEntityManager().persist(outcome);
} catch (Exception e) {
System.out.println("No I caught the exception : " + e);
}
}
当第一个结果发布并为用户保存时,它成功保存在数据库中。如果发布第二个结果,则会按预期抛出错误。
缩短的错误堆栈跟踪是:
javax.persistence.RollbackException:
Error while committing the transaction
Description: The server encountered an unexpected condition that
prevented it from fulfilling the request.
Exception: org.jboss.resteasy.spi.UnhandledException:
javax.persistence.RollbackException: Error while committing the transaction
org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:78)...
Root cause: javax.persistence.RollbackException:
Error while committing the transaction...
Root cause: javax.persistence.PersistenceException:
org.hibernate.exception.ConstraintViolationException:
could not execute statement...
Root cause: org.hibernate.exception.ConstraintViolationException:
could not execute statement...
Root cause: postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint
Detail: Key (user_id)=(361) already exists.
我想捕获此错误以返回错误的请求响应。 尽管尝试在堆栈跟踪中捕获通用异常和每种特定类型的异常,但都没有输入任何 catch 块。
为什么这两种方法都没有捕获异常?怎么可能呢?
【问题讨论】:
-
不是 EntityManager 抛出异常,而是事务。