【问题标题】:Grails MySQLIntegrityConstraintViolationException thrown抛出 Grails MySQLIntegrityConstraintViolationException
【发布时间】:2013-01-15 01:05:26
【问题描述】:

我创建了一个 Grails 应用程序并将其部署到 Tomcat 服务器。我已将 Grails 应用程序配置为使用外部 MySQL 数据库。在启动 Tomcat 服务器并部署应用程序(并创建数据库模式)后,我运行一个脚本,它将关系数据从现有数据库中的多个表传输到 Grails 应用程序生成的新数据库。

脚本运行没有问题,并且在插入过程中没有数据库错误。

插入后,我使用我的 Grails 应用程序查看所有现有数据,没有任何问题。

但是,如果我停止 Tomcat 服务器然后重新启动它,就会出现问题。我在 catalina.out 中遇到以下错误:

2013-01-15 00:43:37,820 [pool-2-thread-1] ERROR util.JDBCExceptionReporter  - Duplicate entry '1-2' for key 'PRIMARY'
2013-01-15 00:43:37,826 [pool-2-thread-1] ERROR events.PatchedDefaultFlushEventListener  - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:679)
Caused by: java.sql.BatchUpdateException: Duplicate entry '1-2' for key 'PRIMARY'
        at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024)
        at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449)
        at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
        at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
        ... 6 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1-2' for key 'PRIMARY'
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
        at com.mysql.jdbc.Util.getInstance(Util.java:386)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415)
        at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1976)
        ... 9 more

我对这个错误感到困惑,因为如果存在重复数据,那么首先如何成功插入信息?

谢谢!

【问题讨论】:

  • 好像又执行了批量数据迁移脚本?

标签: mysql spring grails


【解决方案1】:

检查您的 BootStrap.groovy 文件并检查您没有在现有数据库中插入任何内容

示例: def adminRole = Role.findByAuthority("ROLE_ADMIN") ?: new Role(authority: "ROLE_ADMIN").save(failOnError: true)

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2016-02-24
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2013-05-29
    相关资源
    最近更新 更多