Skip to content

perf(spanner-jdbc): cache JDBC metadata query strings - #14041

Merged
rahul2393 merged 7 commits into
mainfrom
cache-metadata-queries
Aug 20, 2026
Merged

perf(spanner-jdbc): cache JDBC metadata query strings#14041
rahul2393 merged 7 commits into
mainfrom
cache-metadata-queries

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

Cache the SQL strings that are loaded from disk for the standard JDBC metadata queries.

Cache the SQL strings that are loaded from disk for the standard JDBC metadata queries.
@olavloite
olavloite requested review from a team as code owners August 11, 2026 15:00

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces caching for SQL queries loaded from files in JdbcDatabaseMetaData using a ConcurrentHashMap to improve performance, and adds a corresponding unit test to verify the caching behavior. It also updates the file reading logic to use UTF-8 explicitly and adds a null check for the resource stream. The reviewer suggests managing all closeable resources (InputStream, InputStreamReader, and Scanner) within a try-with-resources block to prevent potential resource leaks and notes that BufferedReader is redundant.

@olavloite

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces caching for SQL files read from resources in JdbcDatabaseMetaData using a ConcurrentHashMap to avoid redundant file I/O operations, and adds corresponding unit tests. The review feedback suggests optimizing the cache lookup by performing a fast get check before calling computeIfAbsent to prevent unnecessary lambda allocations on cache hits.

@olavloite
olavloite force-pushed the cache-metadata-queries branch from 14ca10f to 40d5d98 Compare August 19, 2026 07:24
@olavloite olavloite added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Aug 19, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Aug 19, 2026
@olavloite

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces SQL file caching using a ConcurrentHashMap in JdbcDatabaseMetaData to optimize metadata queries, along with corresponding unit tests. It also updates the MyBatis sample tests to spin up a Spanner emulator container using Testcontainers. The review feedback suggests avoiding blocking I/O inside computeIfAbsent to prevent thread contention, removing the alwaysPull policy on the emulator container to leverage local caches, and clearing modified system properties in the test cleanup phase to ensure JVM isolation.

builder.append(line).append("\n");
}
} catch (IOException e) {
throw SpannerExceptionFactory.newSpannerException(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: earlier there was a chance of NPE, and now better

Just wanted to fyi since the PR title did not mention it

private static final String PRODUCT_NAME = "Google Cloud Spanner";
private static final String POSTGRESQL_PRODUCT_NAME = PRODUCT_NAME + " PostgreSQL";

private static final ConcurrentMap<String, String> SQL_CACHE = new ConcurrentHashMap<>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache is unbounded but negligible risk since metadata are bounded.

@rahul2393
rahul2393 merged commit 31c628f into main Aug 20, 2026
202 checks passed
@rahul2393
rahul2393 deleted the cache-metadata-queries branch August 20, 2026 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants