Skip to content

Comments

CMR-11083: Switch a SQL transaction to read only#2381

Merged
jceaser merged 3 commits intomasterfrom
cmr-11083-readonly-transaction
Feb 18, 2026
Merged

CMR-11083: Switch a SQL transaction to read only#2381
jceaser merged 3 commits intomasterfrom
cmr-11083-readonly-transaction

Conversation

@jceaser
Copy link
Contributor

@jceaser jceaser commented Feb 11, 2026

Overview

What is the objective?

Change the search transaction in find-concepts-in-batches to readonly and test. This change is because the transaction causes an error if it takes to long and SQL closes it. This happens when doing a bulk index of granules.

What are the changes?

Added ":read-committed :read-only? true" to the transaction call as is documented on line and in the macro documentation for the library (see defmacro with-db-transaction).

What areas of the application does this impact?

This error happens when running bulk index through bootstrap

Required Checklist

  • New and existing unit and int tests pass locally and remotely
  • clj-kondo has been run locally and all errors in changed files are corrected
  • I have commented my code, particularly in hard-to-understand areas
  • I have made changes to the documentation (if necessary)
  • My changes generate no new warnings

Additional Checklist

  • I have removed unnecessary/dead code and imports in files I have changed
  • I have cleaned up integration tests by doing one or more of the following:
    • migrated any are2 tests to are3 in files I have changed
    • de-duped, consolidated, removed dead int tests
    • transformed applicable int tests into unit tests
    • reduced number of system state resets by updating fixtures. Ex) (use-fixtures :each (ingest/reset-fixture {})) to be :once instead of :each

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of batch data retrieval by enforcing read-committed, read-only transactional behavior for batch queries.
  • Chores
    • Added informational logging around batch processing, including pre-query notices and per-batch result counts.

@coderabbitai
Copy link

coderabbitai bot commented Feb 11, 2026

📝 Walkthrough

Walkthrough

Batch concept queries in the search module now run inside transactions explicitly set to read-committed isolation and marked read-only; logging around batch transaction start and per-batch result counts was added. No public APIs or function signatures changed.

Changes

Cohort / File(s) Summary
Batch Query Transaction & Logging
metadata-db-app/src/cmr/metadata_db/data/oracle/search.clj
find-concepts-in-batches now opens transactions with explicit READ-COMMITTED isolation and read-only flag; pre-transaction log message updated and per-batch result-count informational logging added. No public signatures changed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 I hopped to the DB with care and grace,
Read-committed paths in a quiet place,
No scribbles left, just gentle peeks,
Counting batches, a calm technique,
Cheers — a rabbit's tidy trace! 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: converting a SQL transaction to read-only mode, which aligns with the changeset's core modification.
Description check ✅ Passed The description provides the objective, changes made, and impacted areas as required; however, all checklist items remain unchecked, indicating incomplete verification before submission.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch cmr-11083-readonly-transaction

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@jceaser jceaser marked this pull request as ready for review February 11, 2026 15:33
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
metadata-db-app/src/cmr/metadata_db/data/oracle/search.clj (1)

249-264: ⚠️ Potential issue | 🔴 Critical

The read-only transaction is bypassed — query executes on db instead of conn.

Line 264 passes db (the original datasource) to su/query instead of conn (the transactional connection bound by with-db-transaction). This means the query runs outside the transaction, so the :isolation :read-committed :read-only? true options are not applied to the actual SQL execution.

The same issue occurs at line 301 in find-concepts-in-batches-with-stmt, but find-concepts-in-table (line 226) correctly demonstrates the proper pattern by passing conn to su/query within the transaction block.

Proposed fix
-                      batch-result (su/query db stmt)]
+                      batch-result (su/query conn stmt)]

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
metadata-db-app/src/cmr/metadata_db/data/oracle/search.clj (1)

249-267: ⚠️ Potential issue | 🔴 Critical

Critical bug: Query bypasses the read-only transaction — uses db instead of conn on line 267.

Line 267 executes (su/query db stmt) using the raw datasource db, not the transaction-bound connection conn established on line 250. The :read-only? true :isolation :read-committed options have no effect on the actual query. This defeats the entire purpose of this PR.

For comparison, every other transaction block in this file (lines 196, 226, 367) correctly uses conn for queries. Lines 192–196 and 217–226 show the correct pattern: they declare [conn db] with transaction options and execute su/query conn stmt.

Proposed fix
                      batch-result (su/query db stmt)]
+                     batch-result (su/query conn stmt)]
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@metadata-db-app/src/cmr/metadata_db/data/oracle/search.clj` around lines 249
- 267, The query inside the j/with-db-transaction is using the raw datasource
`db` instead of the transaction-bound `conn`, so the read-only and isolation
options are ignored; change the call to use `conn` (i.e., replace the `su/query
db stmt` invocation with `su/query conn stmt`) so the SQL statement built in
`stmt` executes within the `j/with-db-transaction` transaction boundary (symbols
to update: j/with-db-transaction, conn, db, su/query, stmt).
🧹 Nitpick comments (2)
metadata-db-app/src/cmr/metadata_db/data/oracle/search.clj (2)

301-315: Consider applying the same read-only transaction treatment to find-concepts-in-batches-with-stmt.

This sibling function (not changed in this PR) has the same pattern: it opens a transaction but queries with db instead of conn (line 312), and lacks the :read-only? / :isolation options. If the read-only transaction fix addresses transaction-closure errors for find-concepts-in-batches, this function likely needs it too.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@metadata-db-app/src/cmr/metadata_db/data/oracle/search.clj` around lines 301
- 315, The sibling function find-concepts-in-batches-with-stmt opens a
transaction but still executes queries against db and does not mark the tx
read-only; mirror the fix from find-batch by changing the j/with-db-transaction
call in find-concepts-in-batches-with-stmt to bind [conn db] with options
{:read-only? true :isolation :read-committed} (or the same options used in
find-batch), run the su/query against conn (or pass conn as the DB argument used
elsewhere), and ensure subsequent mapping calls use conn (e.g.,
oc/db-result->concept-map with conn) so the query runs inside the read-only
transaction.

253-275: Logging at info level for every batch may be noisy during bulk indexing.

During bulk granule indexing (bootstrap), this will produce two info log lines per batch. If there are thousands of batches, consider using debug instead, or at least for the per-batch result count log (lines 268–275), to reduce log volume in production.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@metadata-db-app/src/cmr/metadata_db/data/oracle/search.clj` around lines 253
- 275, The two per-batch info logs in the find-concepts-in-batches block are too
noisy; change the second info call that logs batch-result count to use debug
instead so per-batch counts are emitted at debug level (replace the (info
(format ... (count batch-result) ...)) with a debug-level log), keeping the
initial informational context log as-is if desired; reference the (info ...)
that formats "Found %d results in batch for provider [%s] concept type ..." and
the batch-result/provider-id/concept-type/start-index/batch-size symbols to
locate the exact call to change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@metadata-db-app/src/cmr/metadata_db/data/oracle/search.clj`:
- Around line 249-267: The query inside the j/with-db-transaction is using the
raw datasource `db` instead of the transaction-bound `conn`, so the read-only
and isolation options are ignored; change the call to use `conn` (i.e., replace
the `su/query db stmt` invocation with `su/query conn stmt`) so the SQL
statement built in `stmt` executes within the `j/with-db-transaction`
transaction boundary (symbols to update: j/with-db-transaction, conn, db,
su/query, stmt).

---

Nitpick comments:
In `@metadata-db-app/src/cmr/metadata_db/data/oracle/search.clj`:
- Around line 301-315: The sibling function find-concepts-in-batches-with-stmt
opens a transaction but still executes queries against db and does not mark the
tx read-only; mirror the fix from find-batch by changing the
j/with-db-transaction call in find-concepts-in-batches-with-stmt to bind [conn
db] with options {:read-only? true :isolation :read-committed} (or the same
options used in find-batch), run the su/query against conn (or pass conn as the
DB argument used elsewhere), and ensure subsequent mapping calls use conn (e.g.,
oc/db-result->concept-map with conn) so the query runs inside the read-only
transaction.
- Around line 253-275: The two per-batch info logs in the
find-concepts-in-batches block are too noisy; change the second info call that
logs batch-result count to use debug instead so per-batch counts are emitted at
debug level (replace the (info (format ... (count batch-result) ...)) with a
debug-level log), keeping the initial informational context log as-is if
desired; reference the (info ...) that formats "Found %d results in batch for
provider [%s] concept type ..." and the
batch-result/provider-id/concept-type/start-index/batch-size symbols to locate
the exact call to change.

@codecov-commenter
Copy link

codecov-commenter commented Feb 17, 2026

Codecov Report

❌ Patch coverage is 10.00000% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.02%. Comparing base (6ac3971) to head (5e370a8).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...-db-app/src/cmr/metadata_db/data/oracle/search.clj 10.00% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2381      +/-   ##
==========================================
- Coverage   58.04%   58.02%   -0.02%     
==========================================
  Files        1065     1065              
  Lines       72992    72998       +6     
  Branches     2119     2117       -2     
==========================================
- Hits        42365    42356       -9     
- Misses      28659    28668       +9     
- Partials     1968     1974       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jceaser jceaser merged commit b72194d into master Feb 18, 2026
7 checks passed
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.

5 participants