mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
44 lines
730 B
Crystal
44 lines
730 B
Crystal
|
module DB
|
||
|
abstract class Transaction
|
||
|
include Disposable
|
||
|
|
||
|
abstract def connection : Connection
|
||
|
|
||
|
def commit
|
||
|
close!
|
||
|
end
|
||
|
|
||
|
def rollback
|
||
|
close!
|
||
|
end
|
||
|
|
||
|
private def close!
|
||
|
raise DB::Error.new("Transaction already closed") if closed?
|
||
|
close
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class TopLevelTransaction < Transaction
|
||
|
# :nodoc:
|
||
|
getter connection
|
||
|
|
||
|
def initialize(@connection : Connection)
|
||
|
@connection.perform_begin_transaction
|
||
|
end
|
||
|
|
||
|
def commit
|
||
|
@connection.perform_commit_transaction
|
||
|
close!
|
||
|
end
|
||
|
|
||
|
def rollback
|
||
|
@connection.perform_rollback_transaction
|
||
|
close!
|
||
|
end
|
||
|
|
||
|
protected def do_close
|
||
|
connection.release_from_transaction
|
||
|
end
|
||
|
end
|
||
|
end
|