Skip to content

SAVEPOINT can only be used in transaction blocks #1119

@jwoertink

Description

@jwoertink

When using the table locking my specs are failing with this error. I have this code inside of an operation

it "runs" do
  RunUpdates.run do |op, _val|
    op.valid?.should eq(true)
  end
end
# RunUpdates operation
def run
  AppDatabase.with_lock_on(Thing, mode: :row_exclusive) do
    SaveThing.update!(thing, whatever: true)
  end

  SaveOther.create!(number: 1)
end
       SAVEPOINT can only be used in transaction blocks (PQ::PQError)
         from lib/pg/src/pq/connection.cr:214:7 in 'handle_error'
         from lib/pg/src/pq/connection.cr:197:7 in 'handle_async_frames'
         from lib/pg/src/pq/connection.cr:173:7 in 'read'
         from lib/pg/src/pq/connection.cr:168:7 in 'read'
         from lib/pg/src/pq/connection.cr:446:31 in 'expect_frame'
         from lib/pg/src/pq/connection.cr:445:5 in 'expect_frame'
         from lib/pg/src/pg/result_set.cr:40:15 in 'move_next'
         from lib/db/src/db/result_set.cr:39:13 in 'perform_exec'
         from lib/db/src/db/statement.cr:93:9 in 'perform_exec_and_release'
         from lib/db/src/db/statement.cr:78:7 in 'exec:args'
         from lib/db/src/db/query_methods.cr:275:7 in 'exec'
         from lib/db/src/db/connection.cr:131:7 in 'perform_create_savepoint'
         from lib/avram/src/ext/db/transaction.cr:24:7 in 'initialize'
         from lib/avram/src/ext/db/transaction.cr:21:5 in 'new'
         from lib/db/src/db/transaction.cr:85:7 in 'create_save_point_transaction'
         from lib/db/src/db/transaction.cr:81:7 in 'create_save_point_transaction'
         from lib/db/src/db/transaction.cr:65:7 in 'begin_transaction'
         from lib/db/src/db/begin_transaction.cr:19:12 in 'save'
         from lib/avram/src/avram/save_operation.cr:243:8 in 'save!'

Removing the AppDatabase.with_lock_on block resolves the error. Strange thing is I am using this in another area with no issues... so I'm not sure what's different here 🤔

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedAdditional expertise needed

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions