Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
PStore#transaction
transaction(read_only = false) { |pstore| ... }
lib/pstore.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/lib/pstore.rb, line 551
def transaction(read_only = false) # :yields: pstore
value = nil
if !@thread_safe
raise PStore::Error, "nested transaction" unless @lock.try_lock
else
begin
@lock.lock
rescue ThreadError
raise PStore::Error, "nested transaction"
end
end
begin
@rdonly = read_only
@abort = false
file = open_and_lock_file(@filename, read_only)
if file
begin
@table, checksum, original_data_size = load_data(file, read_only)
catch(:pstore_abort_transaction) do
value = yield(self)
end
if !@abort && !read_only
save_data(checksum, original_data_size, file)
end
ensure
file.close
end
else
# This can only occur if read_only == true.
@table = {}
catch(:pstore_abort_transaction) do
value = yield(self)
end
end
ensure
@lock.unlock
end
value
end
Opens a transaction block for the store. See Transactions.
With argument read_only as false, the block may both read from and write to the store.
With argument read_only as true, the block may not include calls to transaction, []=, or delete.
Raises an exception if called within a transaction block.
See also
#save_data_with_fast_strategy
#save_data_with_atomic_file_rename_strategy
also in
3.4 · 3.2 · 3.1 · 3.0