Skip to content

Instantly share code, notes, and snippets.

@edas
Last active May 7, 2020 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edas/f122581a92c278119b21bfd64f813aab to your computer and use it in GitHub Desktop.
Save edas/f122581a92c278119b21bfd64f813aab to your computer and use it in GitHub Desktop.
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
conn.exec("ROLLBACK TO SAVEPOINT #{name}")
raise
end
when PQTRANS_INERROR
raise "Error"
when PQTRANS_UNKNOWN
raise "Unknown"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment