开发者

Get stream from java.sql.Blob in Hibernate

I'm trying to use hibernate @Entity with java.sql.Blob to store some binary data. Storing doesn't throw any exceptions (however, I'm not sure if it really stores the bytes), but reading does. Here is my test:

@Test
public void shouldStoreBlob() {
    InputStream readFile = getClass().getResourceAsStream("myfile");

    Blob blob = dao.createBlob(readFile, readFile.available());
    Ent ent = new Ent();
    ent.setBlob(blob);

    em.persist(ent);
    long id = ent.getId();

    Ent fromDb = em.find(Ent.class, id);

    //Exception is thrown from getBinaryStream()
    byte[] fromDbBytes = IOUtils.toByteArray(fromDb.getBlob().getBinaryStream());
}

So it throws an exception:

java.sql.SQLException: could not reset reader
    at org.hibernate.engine.jdbc.BlobProxy.getStream(BlobProxy.java:86)
    at org.hibernate.engine.jdbc.BlobProxy.invoke(BlobProxy.java:108)
    at $Proxy81.getBinaryStream(Unknown Source)
    ...

Why? Sh开发者_Python百科ouldn't it read bytes form DB here? And what can I do for it to work?


Try to refresh entity:

em.refresh(fromDb);

Stream will be reopened. I suspect that find(...) is closing the blob stream.


It is not at all clear how you are using JPA here, but certainly you do not need to deal with Blob data type directly if you are using JPA.

You just need to declare a field in the entity in question of @Lob somewhat like this:

@Lob
@Basic(fetch = LAZY)
@Column(name = "image")
private byte[] image;

Then, when you retrieve your entity, the bytes will be read back again in the field and you will be able to put them in a stream and do whatever you want with them.

Of course you will need a getter and setter methods in your entity to do the byte conversion. In the example above it would be somewhat like:

private Image getImage() {
    Image result = null;

    if (this.image != null && this.image.length > 0) {
        result = new ImageIcon(this.image).getImage();
    }

    return result;
}

And the setter somewhat like this

private void setImage(Image source) {

   BufferedImage buffered = new BufferedImage(source.getWidth(null), source.getHeight(null), BufferedImage.TYPE_INT_RGB);
   Graphics2D g = buffered.createGraphics();
   g.drawImage(source, 0, 0, null);
   g.dispose();

   ByteArrayOutputStream stream = new ByteArrayOutputStream();
   try {
      ImageIO.write(buffered, "JPEG", stream);
      this.image = stream.toByteArray();
    }
    catch (IOException e) {
      assert (false); // should never happen
    }
}

}


You need to set a breakpoint on method org.hibernate.engine.jdbc.BlobProxy#getStream on line stream.reset() and examine a reason of IOException:

    private InputStream getStream() throws SQLException {
            try {
                    if (needsReset) {
                            stream.reset(); // <---- Set breakpoint here
                    }
            }
            catch ( IOException ioe) {
                    throw new SQLException("could not reset reader");
            }
            needsReset = true;
            return stream;
    }

In my case the reason of IOException was in usage of org.apache.commons.io.input.AutoCloseInputStream as a source for Blob:

InputStream content = new AutoCloseInputStream(stream);
...
Ent ent = new Ent();
...
Blob blob = Hibernate.getLobCreator(getSession()).createBlob(content, file.getFileSize())
ent.setBlob(blob);
em.persist(ent);

While flushing a Session hibernate closes Inpustream content (or rather org.postgresql.jdbc2.AbstractJdbc2Statement#setBlob closes Inpustream in my case). And when AutoCloseInputStream is closed - it rases an IOException in method reset()

update In your case you use a FileInputStream - this stream also throws an exception on reset method. There is a problem in test case. You create blob and read it from database inside one transaction. When you create Ent, Postgres jdbc driver closes InputStream while flushing a session. When you load Ent (em.find(Ent.class, id)) - you get the same BlobProxy object, that stores already closed InputStream.

Try this:

TransactionTemplate tt;

@Test
public void shouldStoreBlob() {
    final long id = tt.execute(new TransactionCallback<long>()
    {
        @Override
        public long doInTransaction(TransactionStatus status)
        {
            try
            {
                InputStream readFile = getClass().getResourceAsStream("myfile");

                Blob blob = dao.createBlob(readFile, readFile.available());
                Ent ent = new Ent();
                ent.setBlob(blob);

                em.persist(ent);
                return ent.getId();
            }
            catch (Exception e)
            {
                return 0;
            }
        }
    });

    byte[] fromStorage = tt.execute(new TransactionCallback<byte[]>()
    {
        @Override
        public byte[] doInTransaction(TransactionStatus status)
        {
            Ent fromDb = em.find(Ent.class, id);
            try
            {
                return IOUtils.toByteArray(fromDb.getBlob().getBinaryStream());
            }
            catch (IOException e)
            {
                return new byte[] {};
            }
        }
    });
}


My current and only solution is closing the write session and opening new Hibernate session to get back the streamed data. It works. However I do not know what is the difference. I called inputStream.close(), but that was not enough.

Another way: I tried to call free() method of blob after session.save(attachment) call too, but it throws another exception:

Exception in thread "main" java.lang.AbstractMethodError: org.hibernate.lob.SerializableBlob.free()V
 at my.hibernatetest.HibernateTestBLOB.storeStreamInDatabase(HibernateTestBLOB.java:142)
 at my.hibernatetest.HibernateTestBLOB.main(HibernateTestBLOB.java:60)

I am using PostgreSQL 8.4 + postgresql-8.4-702.jdbc4.jar, Hibernate 3.3.1.GA


Is the method IOUtils.toByteArray closing the input stream?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜