偶尔会出现HibernateException

时间:2019-06-18 09:49:31

标签: java hibernate spring-data-jpa spring-transactions

我尝试同时修改约14个REST实体。

前几次修改是成功的(中间的一些),而另一些则偶尔会因HibernateException而失败。

这是我的servlet:

public class ReSTAPIFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
            HttpServletResponse res = (HttpServletResponse) response;
            HttpServletRequest req = (HttpServletRequest) request;

            AnnotationConfigWebApplicationContext ctx = (AnnotationConfigWebApplicationContext) request
                    .getServletContext()
                    .getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.Restful-Spring");
            RESTEntityModifier bean = ctx.getBean(RESTEntityModifier.class);
            if (bean.canHandle(req)) {
                try {
                    bean.handle(req, res);
                    return;
                } catch (Exception e) {
                    System.out.println(
                            req.getRequestURI() + "-T" + Thread.currentThread().hashCode() + " - " + e.getMessage());
                    return;
                }
            }
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}

这是处理方法:

@Transactional
public void handle(HttpServletRequest r, HttpServletResponse response) throws Exception {
    LOG.fine("Handle " + r.getMethod() + " " + r.getRequestURL() + " - Transaction: "
            + TransactionAspectSupport.currentTransactionStatus() + " T" + Thread.currentThread().hashCode());
    entityManager.persist(...);

这是日志记录输出:

2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle POST https://localhost/X/rest/BuildResult - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@3b6dbbb5 T1440613991
2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@51269d65 T1440613991
2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@74e3d56b T590958110
2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@2d07e97b T1576512215
2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@3652c837 T590958110
2019-06-18 11:32:15 INFO org.hibernate.event.internal.DefaultLoadEventListener doOnLoad HHH000327: Error performing load command : org.hibernate.HibernateException: Unable to access lob stream
2019-06-18 11:32:15 INFO org.hibernate.event.internal.DefaultLoadEventListener doOnLoad HHH000327: Error performing load command : org.hibernate.HibernateException: Unable to access lob stream
2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@684c4367 T590958110
/x/rest/buildResult/202/params-T1440613991 - Unable to access lob stream; nested exception is org.hibernate.HibernateException: Unable to access lob stream
/x/rest/buildResult/202/params-T1576512215 - Unable to access lob stream; nested exception is org.hibernate.HibernateException: Unable to access lob stream
2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@571090c6 T1395592956
2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@3b12eb0 T1440613991
2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@5dd9b196 T590958110
2019-06-18 11:32:15 INFO org.hibernate.event.internal.DefaultLoadEventListener doOnLoad HHH000327: Error performing load command : org.hibernate.HibernateException: Unable to access lob stream
/x/rest/buildResult/202/params-T1395592956 - Unable to access lob stream; nested exception is org.hibernate.HibernateException: Unable to access lob stream
2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@1ac1fbe9 T1395592956
2019-06-18 11:32:15 INFO org.hibernate.event.internal.DefaultLoadEventListener doOnLoad HHH000327: Error performing load command : org.hibernate.HibernateException: Unable to access lob stream
/x/rest/buildResult/202/params-T1440613991 - Unable to access lob stream; nested exception is org.hibernate.HibernateException: Unable to access lob stream
2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@ba7efb6 T1440613991
2019-06-18 11:32:15 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@7dabe5fc T590958110
2019-06-18 11:32:15 INFO org.hibernate.event.internal.DefaultLoadEventListener doOnLoad HHH000327: Error performing load command : org.hibernate.HibernateException: Unable to access lob stream
/x/rest/buildResult/202/params-T1395592956 - Unable to access lob stream; nested exception is org.hibernate.HibernateException: Unable to access lob stream
2019-06-18 11:32:16 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@5df4cf5e T1395592956
2019-06-18 11:32:16 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@279dc900 T590958110
2019-06-18 11:32:16 INFO org.hibernate.event.internal.DefaultLoadEventListener doOnLoad HHH000327: Error performing load command : org.hibernate.HibernateException: Unable to access lob stream
/x/rest/buildResult/202/params-T1395592956 - Unable to access lob stream; nested exception is org.hibernate.HibernateException: Unable to access lob stream
2019-06-18 11:32:16 INFO org.hibernate.event.internal.DefaultLoadEventListener doOnLoad HHH000327: Error performing load command : org.hibernate.HibernateException: Unable to access lob stream
2019-06-18 11:32:16 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@fa7fdbd T1395592956
/x/rest/buildResult/202/params-T1440613991 - Unable to access lob stream; nested exception is org.hibernate.HibernateException: Unable to access lob stream
2019-06-18 11:32:16 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@b0f4477 T1440613991
2019-06-18 11:32:16 FEIN x.web.rm.RESTEntityModifier handle Handle PUT https://localhost/x/rest/buildResult/202/params - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@4026ebe4 T384544935
2019-06-18 11:32:16 INFO org.hibernate.event.internal.DefaultLoadEventListener doOnLoad HHH000327: Error performing load command : org.hibernate.HibernateException: Unable to access lob stream
/x/rest/buildResult/202/params-T1395592956 - Unable to access lob stream; nested exception is org.hibernate.HibernateException: Unable to access lob stream
2019-06-18 11:32:16 INFO org.hibernate.event.internal.DefaultLoadEventListener doOnLoad HHH000327: Error performing load command : org.hibernate.HibernateException: Unable to access lob stream
/x/rest/buildResult/202/params-T1440613991 - Unable to access lob stream; nested exception is org.hibernate.HibernateException: Unable to access lob stream
2019-06-18 11:32:16 FEIN x.web.rm.RESTEntityModifier handle Handle PATCH https://localhost/x/buildResult/202/finished - Transaction: org.springframework.transaction.support.DefaultTransactionStatus@415fd4ad T384544935

我使用hibernate-envers:5.4.3-final,Hibernate-core:5.4.3-final和Spring-tx-5.1.7-Release

这些请求成功:

  • 线程#1440613991
  • 线程#1576512215
  • 线程#1395592956

所有其他请求均成功。

这是堆栈跟踪:

org.springframework.orm.jpa.JpaSystemException: Unable to access lob stream; nested exception is org.hibernate.HibernateException: Unable to access lob stream
        at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:407)
        at org.springframework.orm.jpa.DefaultJpaDialect.translateExceptionIfPossible(DefaultJpaDialect.java:127)
        at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
        at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:545)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:534)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:305)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
        at X.RESTEntityModifier$$EnhancerBySpringCGLIB$$b9cd3ce3.handle(<generated>)
        at X.ReSTAPIFilter$1.run(ReSTAPIFilter.java:39)
Caused by: org.hibernate.HibernateException: Unable to access lob stream
        at org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor.wrap(PrimitiveByteArrayTypeDescriptor.java:123)
        at org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor.wrap(PrimitiveByteArrayTypeDescriptor.java:26)
        at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$1.doExtract(BlobTypeDescriptor.java:48)
        at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243)
        at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:329)
        at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:3014)
        at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.loadFromResultSet(EntityReferenceInitializerImpl.java:305)
        at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.hydrateEntityState(EntityReferenceInitializerImpl.java:233)
        at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:103)
        at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:254)
        at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122)
        at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
        at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
        at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:197)
        at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4278)
        at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:480)
        at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:450)
        at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:201)
        at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)
        at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:103)
        at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:71)
        at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1286)
        at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1163)
        at org.hibernate.internal.ExceptionConverterImpl.wrapStaleStateException(ExceptionConverterImpl.java:220)
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:93)
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
        at org.hibernate.internal.ExceptionConverterImpl.convertCommitException(ExceptionConverterImpl.java:65)
        at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:107)
        at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:532)
        ... 9 more
Caused by: org.postgresql.util.PSQLException: LargeObjects (LOB) dürfen im Modus 'auto-commit' nicht verwendet werden.
        at org.postgresql.largeobject.LargeObjectManager.open(LargeObjectManager.java:265)
        at org.postgresql.largeobject.LargeObjectManager.open(LargeObjectManager.java:251)
        at org.postgresql.jdbc.AbstractBlobClob.getLo(AbstractBlobClob.java:283)
        at org.postgresql.jdbc.AbstractBlobClob.getBinaryStream(AbstractBlobClob.java:127)
        at org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor.wrap(PrimitiveByteArrayTypeDescriptor.java:120)
        ... 40 more

为什么有些不成功?

1 个答案:

答案 0 :(得分:0)

我发现了一个被抑制的异常:

它指出异常是在hibernate或spring-tx内部的一个错误,它隐藏了原始异常(行同时更新了问题)。

enter image description here

所以答案是将它们放入同步方法中

private synchronized void handle(HttpServletResponse res, 
                 HttpServletRequest req, RESTEntityModifier bean) throws Exception {
    bean.handle(req, res);
}