Could not obtain transaction-synchronized Session for current thread

Posted 正怒月神

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Could not obtain transaction-synchronized Session for current thread相关的知识,希望对你有一定的参考价值。

一 场景

我的项目中,发生这个错误是由于我使用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;
    

以上是关于Could not obtain transaction-synchronized Session for current thread的主要内容,如果未能解决你的问题,请参考以下文章

Could not obtain transaction-synchronized Session for current thread

Could not obtain transaction-synchronized Session for current thread

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thre

Could not obtain transaction-synchronized Session for current thread

Could not obtain connection metadata

Could not obtain connection to query metadata : Public Key Retrieval is not allowed