我是hibernate的新手。 我想在迭代器中使用事务,所以我喜欢这样(没有业务逻辑):
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for {
try {
Something something = session.get(Something.class, id);
something.setX(X);
session.save(something)
tx.commit(); <-- error occurs after iterated once.
catch { tx.rollback(); }
}
Something something2 = session.get(Something.class, id2);
something2.setX(X);
session.save(something2);
tx.commit():
session.close();
但是hibernate发生了Transaction not successfully started错误
所以我改变了这样的代码。
Session session = sessionFactory.openSession();
Transaction tx;
for {
try {
tx = session.beginTransaction();
Something something = session.get(Something.class, id);
something.setX(X);
session.save(something)
tx.commit();
catch { tx.rollback(); }
}
session.beginTransaction();
Something something2 = session.get(Something.class, id2);
something2.setX(X);
session.save(something2);
tx.commit():
session.close();
此代码第一次有效。但是当我重新启动这个程序时,会发生lock wait timeout exceeded
虽然我将innodb_lock_wait_timeout设置为120但结果是一样的
那有什么不对!?