org.hibernate.HibernateException: 無法存取 lob stream

2022-09-01 06:53:31

我正在使用休眠3.6.7从我的类到oracle表进行映射。我通常不会得到这个例外:

org.hibernate.HibernateException: Unable to access lob stream
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
    at java.util.concurrent.FutureTask.get(FutureTask.java:83)
    at com.mycompany.TransferFiles.TransferFilesToHadoop(TransferFiles.java:85)
    at com.mycompany.TransferJob.execute(TransferJob.java:25)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
 Caused by: org.hibernate.HibernateException: Unable to access lob stream
       at org.hibernate.type.descriptor.java.ClobTypeDescriptor.unwrap(ClobTypeDescriptor.java:117)
    at org.hibernate.type.descriptor.java.ClobTypeDescriptor.unwrap(ClobTypeDescriptor.java:46)
    at org.hibernate.type.descriptor.sql.ClobTypeDescriptor$3$1.doBind(ClobTypeDescriptor.java:83)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:91)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:283)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:278)
    at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:89)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2184)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2558)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2494)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2821)
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:113)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at com.mycompany.HibernateUtil.getSession(HibernateUtil.java:36)
    at com.mycompany.BasicDaoImpl.saveOrUpdate(BasicDaoImpl.java:34)
    at com.mycompany.FileCopyRoutine.call(TransferFiles.java:297)
    at com.mycompany..FileCopyRoutine.call(TransferFiles.java:225)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
 Caused by: java.sql.SQLException: Closed Connection
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
    at oracle.sql.CLOB.getDBAccess(CLOB.java:1196)
    at oracle.sql.CLOB.getCharacterStream(CLOB.java:278)
    at sun.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.hibernate.engine.jdbc.SerializableClobProxy.invoke(SerializableClobProxy.java:74)
    at $Proxy2.getCharacterStream(Unknown Source)
    at org.hibernate.type.descriptor.java.ClobTypeDescriptor.unwrap(ClobTypeDescriptor.java:114)

当我调用 saveOrUpdate 方法时。

任何人都可以给我一些建议吗?

public static synchronized Session getSession() throws DBConnectionException{

    Session session = threadSession.get();

    if(session==null||!session.isOpen()||!session.isConnected()){
        if(sessionFactory==null||sessionFactory.isClosed())initHibernate();
        try{
            session = sessionFactory.openSession();
        }catch(Exception e){
            throw new DBConnectionException("Exception caught when trying to generate Hibernate session.");
        }
        threadSession.set(session);
    }
    if(session.isDirty()){
        session.flush();(36 line of HibernateUtil)
    }
    session.clear();
    return session;
}

我用了很长时间。

现在我正在并行传输文件,然后使用saveOrUpdate方法更新数据库。通过有很多线程,我在我的代码中对这部分进行了随机化。我认为我们一直与数据库有连接,但它提供了闭合连接的借口。任何人都可以给我一些关于这个例外的建议。问题应该是什么?

我认为它可以因为使用saveOrUpdate,所以我改为合并,但它具有相同的问题。

Caused by: org.hibernate.HibernateException: Unable to access lob stream
    at org.hibernate.type.descriptor.java.ClobTypeDescriptor.unwrap(ClobTypeDescriptor.java:117)
    at org.hibernate.type.descriptor.java.ClobTypeDescriptor.unwrap(ClobTypeDescriptor.java:46)
    at org.hibernate.type.descriptor.sql.ClobTypeDescriptor$3$1.doBind(ClobTypeDescriptor.java:83)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:91)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:283)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:278)
    at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:89)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2184)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2558)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2494)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2821)
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:113)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at mycompany.jobsrc.BasicDaoImpl.merge(BasicDaoImpl.java:52)
    at mycompany.FileCopyRoutine.call(TransferFiles.java:301)
    at mycompany.FileCopyRoutine.call(TransferFiles.java:226)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
   Caused by: java.sql.SQLException: Closed Connection
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
    at oracle.sql.CLOB.getDBAccess(CLOB.java:1196)
    at oracle.sql.CLOB.getCharacterStream(CLOB.java:278)
    at sun.reflect.GeneratedMethodAccessor36.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.hibernate.engine.jdbc.SerializableClobProxy.invoke(SerializableClobProxy.java:74)
    at $Proxy2.getCharacterStream(Unknown Source)
    at org.hibernate.type.descriptor.java.ClobTypeDescriptor.unwrap(ClobTypeDescriptor.java:114)
    ... 27 more

我的合并代码:

public void merge(T t) throws DBConnectionException{
    Session session = HibernateUtil.getSession();
    Transaction transaction = session.beginTransaction();
    session.merge(t);
    transaction.commit();
}

答案 1

尝试在尝试访问 lob 字段对象的服务方法上添加@Transactional。它对我有用。


答案 2

我相信我以前有过这个。

从 Oracle 加载大型对象(CLOB、BLOB)时,它将作为对流的引用返回。如果记录映射到休眠状态,并且在加载对象后关闭会话(因此对象被分离),则映射到的 java 字段 blob 尚未包含数据;因此,当您尝试将其保存回去时,它无法与它一起使用,因为 clob/blob 流已消失。

首先,尝试在同一会话中继续加载和保存。

如果我没记错的话,另一种解决方法是在加载会话仍处于打开状态时“触摸”clob/bloc java 字段。然后,将从数据库中读取数据。但它的效率非常低下(它是有原因的,即它很大)。

另一种选择是将小数据和 lob 拆分为两个不同的表,并仅在需要时加载 lob。

希望这对您有所帮助。


推荐