[英]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
,以使inserts
和updates
性能更好。
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个简单实体 ( ProductType
, RawMaterialType
和PriceHistory
)。 computeDelta
and computePrice
are just algorithm functions with no DB stuff. computeDelta
和computePrice
只是没有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.