@@ -133,6 +133,25 @@ module LockTestCases
133133 # After the block, current_advisory_lock should be nil regardless
134134 assert_nil model_class . current_advisory_lock
135135 end
136+
137+ test 'does not write and read lock queries from the query cache' do
138+ model_class . connection . cache do
139+ model_class . with_advisory_lock! ( "lock" , timeout_seconds : 0 ) { }
140+ assert_nil model_class . connection . query_cache [ "SELECT GET_LOCK('lock', 0)" ]
141+
142+ # Blocker thread uses a different connection
143+ blocker = Thread . new do
144+ model_class . with_advisory_lock ( "lock" , timeout_seconds : 0 ) { sleep 1 }
145+ end
146+ # blocker should get the lock and not release it in time
147+ sleep 0.5
148+ lock_result = model_class . with_advisory_lock_result ( "lock" , timeout_seconds : 0 ) do
149+ raise "Successfully entered critical region while lock was held by other thread"
150+ end
151+ blocker . join
152+ assert_not ( lock_result . lock_was_acquired? )
153+ end
154+ end
136155 end
137156end
138157
@@ -177,7 +196,7 @@ def setup
177196 # Hold a lock in another connection - need to use the same prefixed name as the gem
178197 other_conn = model_class . connection_pool . checkout
179198 lock_keys = other_conn . lock_keys_for ( lock_name )
180- other_conn . select_value ( "SELECT GET_LOCK(#{ other_conn . quote ( lock_keys . first ) } , 0)" )
199+ other_conn . query_value ( "SELECT GET_LOCK(#{ other_conn . quote ( lock_keys . first ) } , 0)" )
181200
182201 begin
183202 # Attempt to acquire with a short timeout - should fail quickly
@@ -190,7 +209,7 @@ def setup
190209 assert_not result
191210 assert elapsed < 3.0 , "Expected quick timeout, but took #{ elapsed } seconds"
192211 ensure
193- other_conn . select_value ( "SELECT RELEASE_LOCK(#{ other_conn . quote ( lock_keys . first ) } )" )
212+ other_conn . query_value ( "SELECT RELEASE_LOCK(#{ other_conn . quote ( lock_keys . first ) } )" )
194213 model_class . connection_pool . checkin ( other_conn )
195214 end
196215 end
0 commit comments