Spring - Transaction Readonly [英] Spring - Transaction Readonly

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

问题描述

只想获得关于Spring的声明式事务管理的专家意见。这是我的设置:

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


  1. DAO层是使用Spring JdbcTemplate(无Hibernate等)的普通旧JDBC

  2. 服务层是POJO,声明式事务如下 -
    save *,readonly = false,回滚为Throwable

  1. DAO layer is plain old JDBC using Spring JdbcTemplate (No Hibernate etc)
  2. Service layer is POJO with declarative transactions as follows - save*, readonly = false, rollback for 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 * 方法都是纯读取DB操作。我不希望在任何事务上下文中运行它们。上述错误有多严重?

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。是的,当使用hibernate时,它通过将刷新模式设置为<$ c来提供性能优势$ 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.

答:我认为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 - Transaction Readonly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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