gpt4 book ai didi

mysql - 如何捕获 Grails 中的约束(索引)违规?

转载 作者:行者123 更新时间:2023-11-29 22:01:21 25 4
gpt4 key购买 nike

似乎无法找到正确的方法来捕获 Grails 应用程序中的 MySQL 索引违规。来自 Controller 的代码:

try {
configurationInstance.save(flush: true) {

request.withFormat {...}
}

catch (JDBCException exception) {
flash.error = "This would be a duplicate attribute, please change the suffix."
render view: 'edit', model: [exception: exception]

}

当我通过提交重复值引发错误条件时,catch block 将被忽略,并且我会收到包含以下详细信息的 500 错误页面:

URI/itools/configuration/save

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException 类

消息 key “publisher_config_uk”的条目重复“1009303-photo-recommendable-exclusion-pattern.2-0”

在控制台中我得到了:ERROR 错误。GrailsExceptionResolver - 处理请求时发生 MySQLIntegrityConstraintViolationException:[POST]/itools/configuration/save - 参数:我尝试过捕捉

  • JDBC异常
  • SQL错误
  • SQLException
  • SQLDataException
  • SQLIntegrityConstraintViolationException
  • MySQLIntegrityConstraintViolationException
  • com.mysql.jdbc.exceptions.jdbc4.MySQLDataException

一切都无济于事。 (FWIW 我还在相关属性上应用了唯一约束,当它应该导致验证错误时它什么也不做。这是一个完全不同的问题。)

正确的做法是什么?

最佳答案

找到它:它是 DuplicateKeyException。

(我必须捕获通用异常并查看其详细信息才能找到它。)

现在我遇到了一个新问题:在尝试渲染或重定向时,我得到“itools.Configuration 条目中的 null id(发生异常后不要刷新 session )”。这对我来说毫无意义——由于重复的键,新实体从未保存到数据库中,那么为什么空 ID 对象会成为问题呢?

关于mysql - 如何捕获 Grails 中的约束(索引)违规?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32616726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com