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

ActiveRecord::PreparedStatementCacheExpired errors due to document after save hook #3774

Open
jsugarman opened this issue Feb 26, 2021 · 1 comment

Comments

@jsugarman
Copy link
Contributor

jsugarman commented Feb 26, 2021

How to replicate
Deployment of the application which includes a migration against the documents table (Document model) coupled with saving a claim (on a server instance that has not been restarted since the migration?!)

Impact
Will raise an ActiveRecord::PreparedStatementCacheExpired error and users creating or updating claims
will experience a 500 error and possibly lose some changes they have made as a result.

Cause
The code below means that any save on claim queries and could attempt to update the document table. I believe the query itself is what can raise the error. If the document table has been migrated the query's prepared statement expires. Existing sessions on a server that have not been restarted following the migration will therefore be using an expired statement?!

    # app/models/claim/base_claim.rb
    after_save :find_and_associate_documents, :update_vat
    ...
    ...
    def find_and_associate_documents
      return if form_id.nil?

      Document.where(form_id: form_id).find_each do |document| <---- this line raises error
        document.update_column(:claim_id, id)
        document.update_column(:external_user_id, external_user_id)
      end
    end

Fix

This article is useful background reading but we should question whether we even need this after_save hook now.

@jrmhaig
Copy link
Contributor

jrmhaig commented Feb 26, 2021

Further to the comment about whether we need the after_save block, it is possible that we can set the claim_id and external_user_id fields when the document is uploaded rather than when the claim is submitted.

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

No branches or pull requests

2 participants