Understanding r2d2's Pool::get()

What happens if r2d2::Pool::get() is called multiple times in one thread? Will it return the same connection if the request comes from the same thread, or will it use multiple connections out of the pool?

I'm attempting a layered "ports and adapters" architecture where I wish to call a repository layer from some code that need to be agnostic to the implementation details of the repository layer.

My current idea is to call a Repository::commit method that will take a closure - the calling code will then call other functions in the repository that each do r2d2::Pool::get() to get the connection they need - now I'm worried I will end up using a large part of the connection pool per Repository::commit while I should really only need one.

It's tempting to expose the connection in the closure and have the calling layer pass the connection to each method called in that closure - but having the calling layer passing around something as concrete as a database connection really doesn't make it very agnostic :thinking:

...or does the fact that methods are called sequentially within the closure make it so that only maximum of two connection will be used at the same time anyways - one for opening the db-transaction and one for each sequential call in the closure..?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.