JDBC事务标记为仅回滚(异常提供了堆栈跟踪)在Spring Boot 3项目启动时。

huangapple go评论141阅读模式
英文:

JDBC transaction marked for rollback-only (exception provided for stack trace) at the start up of project in springboot 3

问题

N.B:这个问题可能看起来是重复的,但我认为这不是因为在我的情况下,我只在springboot 3中获取到这个错误,而不是在实际事务中获取到。

我正在从springboot 2.7.5迁移到3.0.6。除了build.gradle之外,没有对代码进行任何更改,但只有在启用调试模式时,在项目启动过程中才会获取到一个新的异常

java.lang.Exception: exception just for purpose of providing stack trace
        at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.markRollbackOnly(JdbcResourceLocalTransactionCoordinatorImpl.java:324)
        at org.hibernate.internal.AbstractSharedSessionContract.buildNamedQuery(AbstractSharedSessionContract.java:1003)
        at org.hibernate.internal.AbstractSharedSessionContract.createNamedQuery(AbstractSharedSessionContract.java:866)
        at org.hibernate.internal.AbstractSharedSessionContract.createNamedQuery(AbstractSharedSessionContract.java:126)
        at jdk.internal.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)
        ...
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293)
        at com.Application.main(Nid1StreamsApplication.java:10)

我的服务类用@Transactional注释。当发生异常时,Hibernate会提供这个消息JDBC事务标记为仅回滚。我的问题是:

  1. 为什么在项目启动过程中没有异常时事务被标记为仅回滚?
  2. exception just for purpose of providing stack trace,这意味着我应该忽略这个异常吗?

编辑:对于每个存储库方法,我都会得到这个异常。在每个异常之前,我会得到这样的日志

2023-06-19 11:04:53,968 DEBUG [main] o.s.o.j.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler:302 - Creating new EntityManager for shared EntityManager invocation
2023-06-19 11:04:53,970 DEBUG [main] o.s.d.r.c.s.RepositoryFactorySupport:272 - Initializing repository instance for com.demo.repo.CountryRepo…
2023-06-19 11:04:53,971 DEBUG [main] o.s.o.j.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler:302 - Creating new EntityManager for shared EntityManager invocation
2023-06-19 11:04:53,975 DEBUG [main] o.s.d.j.r.q.NamedQuery:141 - Looking up named query CountryRepo.findByName
英文:

N.B This question might seem duplicate but I think this is not because in my case I am getting this error only in springboot 3 and during starting the project, not during actual transaction.

I am migrating from springboot 2.7.5 to 3.0.6 . Without making change to code except build.gradle I am getting a new exception during project startup only when debug mode is enabled

023-06-15 12:30:23,839 DEBUG [main] o.h.r.t.b.j.i.JdbcResourceLocalTransactionCoordinatorImpl:324 - JDBC transaction marked for rollback-only (exception provided for stack trace)
java.lang.Exception: exception just for purpose of providing stack trace
        at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.markRollbackOnly(JdbcResourceLocalTransactionCoordinatorImpl.java:324)
        at org.hibernate.internal.AbstractSharedSessionContract.buildNamedQuery(AbstractSharedSessionContract.java:1003)
        at org.hibernate.internal.AbstractSharedSessionContract.createNamedQuery(AbstractSharedSessionContract.java:866)
        at org.hibernate.internal.AbstractSharedSessionContract.createNamedQuery(AbstractSharedSessionContract.java:126)
        at jdk.internal.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:360)
        at jdk.proxy2/jdk.proxy2.$Proxy147.createNamedQuery(Unknown Source)
        at org.springframework.data.jpa.repository.query.NamedQuery.hasNamedQuery(NamedQuery.java:115)
        at org.springframework.data.jpa.repository.query.NamedQuery.lookupFrom(NamedQuery.java:144)
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:180)
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:252)
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:95)
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:111)
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:99)
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new$0(QueryExecutorMethodInterceptor.java:88)
        at java.base/java.util.Optional.map(Optional.java:260)
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:88)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:357)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:279)
        at org.springframework.data.util.Lazy.getNullable(Lazy.java:245)
        at org.springframework.data.util.Lazy.get(Lazy.java:114)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:285)
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:132)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:967)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:310)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293)
        at com.Application.main(Nid1StreamsApplication.java:10)

My service class is annotated with @Transactional . Hibernate provides this message JDBC transaction marked for rollback-only when exception occurs. My question is

  1. Why transactions is marked for rollback-only when there is no exception during project startup?
  2. exception just for purpose of providing stack trace, does this mean I should ignore this exception?

Edit: I am getting this exception for each repo method. Before each exception I am getting logs like this

2023-06-19 11:04:53,967 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory:225 - Creating shared instance of singleton bean 'countryRepo'
2023-06-19 11:04:53,968 DEBUG [main] o.s.o.j.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler:302 - Creating new EntityManager for shared EntityManager invocation
2023-06-19 11:04:53,970 DEBUG [main] o.s.d.r.c.s.RepositoryFactorySupport:272 - Initializing repository instance for com.demo.repo.CountryRepo…
2023-06-19 11:04:53,971 DEBUG [main] o.s.o.j.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler:302 - Creating new EntityManager for shared EntityManager invocation
2023-06-19 11:04:53,975 DEBUG [main] o.s.d.j.r.q.NamedQuery:141 - Looking up named query CountryRepo.findByName

答案1

得分: 1

TL;DR 是的,忽略此堆栈跟踪,因为它仅用于信息目的!

这是因为Spring Data JPA在启动时将检查查询方法是否是命名查询或需要解析以生成查询。如果不是命名查询,它将生成并验证查询。

显然,包含在Spring Boot 3.x中的新版Hibernate现在以调试级别引发错误。而旧版本则不会。

简而言之,不存在“我只是从Spring Boot 2.7升级到Spring Boot 3.0”的情况,因为它将升级更多的依赖项和库。对于这些,您也应该阅读变更日志(根据库/框架,您可能需要更改其他内容!)。

英文:

TL;DR Yes ignore this stacktrace as it is for information purpose only!

This is because Spring Data JPA, at startup, will check if the query method is either a named query or needs to be parsed to generate a query. If not a named query it will generate and validate the query.

Apparently the newer Hibernate version included in Spring Boot 3.x now throws an error at debug level for this. Where older versions didn't.

In short there is no such thing as "I only upgraded from Spring Boot 2.7 to Spring Boot 3.0" as it will upgrade a lot more dependencies and libraries. For which you should read the changelogs as well (depending on the library/framework you might need to change other things as well!).

huangapple
  • 本文由 发表于 2023年6月19日 12:56:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定