当前位置: 首页 > 知识库问答 >
问题:

为什么对@Transactional注释执行“调用事务回滚”

张亦
2023-03-14

我对事务REST控制器中的一些异常处理感到困惑。

我有一个管理服务,看起来像:

@Override
public SearchedAdminsEvent search(SearchAdminsEvent searchAdminsEvent) {
    Page<Admin> admins = adminRepository.search(searchAdminsEvent.getSearchTerm(), searchAdminsEvent.getPageRequest());
    if (admins.getTotalElements() == 0) {
        throw new SearchedAdminsNotFoundException(searchAdminsEvent.getSearchTerm());
    } else {
        List<EventAdmin> eventAdmins = EventAdmin.newEventAdminsFrom(admins.getContent()); 
        return new SearchedAdminsEvent(searchAdminsEvent.getSearchTerm(), searchAdminsEvent.getPageRequest(), eventAdmins);
    }
}

我有一个REST控制器,看起来像:

@RequestMapping(value = "/search", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@Transactional(readOnly = true)
@ResponseBody
public ResponseEntity<List<AdminResource>> search(@RequestParam(value = "searchTerm", required = true) String searchTerm, @RequestParam(value = "page", required = true, defaultValue = "0") int page, @RequestParam(value = "size", required = true, defaultValue = "10") int size, UriComponentsBuilder builder) {
    HttpHeaders responseHeaders = new HttpHeaders();
    Pageable pageRequest = buildPageRequest(page, size);
    SearchedAdminsEvent searchedAdminsEvent;
    try {
        searchedAdminsEvent = adminService.search(new SearchAdminsEvent(searchTerm, pageRequest));            
    } catch (SearchedAdminsNotFoundException e) {
        return new ResponseEntity<List<AdminResource>>(responseHeaders, HttpStatus.NOT_FOUND);
    }
    responseHeaders.setLocation(builder.path("/admin/search").queryParam("searchTerm", searchTerm).queryParam("page", page).queryParam("size", size).buildAndExpand(searchedAdminsEvent.getSearchTerm()).toUri());
    List<AdminResource> adminResources = adminResourceAssembler.toResources(searchedAdminsEvent.getEventAdmins());
    ResponseEntity<List<AdminResource>> responseEntity = new ResponseEntity<List<AdminResource>>(adminResources, responseHeaders, HttpStatus.OK);
    return responseEntity;
}

它使用@Transactional(readOnly = true)注释。

如果存在此注释并且没有找到搜索的项目,则它会给出以下异常:

2014-06-30 16:22:39,736 DEBUG  [JpaTransactionManager] Participating transaction failed - marking existing transaction as rollback-only 
2014-06-30 16:22:39,736 DEBUG  [JpaTransactionManager] Participating transaction failed - marking existing transaction as rollback-only 
2014-06-30 16:22:39,736 DEBUG  [JpaTransactionManager] Participating transaction failed - marking existing transaction as rollback-only 
2014-06-30 16:22:39,736 DEBUG  [JpaTransactionManager] Setting JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1f9bfd9] rollback-only 
2014-06-30 16:22:39,736 DEBUG  [JpaTransactionManager] Setting JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1f9bfd9] rollback-only 
2014-06-30 16:22:39,736 DEBUG  [JpaTransactionManager] Setting JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1f9bfd9] rollback-only 
2014-06-30 16:22:39,737 DEBUG  [JpaTransactionManager] Initiating transaction commit 
2014-06-30 16:22:39,737 DEBUG  [JpaTransactionManager] Initiating transaction commit 
2014-06-30 16:22:39,737 DEBUG  [JpaTransactionManager] Initiating transaction commit 
2014-06-30 16:22:39,737 DEBUG  [JpaTransactionManager] Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1f9bfd9] 
2014-06-30 16:22:39,737 DEBUG  [JpaTransactionManager] Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1f9bfd9] 
2014-06-30 16:22:39,737 DEBUG  [JpaTransactionManager] Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1f9bfd9] 
2014-06-30 16:22:39,754 DEBUG  [JpaTransactionManager] Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@1f9bfd9] after transaction 
2014-06-30 16:22:39,754 DEBUG  [JpaTransactionManager] Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@1f9bfd9] after transaction 
2014-06-30 16:22:39,754 DEBUG  [JpaTransactionManager] Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@1f9bfd9] after transaction 
2014-06-30 16:22:39,755 DEBUG  [EntityManagerFactoryUtils] Closing JPA EntityManager 
2014-06-30 16:22:39,755 DEBUG  [EntityManagerFactoryUtils] Closing JPA EntityManager 
2014-06-30 16:22:39,755 DEBUG  [EntityManagerFactoryUtils] Closing JPA EntityManager 
2014-06-30 16:22:39,760 DEBUG  [ExceptionHandlerExceptionResolver] Resolving exception from handler [public org.springframework.http.ResponseEntity<java.util.List<com.thalasoft.learnintouch.rest.resource.AdminResource>> com.thalasoft.learnintouch.rest.controller.AdminController.search(java.lang.String,int,int,org.springframework.web.util.UriComponentsBuilder)]: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly 
2014-06-30 16:22:39,760 DEBUG  [ExceptionHandlerExceptionResolver] Resolving exception from handler [public org.springframework.http.ResponseEntity<java.util.List<com.thalasoft.learnintouch.rest.resource.AdminResource>> com.thalasoft.learnintouch.rest.controller.AdminController.search(java.lang.String,int,int,org.springframework.web.util.UriComponentsBuilder)]: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly 
2014-06-30 16:22:39,760 DEBUG  [ExceptionHandlerExceptionResolver] Resolving exception from handler [public org.springframework.http.ResponseEntity<java.util.List<com.thalasoft.learnintouch.rest.resource.AdminResource>> com.thalasoft.learnintouch.rest.controller.AdminController.search(java.lang.String,int,int,org.springframework.web.util.UriComponentsBuilder)]: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly 
2014-06-30 16:22:39,762 DEBUG  [ResponseStatusExceptionResolver] Resolving exception from handler [public org.springframework.http.ResponseEntity<java.util.List<com.thalasoft.learnintouch.rest.resource.AdminResource>> com.thalasoft.learnintouch.rest.controller.AdminController.search(java.lang.String,int,int,org.springframework.web.util.UriComponentsBuilder)]: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly 
2014-06-30 16:22:39,762 DEBUG  [ResponseStatusExceptionResolver] Resolving exception from handler [public org.springframework.http.ResponseEntity<java.util.List<com.thalasoft.learnintouch.rest.resource.AdminResource>> com.thalasoft.learnintouch.rest.controller.AdminController.search(java.lang.String,int,int,org.springframework.web.util.UriComponentsBuilder)]: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly 
2014-06-30 16:22:39,762 DEBUG  [ResponseStatusExceptionResolver] Resolving exception from handler [public org.springframework.http.ResponseEntity<java.util.List<com.thalasoft.learnintouch.rest.resource.AdminResource>> com.thalasoft.learnintouch.rest.controller.AdminController.search(java.lang.String,int,int,org.springframework.web.util.UriComponentsBuilder)]: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly 
2014-06-30 16:22:39,763 DEBUG  [DefaultHandlerExceptionResolver] Resolving exception from handler [public org.springframework.http.ResponseEntity<java.util.List<com.thalasoft.learnintouch.rest.resource.AdminResource>> com.thalasoft.learnintouch.rest.controller.AdminController.search(java.lang.String,int,int,org.springframework.web.util.UriComponentsBuilder)]: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly 
2014-06-30 16:22:39,763 DEBUG  [DefaultHandlerExceptionResolver] Resolving exception from handler [public org.springframework.http.ResponseEntity<java.util.List<com.thalasoft.learnintouch.rest.resource.AdminResource>> com.thalasoft.learnintouch.rest.controller.AdminController.search(java.lang.String,int,int,org.springframework.web.util.UriComponentsBuilder)]: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly 
2014-06-30 16:22:39,763 DEBUG  [DefaultHandlerExceptionResolver] Resolving exception from handler [public org.springframework.http.ResponseEntity<java.util.List<com.thalasoft.learnintouch.rest.resource.AdminResource>> com.thalasoft.learnintouch.rest.controller.AdminController.search(java.lang.String,int,int,org.springframework.web.util.UriComponentsBuilder)]: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly 
2014-06-30 16:22:39,773 DEBUG  [DispatcherServlet] Could not complete request 
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:522)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
    at com.thalasoft.learnintouch.rest.controller.AdminController$$EnhancerByCGLIB$$ebae6d18.search(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:201)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:73)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:513)
    ... 73 more
2014-06-30 16:22:39,773 DEBUG  [DispatcherServlet] Could not complete request 
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:522)

如果删除注释,则一切正常,响应包含搜索的项。

我假设当没有找到搜索的条目时会抛出一个异常,触发事务回滚消息。

以下是异常处理程序:

@ExceptionHandler(SearchedAdminsNotFoundException.class)
@ResponseBody
public ResponseEntity<ErrorInfo> searchedAdminsNotFoundException(HttpServletRequest request, SearchedAdminsNotFoundException e) {
    String url = request.getRequestURL().toString();
    String errorMessage = localizeErrorMessage("error.admin.search.not.found", new Object[] { e.getSearchTerm() });
    return new ResponseEntity<ErrorInfo>(new ErrorInfo(url, errorMessage), HttpStatus.NOT_FOUND);
}

有没有办法在日志的某个地方看到触发事务回滚的内部异常?

我现在使用的是Spring 3.2.9.RELEASE和Hibernate 3.6.10.Final。

下面是根本原因:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/learnintouch-rest] threw exception [Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly] with root cause
javax.persistence.RollbackException: Transaction marked as rollbackOnly
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:73)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:513)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
    at com.thalasoft.learnintouch.rest.controller.AdminController$$EnhancerByCGLIB$$b11b3634.search(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:201)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

更新:这是我的猜测...异常被捕获,因此不会显示在日志中。注释会回滚事务,即使异常被捕获。我对吗?在这种情况下,如果使用注释,如何处理,即在控制器中捕获异常?

亲切问候,

夏羽·埃伯特

共有1个答案

松国兴
2023-03-14

是的,您可以将异常传播给控制器,但是我会选择将服务方法标记为事务性的。我想知道你为什么抛出一个异常,但我想这也可以。另一种选择是为counting admins(或该查询中的任何计数)处理0值或大于0的值。也就是说,您的服务方法执行查询并返回值。如果是0,那么你为用户准备一个合适的网页,否则就是别的。

 类似资料:
  • 问题内容: 我已成功将此注释用于Dao类。回滚适用于测试。 但是现在我需要回滚真实代码,而不仅仅是测试。有用于测试的特殊注释。但是哪些注释适用于非测试代码?对我来说这是一个大问题。我已经花了一天的时间。官方文档不符合我的需求。 employeeDao is 这是一个注释可以很好地运行的测试: HibernateDaoBeans.xml 是的,我回滚了交易。我刚刚为服务添加了BEAN …,然后注释@

  • 我不明白的是为什么人们说自我调用会破坏交易?只要调用方方法是事务性的,难道一切都不应该像预期的那样工作吗?我有什么遗漏吗?

  • 我正在查看一些现有的代码,并想知道在下面的场景中使用Spring的@Transactional注释会发生什么?考虑以下示例: 下面的updateDataBaseItem()方法是常见的,可以从其他非事务性方法和上面的方法调用:

  • 为什么spring对一个注释为@Cacheable的方法的一次调用执行我的定制@Cacheable密钥生成器两次,为什么不只执行一次。 我的KeyGenerator实现 我的方法用@Cacheable和自定义keyGenerator注释 spring Rest控制器,它调用用@cacheable注释的方法 当我用自定义的keyGenerator对用@Cacheable注释的方法进行一次调用时,我在

  • 下面是一个使用JdbcTemplate的示例。我的问题是,由于runTimeException,doMultipleCalls()在第3步失败,一切是否都会回滚。我只有对多个调用的事务注释,而没有对其他调用的注释。所有人是否共享同一个事务会话?。如果没有,那么如何跨多个服务器传递相同的事务会话?