gpt4 book ai didi

hibernate - org.hibernate.AssertionFailure

转载 作者:行者123 更新时间:2023-12-02 21:59:32 24 4
gpt4 key购买 nike

有时我的线程执行时会遇到这个奇怪的错误。这可能与什么有关?

2011-Jun-25 09:05:22,339 ERROR AssertionFailure:45 - an assertion failure occured (this             may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: null id in com.inrev.bm.bean.IRKeyWordTweet entry (don't flush the Session after an exception occurs)
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:78)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:187)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:49)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.inrev.bm.streaming.IRKeyWordStreaminThread.run(IRKeyWordStreaminThread.java:119)

我的插入代码,

    Transaction tx = null;
Session session = sessionFactory.openSession();
tx = session.beginTransaction();

int count = 0;
try
{
for (Iterator itrList = statusToInsert.iterator(); itrList.hasNext();)
{
try
{
IRecord record = (IRecord) itrList.next();
session.save(record);
count++;
if ( count % 10 == 0 )
{
session.flush();
session.clear();
tx.commit();
tx = session.beginTransaction();
}

}
catch (Exception e)
{
tx.commit();
session.close();
session = sessionFactory.openSession();
tx = session.beginTransaction();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
log.error(sw.toString());
}
}
}
catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
log.error(sw.toString());
}
finally {
tx.commit();
session.close();
}

问候,

罗希特

最佳答案

问题在于您的代码正在处理异常,这在 99.9% 的情况下都是非常非常糟糕的事情,并且正在发生以下情况:

与 session 的交互之一在 try block 中失败,并引发异常。发生这种情况时, session 将失效,并且不能用于任何用途,因为它处于不一致的状态。但是您的代码与 catch block 中的 session 进行交互,从而触发断言。

session 发生异常后唯一安全的做法是回滚事务并关闭它。任何其他类型的交互都可能会生成另一个异常(在本例中为断言异常)。

关于hibernate - org.hibernate.AssertionFailure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6477054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com