【问题标题】:Hibernate + Resteasy + Blobstream休眠 + Resteasy + Blobstream
【发布时间】:2012-11-21 04:05:24
【问题描述】:

我正在使用 resteasy 并通过休眠保存 blob

我已经在视图中配置了打开会话

我的 rest-handler 是非事务性的,我检索 java.sql.blobs 的服务是事务性的。

问题:在服务中检索 blob 后 jdbc-connection 被 HibernateTransactionManager:doCleanupAfterCompletion 关闭(休眠会话未关闭)

所以 rest 以后不能读取 blob 流,因为它只有在同一个 jdbc-connection 仍然打开时才有效

如何在事务性服务功能之后休眠教不关闭 jdbc 连接?

它应该在会话关闭时关闭它

【问题讨论】:

  • 为什么你认为你不能从另一个连接中读取 blob?
  • 因为当我尝试读取时出现异常“连接已关闭”

标签: hibernate session blob transactional


【解决方案1】:

我通过为 Blob 创建一个 Provider 来解决这个问题,采用 InputSTream-Provider

@Provider
@Produces("*/*")
public class BlobProvider implements MessageBodyWriter<Blob> {

    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return Blob.class.isAssignableFrom(type);
    }

    public long getSize(Blob blob, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

    public void writeTo(Blob blob, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
        SessionFactory sessionFactory = (SessionFactory) Utils.getBean("sessionFactory");
        sessionFactory.getCurrentSession().beginTransaction();
        InputStream inputStream = null;
        try {
            inputStream = blob.getBinaryStream();
            int c = inputStream.read();
            if (c == -1) {
                httpHeaders.putSingle(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(0));
                entityStream.write(new byte[0]); // fix RESTEASY-204
                return;
            } else
                entityStream.write(c);
            ProviderHelper.writeTo(inputStream, entityStream);
        } catch (SQLException e) {
            Utils.logError(e);
        } finally {
            if (inputStream != null)
                inputStream.close();
        }
        sessionFactory.getCurrentSession().getTransaction().commit();
    }
}

【讨论】:

    猜你喜欢
    • 2011-12-16
    • 2021-10-21
    • 2015-02-08
    • 2019-04-18
    • 2017-02-06
    • 2017-05-18
    • 2017-09-19
    • 2013-10-19
    • 2010-10-14
    相关资源
    最近更新 更多