你需要数据库事务来读取数据吗? [英] Do you need a database transaction for reading data?

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

问题描述

当我尝试从数据库读取数据时,至少使用

((Session)em.getDelegate())。createCriteria )



抛出一个异常,说事务不存在。



当我添加注释时:

  @Transactional(
value = SomeClass.TRANSACTIONAL_MANAGER,
propagation = Propagation.SUPPORTS,
readOnly = true

它工作的很好。

然而,由于阅读每秒会发生数百万次访问和读取数据,我想确保我们的环境不会不必要地堵塞。

如果不是,创建只读 Propagation.Supports 事务的成本



我不能在没有事务的情况下与Spring组合使用创建Hibernate Criteria Query吗?

解决方案

所有数据库语句都是在物理事务的上下文中执行的,



如果您不声明事务边界,那么每个语句都必须在单独的事务中执行( autocommit 模式)。这甚至可能导致每条语句打开和关闭一个连接,除非您的环境可以处理每个线程连接的绑定。



将服务声明为 @Transactional 会为整个事务处理持续时间提供一个连接,并且所有语句都将使用该单独的隔离连接。这比首先使用显式事务更好。

在大型应用程序中,您可能有许多并发请求,并且减少数据库连接获取请求率肯定会提高您的整体应用程序性能。


$ b JPA不会对读操作执行事务。只有写入操作最终会抛出事务所需的异常,以免忘记启动事务性上下文。尽管如此,即使对于只读事务也要声明事务边界总是更好(在Spring中, @Transactional 允许您标记只读事务,这对性能有很大的好处)。现在,如果使用声明式事务边界(例如 @Transactional ),则需要确保数据库连接获取被延迟,直到有一条JDBC语句被执行。在JTA中,这是默认行为。在使用RESOURCE_LOCAL时,您需要设置 hibernate.connection.provider_disables_autocommit 配置属性,并确保底层连接池设置为禁用自动提交模式。


When I try to read data from the database, at least using

((Session)em.getDelegate()).createCriteria()

an exception is throws saying that a transaction is not present.

When I add the annotation:

@Transactional(
    value = SomeClass.TRANSACTIONAL_MANAGER, 
    propagation = Propagation.SUPPORTS, 
    readOnly = true
)

it works fine.

However, since reading will happen million of times per second to access and read data, I want to make sure that our environment is not clogged up unnecessarily.

If not, what is the cost of creating a read-only Propagation.Supports transaction?

Can I not create a Hibernate Criteria Query without a transaction, in combination with Spring?

解决方案

All database statements are executed within the context of a physical transaction, even when we don’t explicitly declare transaction boundaries (BEGIN/COMMIT/ROLLBACK).

If you don't declare transaction boundaries, then each statement will have to be executed in a separate transaction (autocommit mode). This may even lead to opening and closing one connection per statement unless your environment can deal with connection-per-thread binding.

Declaring a service as @Transactional will give you one connection for the whole transaction duration, and all statements will use that single isolation connection. This is way better than not using explicit transactions in the first place.

On large applications, you may have many concurrent requests, and reducing database connection acquisition request rate will definitely improve your overall application performance.

JPA doesn't enforce transactions on read operations. Only writes end up throwing a transaction required exception in case you forget to start a transactional context. Nevertheless, it's always better to declare transaction boundaries even for read-only transactions (in Spring @Transactional allows you to mark read-only transactions, which has a great performance benefit).

Now, if you use declarative transaction boundaries (e.g. @Transactional), you need to make sure that the database connection acquisition is delayed until there is a JDBC statement to be executed. In JTA, this is the default behavior. When using RESOURCE_LOCAL, you need to set the hibernate.connection.provider_disables_autocommit configuration property and make sure that the underlying connection pool is set to disable the auto-commit mode.

这篇关于你需要数据库事务来读取数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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