-
Notifications
You must be signed in to change notification settings - Fork 30
feat: clear session pool on connection close #543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @olavloite in https://github.com/googleapis/python-spanner-django/pull/543/files#r510984476 that this shouldn't clear the session pool if it doesn't own it.
FWIW in python-spanner Database
doesn't expose the session pool or give users an option to clear
it (unless they created the pool themselves). Like #535 (comment) this might be an argument for changes to python-spanner instead of this library.
I still don't see how one can use single pool for several databases. Pool is binded to a concrete database, if we'll use the same pool for connections to different databases, it's not gonna be correct. Thus, for me it looks like users should not use global pools at all, even for several connections to the same database, as we're not very able to control whether the pool is used only for single database. |
Yes, that is correct, but I'm not sure that is what @c24t meant. So to summarize:
My comment on using something like a global session pool was aimed at that last point from the list above, where 'global' means 'global per database'. So if a user creates multiple connections to different databases, we also need multiple session pools (one for each unique database). The current solution of requiring the user to supply a database instance when creating a connection is as far as I can tell fine for the current use case (Django support). |
@olavloite, our main way to create a connection is actually connect function, and it works with string ids of instance and database. However, one can send a session pool into this function. If user not setting Thus, in fact user can use global pools, but looking at how As the original client doesn't care about closing a binded pool of the database, I don't think this PR is very major. If we don't have a good and simple way to close the pool, I'm okay to just close the PR. |
@IlyaFaer I would say that it would normally be a good practice to let the owner of the pool decide when a pool is closed. So that would mean:
A global session pool that can be used for different backend databases is as you say not possible, as a session pool is always bound to a specific database on the backend. What I meant with a global session pool is a global session pool per database. That would basically mean a pool of session pools with the database-id as the key for getting a session pool from this 'session pool pool'. That is something that would be preferable in the end for the SQLAlchemy implementation. I had a closer look at the current
|
@olavloite, the thing which I don't like in our case is that we're initiating What if we'll expose a |
I don't think we should add a parameter to the |
Of course we can. Can't say I like it, but seems like there is no ideal solution in here, so I'm pushing a protected property. |
from google.cloud.spanner_dbapi import Connection | ||
|
||
connection = Connection(self.INSTANCE, self.DATABASE) | ||
connection = self._make_connection() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test has an error: self.DATABASE
is actually a string, not a Database
object, so passing it into Connection
object constructor will cause errors.
Thank you. I think we should look some more into this later if we start adding support for SqlAlchemy. From what I understand from the documentation on that, a connection is created by SqlAlchemy itself based on a connection string, which means that we probably need to do some more management of databases and session pools. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change itself looks good to me.
Can we do anything with the build failures, or are those related to the general Kokoro / test problems?
Yes, system tests on Kokoro are broken because of parallelization. I'm checking all other sessions, but system tests for now only can be run succesfully on emulator (separate GitHub Workflow check). |
@c24t, we've processed this PR. Do you have any final comments? |
To avoid keeping Cloud Spanner sessions alive after connection close we should clear the related pool on the connection close.