• 【spring源码学习】spring配置的事务方式是REQUIRED,但业务层抛出TransactionRequiredException异常问题


    (1)spring抛出异常的点:org.springframework.orm.jpa.EntityManagerFactoryUtils

    public static DataAccessException convertJpaAccessExceptionIfPossible(RuntimeException ex) {
            // Following the JPA specification, a persistence provider can also
            // throw these two exceptions, besides PersistenceException.
            if (ex instanceof IllegalStateException) {
                return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
            }
            if (ex instanceof IllegalArgumentException) {
                return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
            }
    
            // Check for well-known PersistenceException subclasses.
            if (ex instanceof EntityNotFoundException) {
                return new JpaObjectRetrievalFailureException((EntityNotFoundException) ex);
            }
            if (ex instanceof NoResultException) {
                return new EmptyResultDataAccessException(ex.getMessage(), 1, ex);
            }
            if (ex instanceof NonUniqueResultException) {
                return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
            }
            if (ex instanceof QueryTimeoutException) {
                return new org.springframework.dao.QueryTimeoutException(ex.getMessage(), ex);
            }
            if (ex instanceof LockTimeoutException) {
                return new CannotAcquireLockException(ex.getMessage(), ex);
            }
            if (ex instanceof PessimisticLockException) {
                return new PessimisticLockingFailureException(ex.getMessage(), ex);
            }
            if (ex instanceof OptimisticLockException) {
                return new JpaOptimisticLockingFailureException((OptimisticLockException) ex);
            }
            if (ex instanceof EntityExistsException) {
                return new DataIntegrityViolationException(ex.getMessage(), ex);
            }
                    //抛出异常的点位
            if (ex instanceof TransactionRequiredException) {
                return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
            }
    
            // If we have another kind of PersistenceException, throw it.
            if (ex instanceof PersistenceException) {
                return new JpaSystemException(ex);
            }
    
            // If we get here, we have an exception that resulted from user code,
            // rather than the persistence provider, so we return null to indicate
            // that translation should not occur.
            return null;
        }
    View Code

    (2)异常信息

    ERROR org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler - Unexpected error occurred invoking async method 'public void (此处省略具体业事件监听类)Listener.onApplicationEvent(CompleteEvent)'.
    org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
        at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:413) ~[spring-orm-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246) ~[spring-orm-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:488) ~[spring-orm-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) [spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at com.sun.proxy.$Proxy55.saveAndFlush(Unknown Source) ~[na:na]
        //省略业务类信息
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) [spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) [spring-tx-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) [spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) [spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        atl$$EnhancerBySpringCGLIB$$a432af48.complete(<generated>) ~
    --
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        ... 85 common frames omitted
    View Code
  • 相关阅读:
    从 QSplitter 中移除 QWidget(使用隐藏与显示,切换十分方便,不要真正销毁)
    Qt虽然自己的源代码里不使用Exception,但也提供了一个QException及其子类QUnhandledException
    细说new与malloc的10点区别
    垃圾回收算法
    服务追踪数据使用 RabbitMQ 进行采集 + 数据存储使用 Elasticsearch + 数据展示使用 Kibana
    缓存穿透、缓存击穿与缓存雪崩
    微服务介绍
    分库分表
    Spring Boot、微服务架构和大数据
    Linux基本的操作
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/7516211.html
Copyright © 2020-2023  润新知