lib/sinatra/sequel_transaction.rb in wakame-vdc-dcmgr-10.12.0 vs lib/sinatra/sequel_transaction.rb in wakame-vdc-dcmgr-11.06.0
- old
+ new
@@ -3,20 +3,35 @@
require 'sinatra/base'
require 'sequel'
module Sinatra
# wrap routed request with sequel transaction block.
+ # note: this is NOT thread safe. please ensure not to be used in the
+ # multi-threaded apps.
module SequelTransaction
module Helpers
+ # TODO: abstract database connection. it means that do not use
+ # Sequel::DATABASE.first where to get the connection.
+
+ public
+ # commit manually before return from the request block
+ def commit_transaction
+ db = Sequel::DATABASES.first
+ db << db.__send__(:commit_transaction_sql)
+ end
+
private
def route_eval(&block)
+
db = Sequel::DATABASES.first
-
- ret = nil
- db.transaction do
- ret = instance_eval(&block)
+ begin
+ db.transaction do
+ super(&block)
+ end
+ rescue Sequel::DatabaseError, Sequel::DatabaseConnectionError => e
+ db.disconnect
+ raise e
end
- throw :halt, ret
end
end
def self.registered(app)
app.helpers SequelTransaction::Helpers