简体   繁体   English

Hibernate和MySQL超过了锁定等待超时(使用播放框架)

[英]Lock wait timeout exceeded with Hibernate and MySQL (using play framework)

In my web application I'm using Stateless sessions with Hibernate to have better performances on my inserts and updates . 在我的Web应用程序中,我与Hibernate一起使用了Stateless sessions ,以使insertsupdates性能更好。

It was working fine with H2 database (the one used in play framework in dev mode). 它与H2 database (在开发模式下的播放框架中使用的H2 database )配合良好。

But when I test it with MySQL I get the following exception : 但是,当我使用MySQL测试它时,出现以下异常:

ERROR ~ Lock wait timeout exceeded; try restarting transaction
ERROR ~ HHH000315: Exception executing batch [Lock wait timeout exceeded; try restarting transaction]

Here is the code : 这是代码:

public static void update() {
    Session session = (Session) JPA.em().getDelegate();
    StatelessSession stateless = this.session.getSessionFactory().openStatelessSession();

        try {

            stateless.beginTransaction();

            // Fetch all products
            {
                List<ProductType> list = ProductType.retrieveAllWithHistory();
                for (ProductType pt : list) {
                    updatePrice(pt, stateless);
                }
            }

            // Fetch all raw materials
            {
                List<RawMaterialType> list = RawMaterialType.retrieveAllWithHistory();
                for (RawMaterialType rm : list) {
                    updatePrice(rm, stateless);
                }
            }
        } catch (Exception ex) {
            play.Logger.error(ex.getMessage());
            ExceptionLog.log(ex, Thread.currentThread());
        } finally {
            stateless.getTransaction().commit();
            stateless.close();
        }
}

private static void updatePrice(ProductType pt, StatelessSession stateless) {
    pt.priceDelta = computeDelta();
    pt.unitPrice = computePrice();

    stateless.update(pt);

    PriceHistory ph = new PriceHistory(pt, price);

    stateless.insert(ph);
}

private static void updatePrice(RawMaterialType rm, StatelessSession stateless) {
    rm.priceDelta = computeDelta();
    rm.unitPrice = computePrice();

    stateless.update(rm);

    PriceHistory ph = new GoodPriceHistory(rm, price);

    stateless.insert(ph);
}

In this example I have 3 simple Entities ( ProductType , RawMaterialType and PriceHistory ). 在此示例中,我有3个简单实体ProductTypeRawMaterialTypePriceHistory )。 computeDelta and computePrice are just algorithm functions with no DB stuff. computeDeltacomputePrice只是没有DB内容的算法函数。 retrieveAllWithHistory functions are functions that fetch some data from the database using Play framework model functions. retrieveAllWithHistory函数是使用Play framework model函数从数据库中获取某些数据的函数。

So, this code retrieves some data, edit some, create new one and finally save everything. 因此,这段代码检索了一些数据,编辑了一些数据,创建了一个新数据,最后保存了所有内容。

Why have I a lock exception with MySQL and no exception with H2 ? 为什么我在MySQL有锁定异常而在H2没有异常?

I'm not sure why you have a commit in a finally block. 我不确定您为什么要在finally块中进行提交。 Give this structure a try: 试试这个结构:

try {
    factory.getCurrentSession().beginTransaction();
    factory.getCurrentSession().getTransaction().commit();
} catch (RuntimeException e) {
    factory.getCurrentSession().getTransaction().rollback();
    throw e; // or display error message
}

Also, it might be helpful for you to check this documentation . 另外,查看本文档可能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 hibernate锁定等待超时超时; - hibernate Lock wait timeout exceeded; 超过了锁定等待超时; 尝试在spring_Hibernate_Mysql中重新启动事务 - Lock wait timeout exceeded; try restarting transaction in spring_Hibernate_Mysql MySQL错误:超过了锁定等待超时; 尝试重新启动事务查询 - MySQL Error: Lock wait timeout exceeded; try restarting transaction Query MYSQL锁等待超时超过更新SQL上的错误 - MYSQL Lock wait timeout exceeded error on update sql 超过了锁定等待超时; 尝试使用spring和Mysql重新启动事务Java - Lock wait timeout exceeded; try restarting transaction Java with spring and Mysql 超过了锁定等待超时-使用单例数据库管理器插入 - Lock wait timeout exceeded- insert using a singleton db manager 超过锁定等待超时; 尝试使用JDBC重新启动事务 - Lock wait timeout exceeded; try restarting transaction using JDBC 获取WARN:SQL错误:1205,SQLState:41000错误:超出锁定等待超时; 尝试重新启动事务。 使用休眠保存记录 - Getting WARN: SQL Error: 1205, SQLState: 41000 ERROR: Lock wait timeout exceeded; try restarting transaction. Saving a record in using hibernate 更新数据库时出错:超过了锁定等待超时 - Error updating database:Lock wait timeout exceeded 已超过READ COMMITTED锁定等待超时 - READ COMMITTED lock wait timeout exceeded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM