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

Close a transaction when returning from within its block #167

Conversation

jgaskins
Copy link
Contributor

@jgaskins jgaskins commented Apr 14, 2022

If you open a transaction inside a method and return from that method, the transaction remains open. Seems this else doesn't get invoked on a return.

Reproduction:

require "db"
require "pg"
require "log"

# To show that the transaction is never COMMITed nor ROLLBACKed
Log.setup :debug

pg = DB.open("postgres:///")

2.times { do_the_thing pg }

def do_the_thing(pg)
  pg.transaction do |txn|
    rs = txn.connection.exec "select 42"

    return
  end
end

To do this requires an ensure block, but that would also execute alongside every rescue and the else, so instead I just made the rescue blocks flip a bit and the ensure block does the work based on the state of that bit.

@straight-shoota straight-shoota merged commit e076a08 into crystal-lang:master Oct 27, 2022
@jgaskins jgaskins deleted the commit-when-returning-inside-txn-block branch August 5, 2023 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants