Does the Spring transaction manager bind a connection to a thread?

我怕爱的太早我们不能终老 提交于 2019-12-03 12:35:30

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!