休眠锁定模式

bwleehnv  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(170)

你能解释一下为什么测试失败,这意味着锁坏了吗?

@Test
    @Transactional
    public void run() throws InterruptedException {
        int id = initDB();
        Myobj myobj= dao.get(id, LockMode.PESSIMISTIC_READ);

        Thread thread = new Thread(() -> delete(id));
        thread.start();
        thread.join();

        Assert.assertNotNull(dao.get(id, LockMode.NONE));
   }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void delete(long id){
           Myobj myobj = new Myobj(id);
           dao.delete(myobj);
    }

@Repository
@Transactional(readOnly = true)
public class DAO{

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public Myobj get(long id, LockMode mode){
        return sessionFactory.getCurrentSession().get(Myobj.class,id,mode);

    }

    @Transactional
    public void delete(Myobj myobj){
        sessionFactory.getCurrentSession().delete(Myobj.class.getName(),myobj);
    }
.
.

我正在使用mysql5和hibernate5.4.0
我可以从hibernate日志中看到,他在sql请求中包含了锁: lock in share mode

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题