Hibernate + MySQL + rewriteBatchedStatements=true

Posted

技术标签:

【中文标题】Hibernate + MySQL + rewriteBatchedStatements=true【英文标题】: 【发布时间】:2014-07-03 00:49:09 【问题描述】:

我有以下 Hibernate 配置:

<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3336/db</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>

以及以下映射文件:

<hibernate-mapping default-lazy="false">
    <class name="Table" table="TABLE" batch-size="10" dynamic-update="true">
        <id name="id" column="ID">
            <generator class="uuid"/>
        </id>
        <property name="name" column="NAME" not-null="true"/>
    </class>
</hibernate-mapping>

如果执行这个,它工作正常:

sessionFactory.getSession().beginTransaction();
    sessionFactory.getSession().save(new Table());
    sessionFactory.getSession().save(new Table());
    sessionFactory.getSession().save(new Table());
    sessionFactory.getSession().save(new Table());
sessionFactory.getSession().getTransaction().commit();

但是,将rewriteBatchedStatements=true 添加到hibernate.connection.url

<prop key="hibernate.connection.url">jdbc:mysql://localhost:3336/db?rewriteBatchedStatements=true</prop>

提交时抛出以下异常:

org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 4; expected: 1
    at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:95)
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
    at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)

有什么想法吗?

【问题讨论】:

【参考方案1】:

我使用的 MySQL Connector/J 5.1.28 有这个错误: http://bugs.mysql.com/bug.php?id=68562。降级到 5.1.24 解决了这个问题。

【讨论】:

【参考方案2】:

我也遇到了这个问题,并通过将 mysql 连接器更新到 5.1.36 来修复它:

     <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.36</version>
    </dependency>

【讨论】:

以上是关于Hibernate + MySQL + rewriteBatchedStatements=true的主要内容,如果未能解决你的问题,请参考以下文章

CannotAcquireLockException(Spring、Hibernate、MySQL)

如何将 Hibernate 与 MySQL 连接

JPA+Hibernate+Mysql查询语言问题

Hibernate+maven+mysql

Hibernate、MySQL 视图和 hibernate.hbm2ddl.auto = 验证

JPA、MySQL、Hibernate 和 Maven 骨架