If I have code that looks like the following:
如果我的代码如下所示:
beginTransaction();
// lots of stuff happens, can take anywhere from a minute to several minutes.
// it will read from several tables via calling getter methods on lazy relationships.
commitTransaction();
In between the begin and commit, are the tables that are being read from being locked and subsequently will this cause problems in a multi-user environment where issues will occur when the same code above is called by another user?
在开始和提交之间,正在读取的表是否被锁定,随后这将导致多用户环境中的问题,当另一个用户调用上面的相同代码时会出现问题?
If the above is problematic, should we always try and keep transactions short? and to facilitate this, instead of calling getter methods on lazy relationships, does that mean its best to keep the transactions short and do finds manually for the children of the parents?
如果上述问题,我们是否应该始终尝试保持交易简短?并且为了促进这一点,不是在惰性关系上调用getter方法,这是否意味着最好保持事务简短并为父母的子项手动查找?
2 个解决方案
#1
18
Hibernate is not going to do anything to explicitly lock tables you read from. The answer really depends on what Database you're using and what your isolation levels are set to. Locking an entire table by reading rows shouldn't happen in any full featured database written in this century. For any multiversioning database, nothing is going to get locked unless you explicitly lock the row yourself.
Hibernate不会做任何事情来显式锁定你读取的表。答案实际上取决于您正在使用的数据库以及您设置的隔离级别。通过读取行来锁定整个表不应该发生在本世纪编写的任何全功能数据库中。对于任何多版本数据库,除非您自己明确锁定行,否则不会锁定任何内容。
Your transactions should be whatever length they need to be for your atomic unit of work. There's no right or wrong length. Ask yourself "does everything that happens here succeed or fail as a single unit and all get rolledback together if any one piece fails?" That's the scope you set a transaction for.
您的交易应该是您的原子工作单元所需的任何长度。长度没有对错。问问自己“这里发生的一切都是成功还是失败作为一个单位,如果任何一件失败,所有人都会回滚?”这是您为其设置交易的范围。
Remember, you do not need a transaction to have lazy loading! You just need an open Session. The two are not linked. You can commit your transaction and keep your session open to keep lazy loading working.
请记住,您不需要进行延迟加载的事务!你只需要一个开放的会话。这两者没有联系。您可以提交事务并保持会话打开以保持延迟加载工作。
#2
4
The best thing is to keep transactions short. The locking semantics depends on the transaction isolation levels though.
最好的办法是保持交易简短。锁定语义依赖于事务隔离级别。
Open Session In View is the pattern your are looking for when you are talking about lazy-fetching/relationships.
Open Session In View是您在谈论懒惰/关系时所寻找的模式。
#1
18
Hibernate is not going to do anything to explicitly lock tables you read from. The answer really depends on what Database you're using and what your isolation levels are set to. Locking an entire table by reading rows shouldn't happen in any full featured database written in this century. For any multiversioning database, nothing is going to get locked unless you explicitly lock the row yourself.
Hibernate不会做任何事情来显式锁定你读取的表。答案实际上取决于您正在使用的数据库以及您设置的隔离级别。通过读取行来锁定整个表不应该发生在本世纪编写的任何全功能数据库中。对于任何多版本数据库,除非您自己明确锁定行,否则不会锁定任何内容。
Your transactions should be whatever length they need to be for your atomic unit of work. There's no right or wrong length. Ask yourself "does everything that happens here succeed or fail as a single unit and all get rolledback together if any one piece fails?" That's the scope you set a transaction for.
您的交易应该是您的原子工作单元所需的任何长度。长度没有对错。问问自己“这里发生的一切都是成功还是失败作为一个单位,如果任何一件失败,所有人都会回滚?”这是您为其设置交易的范围。
Remember, you do not need a transaction to have lazy loading! You just need an open Session. The two are not linked. You can commit your transaction and keep your session open to keep lazy loading working.
请记住,您不需要进行延迟加载的事务!你只需要一个开放的会话。这两者没有联系。您可以提交事务并保持会话打开以保持延迟加载工作。
#2
4
The best thing is to keep transactions short. The locking semantics depends on the transaction isolation levels though.
最好的办法是保持交易简短。锁定语义依赖于事务隔离级别。
Open Session In View is the pattern your are looking for when you are talking about lazy-fetching/relationships.
Open Session In View是您在谈论懒惰/关系时所寻找的模式。