Spring - 事务只读 [英] Spring - Transaction Readonly

查看:14
本文介绍了Spring - 事务只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想了解您对 Spring 声明式事务管理的专家意见.这是我的设置:

Just wanted your expert opinions on declarative transaction management for Spring. Here is my setup:

  1. DAO 层是使用 Spring JdbcTemplate 的普通老式 JDBC(无 Hibernate 等)
  2. 服务层是具有声明性事务的 POJO,如下所示 -save*, readonly = false, 回滚 Throwable

以上设置一切正常.但是,当我说 get*, readonly = true 时,我在日志文件中看到错误,指出 数据库连接无法标记为只读.服务层中的所有 get* 方法都会发生这种情况.

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.

现在我的问题是:

A.我是否必须将 get* 设置为只读?我所有的 get* 方法都是纯读取数据库操作.我不希望在任何事务上下文中运行它们.上述错误有多严重?

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?

B.当我删除 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?

C.为什么有人想要 readonly = true 的事务方法?这个配置有什么实际意义吗?

C. Why would anyone want to have transactional methods where readonly = true? Is there any practical significance of this configuration?

谢谢!一如既往,非常感谢您的回复!

Thank you! As always, your responses are much appreciated!

推荐答案

这篇文章 表明行为或readOnly 标志是依赖于持久性机制的.

This post tells that the behaviour or the readOnly flag is persistence-mechanism-dependent.

C. 是的,当使用休眠时,它通过将刷新模式设置为 FLUSH_NEVER(如链接帖子中所述)来提供性能优势

C. Yes, when using hibernate, it gives performance benefits by setting the flush mode to FLUSH_NEVER (as described in the linked post)

B. 是的,JDBC 调用不需要事务(休眠需要一个),因此删除 @Transactional 配置会削减所有事务管理.

B. Yes, JDBC calls do not require a transaction (hibernate requires one), so removing the @Transactional configuration trims all transaction management.

A. 我假设 spring 正在调用 connection.setReadOnly(true) 但您的 JDBC 驱动程序不支持此

A. I'd assume spring is calling connection.setReadOnly(true) but your JDBC driver doesn't support this

底线是:不要在普通 JDBC 中使用 readonly 事务.

The bottom-line is : don't use readonly transactions with plain JDBC.

还有一件事 - 交易应该跨越多个查询.不要让您的交易过于精细.让它们成为工作单元.

And another thing - transactions are supposed to span multiple queries. Don't make your transactions too fine-grained. Make them a unit of work.

这篇关于Spring - 事务只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆