Hibernate 语句挂起
从我的 junit 测试中,我调用一个简单的方法来清除我的表:
@AfterClass
public static void runAfterClass() {
//Clear db contents after tests
// NOTE this clears ALL data from the table
try {
System.out.println("deleting db contents");
HibFunction.clearTable();
} catch (Exception e) {
System.out.println("Couldnt delete db contents");
e.printStackTrace();
}
clearTable 看起来像:
public static void clearTable()
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try{
transaction = session.beginTransaction();
session.createQuery("delete from metadataPoC.hib.TestHib").executeUpdate();
transaction.commit();
}catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
所有相当简单的东西,但我的控制台输出仅此而已:
deleting db contents
我不确定为什么只是简单的从类名中删除
可能会导致此类问题。该程序刚刚挂起 10 分钟,没有失败,也没有 junit 测试结果。
编辑
但是,我已将问题隔离到clearTable() 方法,将其在代码中移动,一旦输入,就会挂起! 我不太清楚为什么,从我在网上找到的信息来看,我的方法似乎是正确的。但一定是这个方法。我将它放在我的 @BeforeClass
中,测试在到达它时就冻结了。
底线是clearTable()方法有问题。你我不知道是什么:(
编辑2
将问题追踪到命令的executeupdate部分,无论出于何种原因,一旦到达:
ParseUtil.encodePath(String, boolean) line: 100,它就会停止
From my junit test I'm calling a simple method to clear my table:
@AfterClass
public static void runAfterClass() {
//Clear db contents after tests
// NOTE this clears ALL data from the table
try {
System.out.println("deleting db contents");
HibFunction.clearTable();
} catch (Exception e) {
System.out.println("Couldnt delete db contents");
e.printStackTrace();
}
The clearTable looks like:
public static void clearTable()
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try{
transaction = session.beginTransaction();
session.createQuery("delete from metadataPoC.hib.TestHib").executeUpdate();
transaction.commit();
}catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
All fairly simple stuff however my console output goes no further than:
deleting db contents
I am unsure why exactly a simple delete from classname
can cause such issues. The programme has just been hanging for 10mins with no failure or result from the junit test.
Edit
However I've isolated the problem to the clearTable() method moved it around in the code and as soon as it entered, hangs!
I'm not exactly sure why exactly, from the info I've found online my method seems correct from what I can see. But it has to be this method. I placed it in my @BeforeClass
and the test just frozen when it got to it.
Bottom line there is a problem with the clearTable() method. Thou I've no idea what :(
EDIT 2
tracked down the problem to the executeupdate portion of the command for whatever reason it just stalls once it gets to the:
ParseUtil.encodePath(String, boolean) line: 100
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是
clearTable()
方法的事务和测试方法的事务之间出现死锁。确保在离开测试方法时完成(提交或回滚)所有事务。
It's probably a deadlock between transaction of
clearTable()
method and transaction of the test method.Make sure that you complete (commit or rollback) all transactions when leaving the test method.