执行 pg_notify BatchUpdateException 错误 0

Posted

技术标签:

【中文标题】执行 pg_notify BatchUpdateException 错误 0【英文标题】:PERFORM pg_notify BatchUpdateException error 0 【发布时间】:2017-05-15 07:06:15 【问题描述】:

我正在使用休眠在表“Stock”中插入多条记录,该表具有调用 PERFORM pg_notify 事件的触发器,但是在通过休眠执行批量更新时它给了我 “BatchUpdateException 错误 0”

当我在 stock 表中创建 PERFORM pg_notify 触发器时,它开始引发此错误,任何人都可以帮助我解决这个问题。

在表格上应用的触发器如下:

DROP TRIGGER stock_change_notifier ON myschema.stock;

CREATE TRIGGER stock_change_notifier
  AFTER UPDATE
  ON myschema.stock
  FOR EACH ROW
  EXECUTE PROCEDURE myschema.notify_change();

notify_change()函数包含如下:

BEGIN
        IF (TG_OP = 'DELETE') THEN
            sku_id = OLD.sku_id;
        ELSE
            sku_id = NEW.sku_id;
        END IF;

        notification = json_build_object(
                          'table',TG_TABLE_NAME,
                          'action', TG_OP,
                          'sku_id', sku_id,
                          'schema',TG_TABLE_SCHEMA);


        PERFORM pg_notify('stock',notification::text);

        RETURN NULL; 
    END;

仅供参考,我正在使用带有休眠功能的 POSTGRES..

已编辑:e.printStackTrace()

org.hibernate.exception.GenericJDBCException: could not execute batch
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
    at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:119)
    at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.doExecuteBatch(BatchingBatch.java:97)
    at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.execute(AbstractBatchImpl.java:147)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.getBatch(JdbcCoordinatorImpl.java:203)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3011)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2948)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3328)
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:145)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:451)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:336)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:335)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1224)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:464)
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2905)
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2281)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:230)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
    at com.brahmin.wms.persistance.ShopifyOrdersDao.synchShopifOrdersToWms(ShopifyOrdersDao.java:225)
    at com.brahmin.wms.persistance.ShopifyDAO.updateShopifySetting(ShopifyDAO.java:614)
    at com.brahmin.wms.service.ShopifyService.updateShopifySetting(ShopifyService.java:133)
    at com.brahmin.wms.controller.NewShopifyController.updateShopifySetting(NewShopifyController.java:223)
    at com.brahmin.wms.controller.NewShopifyController$$FastClassBySpringCGLIB$$bfc84183.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:718)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
    at com.brahmin.wms.aspect.UserAspect.aroundMethod(UserAspect.java:50)
    at sun.reflect.GeneratedMethodAccessor110.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:654)
    at com.brahmin.wms.controller.NewShopifyController$$EnhancerBySpringCGLIB$$a9c57ee0.updateShopifySetting(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at com.brahmin.wms.filter.CORSFilter.doFilterInternal(CORSFilter.java:34)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:133)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.BatchUpdateException: Batch entry 0 update wmsdb00343.orders set created_date='2017-05-15 13:01:39.680000 +05:30:00', customer_name='Shopify', financial_status='paid', integration_shopify_id=22, link_flag='1', modified_date='2017-05-15 13:01:45.984000 +05:30:00', order_date='2017-05-12 14:57:32.000000 +05:30:00', outbound_order_number='SO0143', shopify_order_id=4825985993, shopify_order_number='#1017', shopify_order_status='CF', status='CF', total_price='45.00', wms_order_id=146 where shopify_sku_order_id=219 was aborted.  Call getNextException to see the cause.
    at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2762)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1891)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:405)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2909)
    at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:110)
    ... 121 more

上述错误显示订单更新失败,但如果我删除“库存”表中的触发器,它可以正常工作。这整个更新有不同的更新表,包括“股票”表更新。但是,如果我如上所述删除库存触发器,它再次起作用。

已编辑:getNextException:

我已通过更改为 GenericJDBCException 在异常中执行以下操作。

org.hibernate.exception.GenericJDBCException be=(org.hibernate.exception.GenericJDBCException)e;
java.sql.SQLException sqe= be.getSQLException();
sqe.getNextException();

并得到以下异常:

 (org.postgresql.util.PSQLException) org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block

进一步 getNextException 给了我:

Evaluations must contain either an expression or a block of well-formed statements

已编辑:通过 SQL ERROR MSG 进行多次更新:

执行此操作时:

update wmsdb00343.stock set  available_qty='1.00' , allocated_qty='1.00' where stock_id=229;
update wmsdb00343.stock set  available_qty='2.00' , allocated_qty='1.00' where stock_id=230;
update wmsdb00343.stock set  available_qty='3.00' , allocated_qty='1.00' where stock_id=231;
update wmsdb00343.stock set  available_qty='4.00' , allocated_qty='1.00' where stock_id=232;
update wmsdb00343.stock set  available_qty='5.00' , allocated_qty='1.00' where stock_id=233;
update wmsdb00343.stock set  available_qty='6.00' , allocated_qty='1.00' where stock_id=228;

PSQL 中的错误消息:

ERROR:  function json_build_object(unknown, name, unknown, text, unknown, bigint, unknown, name) does not exist
LINE 1: SELECT json_build_object(
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
QUERY:  SELECT json_build_object(
                          'table',TG_TABLE_NAME,
                          'action', TG_OP,
                          'sku_id', sku_id,
                          'schema',TG_TABLE_SCHEMA)
CONTEXT:  PL/pgSQL function wmsdb00343.notify_change() line 20 at assignment
********** Error **********

ERROR: function json_build_object(unknown, name, unknown, text, unknown, bigint, unknown, name) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Context: PL/pgSQL function wmsdb00343.notify_change() line 20 at assignment

【问题讨论】:

您需要向我们展示完整的堆栈跟踪。理想情况下是原始 Postgres 错误消息或异常,而不是来自您的混淆层(又名“ORM”)的错误消息或异常 @RajuSharma: 真正的错误仍然丢失(“调用 getNextException 查看原因”) 混淆层的乐趣。只要我们没有看到 real 错误消息,这是不可能回答的。当您在 SQL 客户端中手动运行该更新时会发生什么? 触发器对我来说工作得很好(使用 SQL 客户端)。错误消息“current transaction is aborted”表示执行更新之前出现了问题。消息“Evaluations must contain a expression or a block of well-formed statements”不是 Postgres 错误消息。 您使用的是哪个 Postgres 版本? json_build_object() 在 9.4 中引入 【参考方案1】:

正如@a_horse_with_no_name 所提到的,当我从 psql (PostgreSQL) 9.3.15 更新到 9.6 时,它工作正常,因为在 9.4 版本中引入了“json_build_object”。

谢谢@a_horse_with_no_name

【讨论】:

以上是关于执行 pg_notify BatchUpdateException 错误 0的主要内容,如果未能解决你的问题,请参考以下文章

在 postgresql 中为每个更新的行调用一个函数

并行执行串行执行和并发执行

编译执行和解释执行的区别

jvm的解释执行与编译执行

Jmeter--多个线程组顺序执行和并行执行

小记---------手动执行脚本正常执行,使用crontab定时执行时 不执行