2012-12-03 26 views
0

我使用RestEasy的,節約使用Hibernate的斑點休眠+ RestEasy的+ Blobstream

我在視圖中配置的公開會議

我休息,處理是非事務,我的服務,檢索的java .sql.blobs是事務性的。

的問題:在JDBC連接由HibernateTransactionManager來關閉該服務檢索BLOB之後:doCleanupAfterCompletion(Hibernate的Session未閉)

所以休息以後不能讀取BLOB流,因爲它只有在相同的jdbc連接仍然打開的情況下才有效

我如何能夠在事務服務函數之後教會不關閉jdbc連接?

應該關閉會話時通過創建一個斑點封閉提供商

+0

爲什麼你認爲你不能從另一個讀取BLOB連接? –

+0

,因爲當我嘗試從它讀取時,我得到異常「連接關閉」 – wutzebaer

回答

0

我解決了這個問題,adopded從InputStream的提供商

@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(); 
    } 
}