> Following is the controller for post method

@Autowired
TrainerDetailsService trainerDetailsService;
@RequestMapping(path="/saveTrainerDetails" , method = RequestMethod.POST)
public TrainerDetails saveTrainerDetails(@RequestBody TrainerDetails trainerDetails)
{
     trainerDetailsService.savetrainerDetails(trainerDetails);
     return trainerDetails;
}



服务层


@Override
public TrainerDetails savetrainerDetails(TrainerDetails trainerDetails) {
    return trainerDetailsDao.saveTrainerDetails(trainerDetails);
}



DAO层


@Override

public TrainerDetails saveTrainerDetails(TrainerDetails trainerDetails) {
        Transaction tr =null;
        try {
            Session s = sessionFactory.openSession();
            tr =s.beginTransaction();
            s.saveOrUpdate(trainerDetails);
            tr.commit();
            s.flush();
            s.close();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return trainerDetails;
    }



还想知道如果多个用户同时访问它,我该如何处理PUT请求

最佳答案

您可以尝试在Service类上使用@Transactional注释,而不是手动处理事务。
另外,我没有看到设置隔离(除非代码比这里显示的更多)。
根据您的问题,应设置ISOLATION.READ_COMMITTED。

请参阅本文以更详细地了解这些属性:https://www.baeldung.com/spring-transactional-propagation-isolation

10-04 23:30