Class: Glancer::Utils::Transaction
- Inherits:
-
Object
- Object
- Glancer::Utils::Transaction
- Defined in:
- lib/glancer/utils/transaction.rb
Class Method Summary collapse
- .connection_config_name(connection) ⇒ Object
- .make(&block) ⇒ Object
- .read_only_connection ⇒ Object
- .read_only_connection_used? ⇒ Boolean
Class Method Details
.connection_config_name(connection) ⇒ Object
52 53 54 55 56 |
# File 'lib/glancer/utils/transaction.rb', line 52 def self.connection_config_name(connection) connection.pool.db_config.name rescue StandardError "unknown" end |
.make(&block) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/glancer/utils/transaction.rb', line 6 def self.make(&block) original_config = ActiveRecord::Base.connection_db_config connection = read_only_connection || ActiveRecord::Base.connection Glancer::Utils::Logger.info("Utils::Transaction", "Using \e[1;33m#{connection_config_name(connection)}\e[36m connection for query execution.") connection.transaction { yield(connection) } rescue StandardError => e Glancer::Utils::Logger.error("Utils::Transaction", "An error occurred: #{e.}") raise ensure ActiveRecord::Base.establish_connection(original_config) if read_only_connection_used? if defined?(connection) && connection&.transaction_open? Glancer::Utils::Logger.warn("Utils::Transaction", "Transaction was not closed properly. Please check your code.") else Glancer::Utils::Logger.info("Utils::Transaction", "Transaction completed successfully.") end end |
.read_only_connection ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/glancer/utils/transaction.rb', line 28 def self.read_only_connection return nil unless Glancer.configuration.read_only_db Glancer::Utils::Logger.info("Utils::Transaction", "Establishing connection to read-only database...") @used_read_only = true connection = ActiveRecord::Base.establish_connection(Glancer.configuration.read_only_db).connection Glancer::Utils::Logger.info("Utils::Transaction", "Read-only database connection established successfully.") connection rescue StandardError => e Glancer::Utils::Logger.error("Utils::Transaction", "Failed to connect to read-only database: #{e.class} - #{e.}") Glancer::Utils::Logger.debug("Utils::Transaction", "Backtrace:\n#{e.backtrace.join("\n")}") raise Glancer::Error, "Read-only DB connection failed: #{e.}" end |
.read_only_connection_used? ⇒ Boolean
46 47 48 49 50 |
# File 'lib/glancer/utils/transaction.rb', line 46 def self.read_only_connection_used? value = @used_read_only @used_read_only = false # reset value end |