如何设置查询被取消读取? [英] how to set query to be read uncommited?
问题描述
我不希望这是一个全局设置,只是想在每个查询的基础上做到这一点。
< Hibernate API本身没有办法以编程的方式做到这一点,我知道。该文档确实表示您可以指定隔离级别作为配置属性:
hibernate.connection.isolation - 将
设置为JDBC事务隔离级别。
检查java.sql.Connection获得
有意义的值
但当然这适用于整个 SessionFactory
,而不仅限于特定的查询。
很容易摆脱底层的 java.sql .Connection
隔离级别,但我会非常小心地做这样的事情,你会冒着与hibernate自己的锁定策略冲突的风险,这些策略使用JDBC隔离级别来确定哪些<$ c将使用$ c> LockMode 。如果您直接在连接上更改此设置,则可能会得到奇怪的结果。当然,你可能没有选择,但要小心。
更好的解决方案是使用 Spring的Hibernate Transaction API ,它可以让事务语义分离完全可以从Hibernate API中以一种可预测和可靠的方式实现隔离级别。
with hibernate, how can I set a query to be read uncommitted? I don't want this to be a global setting, just want to do it on a per query basis.
The Hibernate API itself provides no way to do this programmatically, that I'm aware of. The docs do say that you can specify the isolation level as a configuration property:
hibernate.connection.isolation - Sets the JDBC transaction isolation level. Check java.sql.Connection for meaningful values
But of course this applies to the whole SessionFactory
, not just to specific queries.
It's tempting to fiddle with the underlying java.sql.Connection
isolation level, but I'd be very careful about doing that sort of thing, you run the risk of conflicting with hibernate's own locking strategies, which use the JDBC isolation level to determine which LockMode
will be used. If you change this directly on the Connection, you might get odd results. You may not have a choice, of course, but be careful.
A "better" solution would be to use Spring's Hibernate Transaction API, which allows you to decouple the transaction semantics like isolation level from the Hibernate API completely, in a predictable and reliable way.
这篇关于如何设置查询被取消读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!