Class: SolidObjects::DatabaseAdapters::Sqlite
Constant Summary
collapse
- LOCK_RETRY_INTERVAL =
0.001
- LOCK_RETRY_MUTEX =
Thread::Mutex.new
- LOCK_RETRY_CONDITION =
Thread::ConditionVariable.new
Instance Attribute Summary
#connection_pool, #fixed_connection
Instance Method Summary
collapse
#claim_lock, #configure_transaction_deadline, #database_now, for, #initialize, #lock_candidates, #supports_skip_locked?, #with_connection
Instance Method Details
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/solid_objects/database_adapters/sqlite.rb', line 104
def configured_busy_handler_timeout(connection)
return nil unless connection.respond_to?(:raw_connection)
return nil unless connection.raw_connection.respond_to?(:busy_handler_timeout=)
pool = connection.respond_to?(:pool) ? connection.pool : nil
return nil unless pool.respond_to?(:db_config)
timeout = pool.db_config.configuration_hash[:timeout]
timeout&.to_i
end
|
#current_time_expression ⇒ String
11
12
13
|
# File 'lib/solid_objects/database_adapters/sqlite.rb', line 11
def current_time_expression
"STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')"
end
|
#deadline_error?(error) ⇒ Boolean
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/solid_objects/database_adapters/sqlite.rb', line 116
def deadline_error?(error)
return false unless SyncDeadline.active?
cause = error
while cause
return true if cause.class.name.match?(/BusyException|BusyError/)
cause = cause.cause
end
false
end
|
#restorable_busy_wait(connection) ⇒ Hash[Symbol, untyped]?
82
83
84
85
86
87
88
89
90
|
# File 'lib/solid_objects/database_adapters/sqlite.rb', line 82
def restorable_busy_wait(connection)
pragma_timeout = connection.select_value("PRAGMA busy_timeout").to_i
return { pragma_timeout: } if pragma_timeout.positive?
handler_timeout = configured_busy_handler_timeout(connection)
return nil unless handler_timeout
{ pragma_timeout:, handler_timeout: }
end
|
#restore_busy_wait(connection, busy_wait) ⇒ void
This method returns an undefined value.
93
94
95
96
97
98
99
100
101
|
# File 'lib/solid_objects/database_adapters/sqlite.rb', line 93
def restore_busy_wait(connection, busy_wait)
handler_timeout = busy_wait[:handler_timeout]
if handler_timeout
connection.raw_connection.busy_handler_timeout = handler_timeout
return
end
connection.execute("PRAGMA busy_timeout = #{busy_wait.fetch(:pragma_timeout)}")
end
|
#transaction { ... } ⇒ Object
16
17
18
19
20
|
# File 'lib/solid_objects/database_adapters/sqlite.rb', line 16
def transaction(&block)
return super unless SyncDeadline.active?
with_lock_retry { super }
end
|
#wait_before_retry ⇒ void
This method returns an undefined value.
#with_lock_probe { ... } ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/solid_objects/database_adapters/sqlite.rb', line 50
def with_lock_probe
return yield unless SyncDeadline.active?
with_connection do |connection|
with_transaction_deadline(connection) { yield }
end
rescue => error
raise unless deadline_error?(error)
raise DatabaseDeadlineExceeded,
"database remained locked at the synchronous invocation deadline",
cause: error
end
|
#with_lock_retry { ... } ⇒ Object
#with_transaction_deadline(connection) { ... } ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/solid_objects/database_adapters/sqlite.rb', line 67
def with_transaction_deadline(connection)
return yield unless SyncDeadline.active?
busy_wait = restorable_busy_wait(connection)
return yield unless busy_wait
begin
connection.execute("PRAGMA busy_timeout = 0")
yield
ensure
restore_busy_wait(connection, busy_wait)
end
end
|