gpt4 book ai didi

java - 在 Hibernate 中阻止写操作

转载 作者:太空宇宙 更新时间:2023-11-04 08:25:21 25 4
gpt4 key购买 nike

Hibernate 中的写入特定操作是否会阻塞?我的意思是,如果我执行:

session.save(data);

来自注释为@Transactional的方法,是否保证每个

session.get(id);

来自不同线程的调用,在该行之后立即执行,将返回正确的数据(非空)?

最佳答案

您获得的结果取决于事务隔离级别。 Spring 允许以下隔离级别:

DEFAULT Use the default isolation level of the underlying datastore.

READ_COMMITTED A constant indicating that dirty reads are prevented; non-repeatable reads and phantom reads can occur.

READ_UNCOMMITTED A constant indicating that dirty reads, non-repeatable reads and phantom reads can occur.

REPEATABLE_READ A constant indicating that dirty reads and non-repeatable reads are prevented; phantom reads can occur.

SERIALIZABLE A constant indicating that dirty reads, non-repeatable reads and phantom reads are prevented.

调用save()(或flush())并不能保证事务已提交。如果您的事务(调用 save() 的地方)在调用 get() 之前提交,您将始终在新线程中获得正确的结果。但是,如果事务没有提交,是否得到正确的结果取决于事务隔离级别。您将在允许“脏读”的隔离级别中获得正确的结果。

关于java - 在 Hibernate 中阻止写操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8664940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com