Spring事务管理器是否将连接绑定到线程? [英] Does the Spring transaction manager bind a connection to a thread?

查看:228
本文介绍了Spring事务管理器是否将连接绑定到线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了以下线程:如何将JdbcTemplate与TransactionManager精确地配合使用?/a>

I found the following thread: How exactly JdbcTemplate with TransactionManager works together?

第一句话:

据我了解,DataSourceTransactionManager将JDBC连接从指定的DataSource绑定到当前线程,从而为每个DataSource允许一个线程绑定的Connection.如果它是一个连接池,它将采用一个可用的连接.

As far as I understood DataSourceTransactionManager binds a JDBC connection from the specified DataSource to the current thread, allowing for one thread-bound Connection per DataSource. If it's a pool of connections, it will take one of the available connections.

...正是我想知道的.

... is exactly what I want to know.

使用事务管理器时,每个线程都有自己的单个连接吗?另外,该连接可以存活多长时间?在单个请求中,相同的线程使用相同的连接吗?还是发生了其他情况?我只是想了解当您拥有事务管理器和不拥有事务管理器时,Spring到底在做什么(无论您是否实际有事务).

When using a transaction manager, do you end up with each thread having it's own single connection? Also, how long does that connection live? Does the same thread use the same connection throughout a single request, or is there something else going on? I'm just trying to understand what exactly Spring is doing underneath when you have a transaction manager and when you don't (regardless of whether or not you actually have a transaction).

推荐答案

使用事务管理器时,每个线程都有自己的单个连接吗?另外,该连接可以存活多长时间?

When using a transaction manager, do you end up with each thread having it's own single connection? Also, how long does that connection live?

通常从连接池获得连接.当事务管理器启动事务时,从池中借用该连接,然后在事务完成时返回到池中.在此期间,连接绑定到线程.

The connection is generally obtained from a connection pool. The connection is borrowed from the pool when the transaction manager starts the transaction, and then returned to the pool when the transaction finishes. During that time, the connection is bound to the thread.

同一线程在单个请求中是否使用相同的连接

Does the same thread use the same connection throughout a single request

在交易期间使用相同的连接.该请求本身是不相关的.

It uses the same connection for the duration of the transaction. The request itself is irrelevant.

无论您是否实际进行交易

regardless of whether or not you actually have a transaction

无论您是否明确进行交易,您总是.如果您未明确配置一个,则JDBC驱动程序和数据库将启动和完成一个驱动程序,直到执行单个操作所需的时间. Spring的事务管理(或任何其他框架的事务管理)使您可以跨多个操作延长该事务的生命周期.这样做需要在交易期间独占使用该连接.

You always have a transaction, whether you do it explicitly or not. If you don't configure one explicitly, then the JDBC driver and database will start and finish one for as long as it takes to perform the single operation. Spring's transaction management (or any other framework's transaction management) lets you extend the lifetime of that transaction across multiple operations. Doing so requires exclusive use of the connection for the duration of the transaction.

这篇关于Spring事务管理器是否将连接绑定到线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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