我发现了奇怪的休眠行为,我无法解释。
如果我在事务内的默认线程中创建一个对象并进行手动刷新,那么我在其他线程中找不到它。
如果我在一个具有相同条件的特殊线程中创建一个对象,那么一切都很好。
这是我上面描述的代码:
// transaction template with propagation required
ttNew.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
Assert.assertEquals(envStDao.getAll().size(), 0);
g = new Group();
g.setDescription("trial");
// in debugger I get id = 1
groupDao.save(g);
groupDao.flush();
accDao.flush();
}
});
// second stage right after the first - searching the group
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
ttNew.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// here I get NULL!
Group gg = groupDao.get(1);
}
});
}
});
t2.start();
t2.join();
如果我像以前一样将代码的第一个块包装到线程中,我会得到该组。
有什么想法吗?
我在junit测试中运行上面的代码。Dao 对象使用 HibernateTemplate。