org.hibernate.engine.jdbc.BlobProxy # getStream stream.reset() IOException:
private InputStream getStream() throws SQLException {
try {
if (needsReset) {
stream.reset();
}
}
catch ( IOException ioe) {
throw new SQLException("could not reset reader");
}
needsReset = true;
return stream;
}
IOException org.apache.commons.io.input.AutoCloseInputStream 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);
Inpustream (, , org.postgresql.jdbc2.AbstractJdbc2Statement # setBlob Inpustream ). AutoCloseInputStream - IOException reset()
FileInputStream - reset.
. blob . Ent, jdbc Postgres InputStream, . Ent (em.find(Ent.class, id)), BlobProxy, InputStream.
:
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[] {};
}
}
});
}