Skip to content

Instantly share code, notes, and snippets.

@edas
Last active May 7, 2020 09:37
Show Gist options
  • Select an option

  • Save edas/f122581a92c278119b21bfd64f813aab to your computer and use it in GitHub Desktop.

Select an option

Save edas/f122581a92c278119b21bfd64f813aab to your computer and use it in GitHub Desktop.

Revisions

  1. edas revised this gist May 7, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions sub transaction pg.rb
    Original file line number Diff line number Diff line change
    @@ -10,9 +10,9 @@ def with_tx(conn)
    conn.exec("SAVEPOINT #{name}")
    yield conn
    conn.exec("RELEASE SAVEPOINT #{name}")
    rescue e
    rescue
    conn.exec("ROLLBACK TO SAVEPOINT #{name}")
    throw e
    raise
    end
    when PQTRANS_INERROR
    raise "Error"
  2. edas created this gist May 7, 2020.
    22 changes: 22 additions & 0 deletions sub transaction pg.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    def with_tx(conn)
    case conn.transaction_status
    when PQTRANS_IDLE, PQTRANS_ACTIVE
    conn.transaction do |conn|
    yield conn
    end
    when PQTRANS_INTRANS
    name = "savepoint_" + SecureRandom::alphanumeric
    begin
    conn.exec("SAVEPOINT #{name}")
    yield conn
    conn.exec("RELEASE SAVEPOINT #{name}")
    rescue e
    conn.exec("ROLLBACK TO SAVEPOINT #{name}")
    throw e
    end
    when PQTRANS_INERROR
    raise "Error"
    when PQTRANS_UNKNOWN
    raise "Unknown"
    end
    end