Skip to content

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

Open
olavloite wants to merge 1 commit into
mainfrom
cache-metadata-queries
Open

perf(spanner-jdbc): cache JDBC metadata query strings#14041
olavloite wants to merge 1 commit 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.

Comment on lines +69 to 71
BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
StringBuilder builder = new StringBuilder();
try (Scanner scanner = new Scanner(reader)) {

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.

medium

To prevent potential resource leaks and ensure exception safety, all closeable resources (InputStream, InputStreamReader, and Scanner) should be managed within the try-with-resources block. If an exception occurs during the initialization of BufferedReader or Scanner, the underlying InputStream might not be closed. Additionally, BufferedReader is redundant here because Scanner performs its own buffering.

    StringBuilder builder = new StringBuilder();
    try (InputStream input = in;
        InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8);
        Scanner scanner = new Scanner(reader)) {
References
  1. When managing a collection of closeable resources, ensure they are closed in the reverse order of their creation (LIFO). The implementation must be exception-safe to prevent resource leaks, meaning all opened resources should be closed even if exceptions occur during their creation or closing.

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.

1 participant