有时,我的应用程序会尝试删除引用其他数据的数据,这会引发“数据完整性违规”,这是预期的。
ERROR org.hibernate.util.JDBCExceptionReporter - Entrada em lote 0 delete from item_tabela_protese where id='2' foi abortada. Chame getNextException para ver a causa.
ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: update or delete on table "item_tabela_protese" violates foreign key constraint "fk_item_tabela_protese" on table "pedido_protese" Detalhe: Key (id)=(2) is still referenced from table "pedido_protese".
出现上述错误后,我无法在应用程序中执行任何操作,我收到“Postgres 连接已关闭”消息。
ERROR org.hibernate.util.JDBCExceptionReporter - This connection has been closed.
如何解决这个问题?
最佳答案
强制回滚后,您必须关闭 session 并获取新 session 。通常,您只需回滚
事务并打开一个新事务,但看起来 Hibernate 可能会在发现错误时关闭连接。如果没有更多日志详细信息,很难确定。
首先尝试回滚事务并获取新的 Hibernate session 。
发生故障后,您不能在同一事务中继续使用 session 。事务处于无效状态,除 ROLLBACK
之外的所有语句都会产生错误。
如果您希望解决这个问题,您可能可以使用SAVEPOINT
和ROLLBACK TO SAVEPOINT
(以性能为代价),但 Hibernate 的缓存可能不能很好地处理这个问题。
关于java - JPA 错误后从 PostgreSQL 连接关闭中恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22178860/