首页 > 解决方案 > DataSourceTransactionManager API中“可能允许每个数据源一个线程绑定连接”是什么意思?

问题描述

我正在阅读 Spring 的 DataSourceTransactionManager 类的 API 描述。但是,有几件事我从描述中无法理解。

这是我不明白的描述部分。

将指定数据源的 JDBC 连接绑定到当前线程,可能允许每个数据源有一个线程绑定连接。

注意:这个事务管理器操作的DataSource需要返回独立的Connections。Connections 可能来自池(典型情况),但 DataSource 不得返回 thread-scoped / request-scoped Connections 等。该事务管理器将根据指定的传播行为将 Connections 与线程绑定事务本身相关联。它假设即使在正在进行的事务期间也可以获得单独的、独立的连接。

让我困惑的两件事是,

  1. 当它说“可能允许每个数据源有一个线程绑定连接”时。这是否意味着它一次只允许每个 DataSource 一个线程绑定连接,即使连接池中还有许多未使用的空闲连接?所以它一次只支持一个用户?它不是应该像“每个事务可能允许一个线程绑定连接”之类的吗?

  2. 它说“数据源不得返回线程范围/请求范围的连接等”,因为事务管理器本身将连接绑定到当前正在执行的线程。因此,它说 DataSource 应该返回独立的连接。不过,这里的独立连接到底意味着什么?一个没有任何作用域的普通 Connection 实例?

谢谢你,我感谢你的帮助,

标签: springscopetransactionsconnectiondatasource

解决方案


In plain English, it says that:

  1. It is the transaction manager's job to ensure only one connection is used by (dedicated to servicing) a given thread at any given time (and so, it does support multiple users, because your application can simultaneously execute different transactions in separate threads)
  2. Precisely because scoping connections to a thread is the transaction manager's job, the data source itself should not attempt to do the same (because that would be in conflict with what the transaction manager is already doing)

I am assuming potentially simply means there might not be enough connections in the pool for each thread in your application to hold on to one, and not all threads even need a connection.


推荐阅读