Skip to content
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

perf: optimize isValid implementation #1444

Merged
merged 1 commit into from Dec 19, 2023
Merged

perf: optimize isValid implementation #1444

merged 1 commit into from Dec 19, 2023

Conversation

olavloite
Copy link
Collaborator

Cloud Spanner JDBC connections do not maintain a physical connection to Cloud Spanner, but are merely a wrapper around the underlying Java client library. This again uses a pool of gRPC channels to communicate with Cloud Spanner. This means that a single JDBC connection will never lose its network connection with Cloud Spanner, and checking whether it is valid or not by executing a query every time is not useful. Instead, the check should:

  1. Verify that a connection has successfully been established with Cloud Spanner. The result should be cached for all JDBC connections using the same Cloud Spanner client library instance.
  2. Verify that the connection has not been closed.

The above can be achieved by checking that the dialect of the database that the connection is connected to has been successfully fetched. This result is cached in the client library, and being able to get that means that there has been a valid connection.

This means that the isValid method will still return true if the network connectivity has been lost completely between the client and Cloud Spanner. However, this check is mostly used by connection pools to determine whether a connection is safe to be handed out to an application, and when all network connectivity has been lost, this will apply to all JDBC connections and not just one, meaning that the check is void.

The original isValid check can be enabled by setting the System property spanner.jdbc.use_legacy_is_valid_check to true or setting the Environment variable SPANNER_JDBC_USE_LEGACY_IS_VALID_CHECK to true.

Fixes #1443

Cloud Spanner JDBC connections do not maintain a physical connection to
Cloud Spanner, but are merely a wrapper around the underlying Java
client library. This again uses a pool of gRPC channels to communicate
with Cloud Spanner. This means that a single JDBC connection will never
lose its network connection with Cloud Spanner, and checking whether it
is valid or not by executing a query every time is not useful. Instead,
the check should:
1. Verify that a connection has successfully been established with Cloud
   Spanner. The result should be cached for all JDBC connections using
   the same Cloud Spanner client library instance.
2. Verify that the connection has not been closed.

The above can be achieved by checking that the dialect of the database
that the connection is connected to has been successfully fetched. This
result is cached in the client library, and being able to get that means
that there has been a valid connection.

This means that the isValid method will still return true if the network
connectivity has been lost completely between the client and Cloud
Spanner. However, this check is mostly used by connection pools to
determine whether a connection is safe to be handed out to an
application, and when all network connectivity has been lost, this will
apply to all JDBC connections and not just one, meaning that the check
is void.

The original isValid check can be enabled by setting the System property
spanner.jdbc.use_legacy_is_valid_check to true or setting the
Environment variable SPANNER_JDBC_USE_LEGACY_IS_VALID_CHECK to true.

Fixes #1443
@olavloite olavloite requested a review from a team as a code owner December 13, 2023 11:53
@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: spanner Issues related to the googleapis/java-spanner-jdbc API. labels Dec 13, 2023
@olavloite olavloite merged commit 914e973 into main Dec 19, 2023
25 checks passed
@olavloite olavloite deleted the optimize-is-valid branch December 19, 2023 06:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the googleapis/java-spanner-jdbc API. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize the isValid method to use a cached value
3 participants