由于关闭了连接,使用JPA2(Hibernate + Postgres)检索BLOB失败

时间:2018-08-30 09:21:13

标签: postgresql hibernate jpa blob

我正在尝试通过流检索 (为了节省内存)一个BLOB,该BLOB正确地作为bytea存储在Postgres中。我的实体显示为:

   public class VirtualFile { 

     @Id
     @Column(length = 36)
     @javax.persistence.GeneratedValue(generator = "system-uuid")
     @org.hibernate.annotations.GenericGenerator(name = "system-uuid", strategy = "uuid.hex")
     private String id;
     public String getId() { return id; }
     public void setId(String id) { this.id = id; }

     @Lob()
     @Basic(fetch = FetchType.LAZY)
     @Column(name="content")
     private java.sql.Blob content;
     public java.sql.Blob getContent() { return content; }
     public void setContent(java.sql.Blob content) { this.content = content; }

  }

Mu代码如下:

...
EntityManager em = ... // get the entity manager
VirtualFileDAO dao = ... // get the VirtualFile repository
EntityTransaction et = em.getTransaction();
OutputStream out = new ByteArrayOutputStream();

et.begin();
Optional<VirtualFile> f2 = dao.findById("ff8081816587b100016587b108a30000"); 
if (f2.isPresent()) {
  InputStream in = f2.get().getContent().getBinaryStream(); // Exception here
  IOUtils.copy(in, out); 
}
em.getTransaction().commit();
...

然后出现以下异常:

org.postgresql.util.PSQLException:此连接已关闭

at org.postgresql.jdbc2.AbstractJdbc2Connection.checkClosed(AbstractJdbc2Connection.java:714)
at org.postgresql.jdbc2.AbstractJdbc2Connection.getAutoCommit(AbstractJdbc2Connection.java:680)
at org.postgresql.fastpath.Fastpath.fastpath(Fastpath.java:74)
at org.postgresql.fastpath.Fastpath.fastpath(Fastpath.java:114)
at org.postgresql.fastpath.Fastpath.getInteger(Fastpath.java:126)
at org.postgresql.largeobject.LargeObject.<init>(LargeObject.java:93)
at org.postgresql.largeobject.LargeObject.copy(LargeObject.java:98)
at org.postgresql.jdbc2.AbstractJdbc2BlobClob.getBinaryStream(AbstractJdbc2BlobClob.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.hibernate.engine.jdbc.SerializableBlobProxy.invoke(SerializableBlobProxy.java:60)
at com.sun.proxy.$Proxy65.getBinaryStream(Unknown Source)
at net.cbsolution.ps.quotty.domain.VirtualFileTest.test2(VirtualFileTest.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

如果我使用字节数组,则一切正常。任何可能的原因的想法?

0 个答案:

没有答案