org.hibernate.AssertionFailure [英] org.hibernate.AssertionFailure

查看:221
本文介绍了org.hibernate.AssertionFailure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  2011年6月25日09:05:22,339错误断言失败:45  - 断言失败发生(这可能表示Hibernate中存在错误,但更可能是由于会话的不安全使用)
org.hibernate.AssertionFailure:com.inrev.bm.bean.IRKeyWordTweet条目中的空id(不要刷新发生异常后的会话)
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:78)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:
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 o rg.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;
尝试
{
for(Iterator itrList = statusToInsert.iterator(); itrList.hasNext();)
{
try
{
IRecord记录=(IRecord)itrList.next();
session.save(record);
count ++;
if(count%10 == 0)
{
session.flush();
session.clear();
tx.commit();
tx = session.beginTransaction();


$ b catch(例外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());


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

问候,

Rohit

解决方案

问题是您的代码正在处理一个异常,这是一个糟糕的事情,在99.9%发生以下情况:

其中一个与会话的交互在try块中失败,并引发异常。发生这种情况时,会话无效,因为它处于不一致的状态,因此无法将用于绝对任何事情。但是你的代码与catch块中的会话交互,触发断言。

在会话异常之后唯一安全的做法是回滚事务并关闭它。任何其他类型的交互可能会产生另一个异常(在这种情况下是断言异常)。


I am getting this strange error sometimes while my thread executes. What could this be related to?

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)

My code for inserting,

    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();
    }

Regards,

Rohit

解决方案

The problem is that your code is handling an Exception, which is a bad, bad thing to do in 99.9% of the cases, and the following is happening:

One of the interactions with the session fails in the try block, and throws an exception. When this happens, the session is invalidated and cannot be used for absolutely anything as it's in an inconsistent state. But your code interacts with the session in the catch block, which triggers the assertion.

The only safe thing to do after an exception with the session, is to rollback the transaction and close it. Any other type of interaction will probably generate another exception (in this case an assertion exception).

这篇关于org.hibernate.AssertionFailure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆