Java Spring - 事务只读
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2562865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring - Transaction Readonly
提问by AAK
Just wanted your expert opinions on declarative transaction management for Spring. Here is my setup:
只是想了解您对 Spring 声明式事务管理的专家意见。这是我的设置:
- DAO layer is plain old JDBC using Spring JdbcTemplate (No Hibernate etc)
- Service layer is POJO with declarative transactions as follows -
save*, readonly = false, rollback for Throwable
- DAO 层是使用 Spring JdbcTemplate(无 Hibernate 等)的普通旧 JDBC
- 服务层是具有声明性事务的 POJO,如下所示 -
save*, readonly = false, rollback for Throwable
Things work fine with above setup. However when I say get*, readonly = true
, I see errors in my log file saying Database connection cannot be marked as readonly
. This happens for all get* methods in service layer.
上面的设置一切正常。但是,当我说时get*, readonly = true
,我在日志文件中看到错误说Database connection cannot be marked as readonly
. 服务层中的所有 get* 方法都会发生这种情况。
Now my questions are:
现在我的问题是:
A. Do I have to set get*
as readonly? All my get*
methods are pure read DB operations. I do not wish to run them in any transaction context. How serious is the above error?
A. 我必须设置get*
为只读吗?我所有的get*
方法都是纯读数据库操作。我不希望在任何事务上下文中运行它们。上述错误有多严重?
B. When I remove the get*
configuration, I do not see the errors. Morever, all my simple get*
operations are performed without transactions. Is this the way to go?
B. 当我删除get*
配置时,我没有看到错误。而且,我所有的简单get*
操作都是在没有事务的情况下执行的。这是要走的路吗?
C. Why would anyone want to have transactional methods where readonly = true
? Is there any practical significance of this configuration?
C. 为什么有人想要在何处使用事务方法readonly = true
?这个配置有什么实际意义吗?
Thank you! As always, your responses are much appreciated!
谢谢!与往常一样,非常感谢您的回复!
采纳答案by Bozho
This posttells that the behaviour or the readOnly
flag is persistence-mechanism-dependent.
这篇文章告诉我们行为或readOnly
标志是依赖于持久性机制的。
C.Yes, when using hibernate, it gives performance benefits by setting the flush mode to FLUSH_NEVER
(as described in the linked post)
C.是的,当使用休眠时,它通过将刷新模式设置为FLUSH_NEVER
(如链接帖子中所述)来提高性能
B.Yes, JDBC calls do not require a transaction (hibernate requires one), so removing the @Transactional
configuration trims all transaction management.
B.是的,JDBC 调用不需要事务(hibernate 需要一个),因此删除@Transactional
配置会修剪所有事务管理。
A.I'd assume spring is calling connection.setReadOnly(true)
but your JDBC driver doesn't support this
答:我假设 spring 正在调用,connection.setReadOnly(true)
但您的 JDBC 驱动程序不支持此功能
The bottom-line is : don't use readonly
transactions with plain JDBC.
底线是:不要使用readonly
带有普通 JDBC 的事务。
And another thing - transactions are supposed to span multiplequeries. Don't make your transactions too fine-grained. Make them a unit of work.
另一件事 - 交易应该跨越多个查询。不要让您的交易过于精细。使它们成为一个工作单元。
回答by matt b
A. Do I have to say get* as readonly? All my get* methods are pure read DB operations. I do not wish to run them in any transaction context. How serious is the above error?
A. 我必须说 get* 为只读吗?我所有的 get* 方法都是纯读取数据库操作。我不希望在任何事务上下文中运行它们。上述错误有多严重?
Actually, you probably still want to run all of your get()
s in the context of a transaction, to make sure that you are getting consistent reads. If on the other hand, you don't care about this, you can set the transaction level accordingly.
实际上,您可能仍然希望get()
在事务的上下文中运行所有s,以确保获得一致的读取。另一方面,如果您不关心这一点,则可以相应地设置事务级别。
C. Why would anyone want to have transactional methods where readonly = true? Is there any practical significance of this configuration?
C. 为什么有人想要 readonly = true 的事务方法?这个配置有什么实际意义吗?
- To help protect against errant writes within get()` methods
- For optimization purposes. Not only can Hibernate make use of this information, as Bozho mentioned, but some databases/JDBC drivers can also be make use of this info.
- 帮助防止 get()` 方法中的错误写入
- 出于优化目的。正如 Bozho 提到的,Hibernate 不仅可以利用这些信息,而且一些数据库/JDBC 驱动程序也可以利用这些信息。