@@ -155,144 +155,6 @@ def is_connected?(connection); connection.raw_connection.to_java.connected end
155
155
field_reader :connected
156
156
end
157
157
158
- context 'connection-retry' do
159
-
160
- class DataSourceMock
161
- include javax . sql . DataSource
162
-
163
- def initialize ( data_source )
164
- @data_source = data_source
165
- @mockery = [ ]
166
- end
167
-
168
- def return_connection ( times = 1 )
169
- times . times { @mockery << false } ; self
170
- end
171
-
172
- def then ( times = 1 , &block )
173
- times . times { @mockery << block } ; self
174
- end
175
-
176
- def raise_sql_exception ( times = 1 , &block )
177
- self . then ( times ) do
178
- if msg = block ? block . call : nil
179
- e = java . sql . SQLException . new ( msg )
180
- else
181
- e = java . sql . SQLException . new
182
- end
183
- raise e
184
- end
185
- end
186
-
187
- def raise_wrapped_sql_exception ( times = 1 , &block )
188
- self . then ( times ) do
189
- if msg = block ? block . call : nil
190
- e = java . sql . SQLException . new ( msg )
191
- else
192
- e = java . sql . SQLException . new
193
- end
194
- raise wrap_sql_exception ( e )
195
- end
196
- end
197
-
198
- def getConnection ( *args )
199
- if block = @mockery . shift
200
- block . call ( @data_source )
201
- else
202
- @data_source . getConnection ( *args )
203
- end
204
- end
205
-
206
- def wrap_sql_exception ( cause )
207
- error = org . jruby . exceptions . RaiseException . new (
208
- JRuby . runtime , ActiveRecord ::JDBCError , cause . message , true
209
- )
210
- error . initCause ( cause )
211
- error
212
- end
213
-
214
- end
215
-
216
- def self . startup
217
- @@name = JNDI_MYSQL_CONFIG [ :jndi ]
218
- @@data_source = ActiveRecord ::ConnectionAdapters ::JdbcConnection . jndi_lookup @@name
219
- clear_cached_jdbc_connection_factory
220
- end
221
-
222
- def self . shutdown ; rebind! @@data_source if @@data_source end
223
-
224
- def setup
225
- @config = JNDI_MYSQL_CONFIG . merge :retry_count => 5
226
- #, :configure_connection => false
227
- assert @@data_source . is_a? javax . sql . DataSource
228
- self . class . rebind! @data_source = DataSourceMock . new ( @@data_source )
229
- # NOTE: we're assuming here that JNDI connections are lazy ...
230
- ActiveRecord ::Base . establish_connection @config
231
- end
232
-
233
- def teardown
234
- self . class . rebind!
235
- disconnect_if_connected
236
- self . class . clear_cached_jdbc_connection_factory
237
- end
238
-
239
- def self . rebind! ( data_source = @@data_source )
240
- javax . naming . InitialContext . new . rebind @@name , data_source
241
- end
242
-
243
- test 'getConnection() works' do
244
- ActiveRecord ::Base . connection . execute 'SELECT 42'
245
- end
246
-
247
- test 'getConnection() fails' do
248
- @data_source . return_connection ( 1 ) . then ( 5 + 1 ) do
249
- raise java . sql . SQLException . new ( "yet a failure" )
250
- end
251
-
252
- Thread . new { ActiveRecord ::Base . connection . execute 'SELECT 1' } . join
253
- begin
254
- ActiveRecord ::Base . connection . execute 'SELECT 2'
255
- fail 'connection unexpectedly retrieved'
256
- rescue ActiveRecord ::JDBCError => e
257
- assert e . cause
258
- assert_match /yet.a.failure/ , e . message
259
- end
260
- end if ar_version ( '3.0' ) # NOTE: for some reason fails on 2.3
261
-
262
- test 'getConnection() works due retry count' do
263
- @data_source . return_connection .
264
- then { raise java . sql . SQLException . new ( "failure 1" ) } .
265
- then { raise java . sql . SQLException . new ( "failure 2" ) } .
266
- then { raise java . sql . SQLException . new ( "failure 3" ) } .
267
- return_connection ( 1 )
268
-
269
- Thread . new { ActiveRecord ::Base . connection . execute 'SELECT 1' } . join
270
- ActiveRecord ::Base . connection . execute 'SELECT 2'
271
- end
272
-
273
- test 'getConnection() does re-lookup on failure' do
274
- another_data_source = DataSourceMock . new ( @@data_source )
275
-
276
- @data_source . return_connection ( 2 ) .
277
- raise_sql_exception ( 2 ) { 'expected-failure' } .
278
- raise_sql_exception do
279
- self . class . rebind! another_data_source
280
- 'failure after re-bound'
281
- end .
282
- raise_sql_exception ( 5 ) { 'unexpected' } # not expected to be called
283
-
284
- Thread . new { ActiveRecord ::Base . connection . execute 'SELECT 1' } . join
285
- assert_equal @data_source , get_jdbc_connection_factory . data_source
286
-
287
- Thread . new { ActiveRecord ::Base . connection . execute 'SELECT 2' } . join
288
- assert_equal @data_source , get_jdbc_connection_factory . data_source
289
-
290
- ActiveRecord ::Base . connection . execute 'SELECT 3'
291
- assert_not_equal @data_source , get_jdbc_connection_factory . data_source
292
- assert_equal another_data_source , get_jdbc_connection_factory . data_source
293
- end
294
-
295
- end
296
158
297
159
private
298
160
0 commit comments