首页 > 其他分享 >Could not obtain transaction-synchronized Session for current thread

Could not obtain transaction-synchronized Session for current thread

时间:2022-11-17 10:35:12浏览次数:40  
标签:transaction thread synchronized Autowired springmvc List tasks camunda

一 场景
我的项目中,发生这个错误是由于我使用springmvc框架,

但是在里面新建了一个 springboot的camunda(流程)模块。

而springmvc使用hibernate,camunda使用jpa的entityManager。

二 解决
在service中,添加@Transactional

以代码为例:

@Transactional
@Service
public class ApproveImpl implements ApproveService {
 
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    HistoryService historyService;
    @Autowired
    CrmCompanyService crmCompanyService;
 
 
    @Override
    public List<CrmCompany> companys(String user) {
        //camunda 的调用
        List<Task> tasks = taskService.createTaskQuery()
                .taskAssignee(user)
                .orderByTaskCreateTime().asc()
                .active()
                .list();
        //获取业务key
        List<Integer> businessKeys=tasks
                .stream()
                .map(x->Integer.parseInt(runtimeService.createProcessInstanceQuery().processInstanceId(x.getProcessInstanceId()).singleResult().getBusinessKey()))
                .collect(Collectors.toList());
 
        //hibernate 的调用
        List<CrmCompany> crmCompanies=crmCompanyService.findList(businessKeys);
        return crmCompanies;
    }
}

 

标签:transaction,thread,synchronized,Autowired,springmvc,List,tasks,camunda
From: https://www.cnblogs.com/hanjun0612/p/16898578.html

相关文章

  • Netty源码-02-FastThreadLocalThread
    一DemopublicclassFastThreadLocalTest00{privatefinalstaticFastThreadLocal<Long>v=newFastThreadLocal<Long>(){@Overrideprotec......
  • Java并发之回顾Thread和runnable
    jdk文档的描述ThreadAthreadisathreadofexecutioninaprogram.TheJavaVirtualMachineallowsanapplicationtohavemultiplethreadsofexecutionrunn......
  • Java中ThreadLocal详解
    一、ThreadLocal简介        ThreadLocal叫做线程变量,意思是ThreadLocal中填充的变量属于当前线程,该变量对其他线程而言是隔离的,也就是说该变量是当前线程独有的......
  • 【Java】Synchronized与ReentrantLock区别总结
    这篇文章是关于这两个同步锁的简单总结比较,关于底层源码实现原理没有过多涉及,后面会有关于这两个同步锁的底层原理篇幅去介绍。相似点:这两种同步方式有很多相似之处,它们......
  • springboot 事务Transactional使用小技巧
    springboot事务Transactional使用小技巧使用场景:我们在处理业务时会有这样的需求:我们需要在业务中需要调用远程的RPC接口,或者调用MQ发送消息,如果一切正常那自然皆大欢喜......
  • C++ folly库解读(三)Synchronized —— 比std::lock_guard/std::unique_lock更易用、功
    目录传统同步方案的缺点folly/Synchronized.h简单使用Synchronized的模板参数withLock()/withRLock()/withWLock()——更易用的加锁方式升级锁ulock()和withULo......
  • Thread的状态变更
      【需注意的是:运行中(Running)和就绪(Ready)并不是Java的线程状态】publicenumState{NEW,RUNNABLE,BLOCKED,WAITING,......
  • python 线程池 ThreadPoolExecutor
    从Python3.2开始,标准库为我们提供了concurrent.futures模块,它提供了ThreadPoolExecutor(线程池)和ProcessPoolExecutor(进程池)两个类。相比threading等模块,该模......
  • CreateThread与_beginthreadex
    今天上实验课编写Windows下Socket多线程实验。机房没有网络,用手机百度之后找到的是CreateThread()函数创建线程。试着创建了一个多线程的小程序,运行之后也挺......
  • frida anti pthread_create
    functionanti_pthread(){varp_pthread_create=Module.findExportByName("libc.so","pthread_create");varpthread_create=newNativeFunction(p_pthread_cr......