Last active
May 7, 2020 09:37
-
-
Save edas/f122581a92c278119b21bfd64f813aab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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