解决问题 Could not obtain transaction-synchronized Session for current thread

这篇具有很好参考价值的文章主要介绍了解决问题 Could not obtain transaction-synchronized Session for current thread。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、问题现象

在使用Hibernate的项目中,在一个定时任务中,执行某段代码,满足条件后,更新表数据。

程序在执行到更新表数据的时候,报错如下:

Could not obtain transaction-synchronized Session for current thread

解决问题 Could not obtain transaction-synchronized Session for current thread


二、解决方案

spring-config.xml 配置事务 切入点切入方法

<!-- 配置事务管理 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <!-- 指定使用cglib -->
     <aop:aspectj-autoproxy proxy-target-class="true" />
    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="dispose*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

    <!-- 配置事务的切入点 -->
    <aop:config>
        <aop:pointcut id="targetMethodSchedule" expression="execution(* com.miracle.luna.mock.schedule.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="targetMethodSchedule" />
    </aop:config>

[说明]
1、<tx:method name=“dispose*” propagation=“REQUIRED” /> 表示对dispose 开头的方法有效;
2、<aop:pointcut id=“targetMethodSchedule” expression=“execution(* com.miracle.luna.mock.schedule..(…))” /> 可以模糊匹配 schedule 目录下的多个服务层。

注意此处,不管schedule目录下有没有细分 service 和 impl层,必须是 schedule..(…)) ,否则会报如下错误。

Caused by: java.lang.IllegalArgumentException: warning no match for this type name: com.miracle.luna.mock.schedule [Xlint:invalidAbsoluteTypeName]
    at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:315)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:217)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:190)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:169)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:220)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:279)
    at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:311)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:119)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:89)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:70)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:346)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:298)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:423)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    ... 149 more

解决问题 Could not obtain transaction-synchronized Session for current thread文章来源地址https://www.toymoban.com/news/detail-477513.html


到了这里,关于解决问题 Could not obtain transaction-synchronized Session for current thread的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • Mac Could not find the GDAL library 问题解决

    https://docs.djangoproject.com/en/5.0/ref/contrib/gis/install/#macos 苹果系统 Mac 中搭建 GeoDjango 开发环境时出现以下报错: 找到 libgdal.dylib 的路径 settings.py 中添加以下代码: 找到 libgeos_c.dylib 的路径 settings.py 中添加以下代码:

    2024年01月21日
    浏览(14)
  • 单元测试 报 Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions

     单元测试: org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the se

    2024年02月06日
    浏览(15)
  • Could not initialize class org.bytedeco.javacpp.avutil问题解决

    最近提供需要提供音视频服务。参考网上的开源程序,提供了一个JAVA版的RTSP转HTTP的服务,并使用ffmpeg作为播放工具。 该程序在自己的机器上没有问题,但生成jar包后放置到服务器上运行出错,出错的是“java.lang.NoClassDefFoundError:Could not initialize class org.bytedeco.ffmpeg.gl

    2024年02月13日
    浏览(14)
  • Git错误解决:如何处理“could not determine hash algorithm“问题

    Git错误解决:如何处理“could not determine hash algorithm“问题

    🌷🍁 博主猫头虎(🐅🐾)带您 Go to New World✨🍁 🦄 博客首页 ——🐅🐾猫头虎的博客🎐 🐳 《面试题大全专栏》 🦕 文章图文并茂🦖生动形象🐅简单易学!欢迎大家来踩踩~🌺 🌊 《IDEA开发秘籍专栏》 🐾 学会IDEA常用操作,工作效率翻倍~💐 🌊 《100天精通Golang(基础

    2024年02月03日
    浏览(10)
  • 【笔记】解决Ubuntu 23.04下Warning: Could not find TensorRT的问题

    1. 有一块quadro显卡,想要试用一下, 先使用conda list安装了tensorflow 和 tensorrt 代码: import tensorflow as tf print(tf.test.is_built_with_cuda()) print(tf.config.list_physical_devices(\\\'GPU\\\')) 遇到如下问题: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT True W tensorflow/core/co

    2024年02月07日
    浏览(12)
  • 已解决 IDEA Maven 项目中 “Could not find artifact“ 问题的常见情况和解决方案

    已解决 IDEA Maven 项目中 “Could not find artifact“ 问题的常见情况和解决方案

    🌷🍁 博主 libin9iOak带您 Go to New World.✨🍁 🦄 个人主页——libin9iOak的博客🎐 🐳 《面试题大全》 文章图文并茂🦕生动形象🦖简单易学!欢迎大家来踩踩~🌺 🌊 《IDEA开发秘籍》学会IDEA常用操作,工作效率翻倍~💐 🪁🍁 希望本文能够给您带来一定的帮助🌸文章粗浅,敬

    2024年02月11日
    浏览(15)
  • IDEA Maven 项目中 “Could not find artifact“ 问题的常见情况和解决方案

    在IDEA(IntelliJ IDEA)中使用Maven构建项目时,有时会遇到“Could not find artifact”这样的错误。这个错误通常意味着Maven试图下载或查找某个依赖项,但是没有在配置的仓库中找到。这个问题可能会导致项目构建失败,因此需要了解常见的情况以及相应的解决方案。 IDEA Maven项目中

    2024年04月26日
    浏览(51)
  • Android Studio 解决Could not resolve com.android.tools.build:gradle:7.4.2问题

    Android Studio 解决Could not resolve com.android.tools.build:gradle:7.4.2问题

    A problem occurred configuring root project \\\'MyApplication2\\\'. Could not resolve all files for configuration \\\':classpath\\\'.    Could not resolve com.android.tools.build:gradle:7.4.2.      Required by:          project : com.android.application:com.android.application.gradle.plugin:7.4.2          project : com.android.library:com.android.library.

    2024年02月01日
    浏览(59)
  • 虚幻引擎图文笔记:could not be compiled. Try rebuilding from source manually.问题的解决

    虚幻引擎图文笔记:could not be compiled. Try rebuilding from source manually.问题的解决

    从SVN上Checkout的必要文件,在其它电脑上构建过一次没有问题,结果到了这台比较新的电脑上出现了如下问题⬇: 尝试生成sln文件⬇: 弹出了如下提示⬇: 在.NET Core官网下载并安装.NET Core。 于是又出现了如下提示 在这里下载并安装framework的指定版本,我这里要求安装 3.1.

    2024年02月11日
    浏览(12)
  • 解决Could not load dynamic library ‘cudnn64_8.dll‘; dlerror: cudnn64_8.dll not found问题

    解决Could not load dynamic library ‘cudnn64_8.dll‘; dlerror: cudnn64_8.dll not found问题

    以前都是在用CPU跑深度学习,只能说太勇了,今天终于想通了用GPU跑代码。 搭建环境参考 全网最详细的深度学习tensorflow-gpu环境配置 这篇文章写得非常棒,在运行的时候出现了报错 Could not load dynamic library ‘cudnn64_8.dll’; dlerror: cudnn64_8.dll not found 这个报错应该就是下载的cu

    2023年04月08日
    浏览(6)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包