19
19
import java .sql .Array ;
20
20
import java .sql .Blob ;
21
21
import java .sql .CallableStatement ;
22
+ import java .sql .ClientInfoStatus ;
22
23
import java .sql .Clob ;
23
24
import java .sql .Connection ;
24
25
import java .sql .DatabaseMetaData ;
36
37
import java .sql .Statement ;
37
38
import java .sql .Struct ;
38
39
import java .util .Arrays ;
40
+ import java .util .Collection ;
41
+ import java .util .Collections ;
42
+ import java .util .HashMap ;
39
43
import java .util .List ;
40
44
import java .util .Map ;
41
45
import java .util .Properties ;
@@ -213,22 +217,24 @@ public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) thr
213
217
214
218
@ Override
215
219
public PreparedStatement prepareStatement (String sql , int [] columnIndexes ) throws SQLException {
220
+ checkNotClosed ();
216
221
throw new SQLFeatureNotSupportedException ();
217
222
}
218
223
219
224
@ Override
220
225
public PreparedStatement prepareStatement (String sql , String [] columnNames ) throws SQLException {
226
+ checkNotClosed ();
221
227
throw new SQLFeatureNotSupportedException ();
222
228
}
223
229
224
230
@ Override
225
231
public CallableStatement prepareCall (String sql ) throws SQLException {
226
- throw new SQLFeatureNotSupportedException ( );
232
+ return prepareCall ( sql , ResultSet . TYPE_FORWARD_ONLY , ResultSet . CONCUR_READ_ONLY );
227
233
}
228
234
229
235
@ Override
230
236
public CallableStatement prepareCall (String sql , int resultSetType , int resultSetConcurrency ) throws SQLException {
231
- throw new SQLFeatureNotSupportedException ( );
237
+ return prepareCall ( sql , resultSetType , resultSetConcurrency , getHoldability () );
232
238
}
233
239
234
240
@ Override
@@ -237,38 +243,63 @@ public CallableStatement prepareCall(String sql,
237
243
int resultSetConcurrency ,
238
244
int resultSetHoldability )
239
245
throws SQLException {
246
+ checkNotClosed ();
240
247
throw new SQLFeatureNotSupportedException ();
241
248
}
242
249
243
250
@ Override
244
251
public String nativeSQL (String sql ) throws SQLException {
252
+ checkNotClosed ();
245
253
throw new SQLFeatureNotSupportedException ();
246
254
}
247
255
248
256
@ Override
249
257
public void setAutoCommit (boolean autoCommit ) throws SQLException {
258
+ checkNotClosed ();
250
259
if (!autoCommit ) {
251
260
throw new SQLFeatureNotSupportedException ();
252
261
}
253
262
}
254
263
255
264
@ Override
256
265
public boolean getAutoCommit () throws SQLException {
266
+ checkNotClosed ();
257
267
return true ;
258
268
}
259
269
260
270
@ Override
261
271
public void commit () throws SQLException {
272
+ checkNotClosed ();
273
+ if (getAutoCommit ()) {
274
+ throw new SQLNonTransientException (
275
+ "Cannot commit when auto-commit is enabled." ,
276
+ SQLStates .INVALID_TRANSACTION_STATE .getSqlState ()
277
+ );
278
+ }
262
279
throw new SQLFeatureNotSupportedException ();
263
280
}
264
281
265
282
@ Override
266
283
public void rollback () throws SQLException {
284
+ checkNotClosed ();
285
+ if (getAutoCommit ()) {
286
+ throw new SQLNonTransientException (
287
+ "Cannot rollback when auto-commit is enabled." ,
288
+ SQLStates .INVALID_TRANSACTION_STATE .getSqlState ()
289
+ );
290
+ }
267
291
throw new SQLFeatureNotSupportedException ();
268
292
}
269
293
270
294
@ Override
271
295
public void rollback (Savepoint savepoint ) throws SQLException {
296
+ checkNotClosed ();
297
+ if (getAutoCommit ()) {
298
+ throw new SQLNonTransientException (
299
+ "Cannot roll back to a savepoint when auto-commit is enabled." ,
300
+ SQLStates .INVALID_TRANSACTION_STATE .getSqlState ()
301
+ );
302
+ }
272
303
throw new SQLFeatureNotSupportedException ();
273
304
}
274
305
@@ -293,52 +324,61 @@ public DatabaseMetaData getMetaData() throws SQLException {
293
324
294
325
@ Override
295
326
public void setReadOnly (boolean readOnly ) throws SQLException {
296
-
327
+ checkNotClosed ();
328
+ throw new SQLFeatureNotSupportedException ();
297
329
}
298
330
299
331
@ Override
300
332
public boolean isReadOnly () throws SQLException {
333
+ checkNotClosed ();
301
334
return false ;
302
335
}
303
336
304
337
@ Override
305
338
public void setCatalog (String catalog ) throws SQLException {
339
+ checkNotClosed ();
306
340
}
307
341
308
342
@ Override
309
343
public String getCatalog () throws SQLException {
344
+ checkNotClosed ();
310
345
return null ;
311
346
}
312
347
313
348
@ Override
314
349
public void setTransactionIsolation (int level ) throws SQLException {
350
+ checkNotClosed ();
315
351
if (level != Connection .TRANSACTION_NONE ) {
316
352
throw new SQLFeatureNotSupportedException ();
317
353
}
318
354
}
319
355
320
356
@ Override
321
357
public int getTransactionIsolation () throws SQLException {
358
+ checkNotClosed ();
322
359
return Connection .TRANSACTION_NONE ;
323
360
}
324
361
325
362
@ Override
326
363
public SQLWarning getWarnings () throws SQLException {
364
+ checkNotClosed ();
327
365
return null ;
328
366
}
329
367
330
368
@ Override
331
369
public void clearWarnings () throws SQLException {
332
-
370
+ checkNotClosed ();
333
371
}
334
372
335
373
@ Override
336
374
public Map <String , Class <?>> getTypeMap () throws SQLException {
375
+ checkNotClosed ();
337
376
throw new SQLFeatureNotSupportedException ();
338
377
}
339
378
340
379
@ Override
341
380
public void setTypeMap (Map <String , Class <?>> map ) throws SQLException {
381
+ checkNotClosed ();
342
382
throw new SQLFeatureNotSupportedException ();
343
383
}
344
384
@@ -360,36 +400,55 @@ public int getHoldability() throws SQLException {
360
400
361
401
@ Override
362
402
public Savepoint setSavepoint () throws SQLException {
403
+ checkNotClosed ();
404
+ if (getAutoCommit ()) {
405
+ throw new SQLNonTransientException (
406
+ "Cannot set a savepoint when auto-commit is enabled." ,
407
+ SQLStates .INVALID_TRANSACTION_STATE .getSqlState ()
408
+ );
409
+ }
363
410
throw new SQLFeatureNotSupportedException ();
364
411
}
365
412
366
413
@ Override
367
414
public Savepoint setSavepoint (String name ) throws SQLException {
415
+ checkNotClosed ();
416
+ if (getAutoCommit ()) {
417
+ throw new SQLNonTransientException (
418
+ "Cannot set a savepoint when auto-commit is enabled." ,
419
+ SQLStates .INVALID_TRANSACTION_STATE .getSqlState ()
420
+ );
421
+ }
368
422
throw new SQLFeatureNotSupportedException ();
369
423
}
370
424
371
425
@ Override
372
426
public void releaseSavepoint (Savepoint savepoint ) throws SQLException {
427
+ checkNotClosed ();
373
428
throw new SQLFeatureNotSupportedException ();
374
429
}
375
430
376
431
@ Override
377
432
public Clob createClob () throws SQLException {
433
+ checkNotClosed ();
378
434
throw new SQLFeatureNotSupportedException ();
379
435
}
380
436
381
437
@ Override
382
438
public Blob createBlob () throws SQLException {
439
+ checkNotClosed ();
383
440
throw new SQLFeatureNotSupportedException ();
384
441
}
385
442
386
443
@ Override
387
444
public NClob createNClob () throws SQLException {
445
+ checkNotClosed ();
388
446
throw new SQLFeatureNotSupportedException ();
389
447
}
390
448
391
449
@ Override
392
450
public SQLXML createSQLXML () throws SQLException {
451
+ checkNotClosed ();
393
452
throw new SQLFeatureNotSupportedException ();
394
453
}
395
454
@@ -431,45 +490,99 @@ private boolean checkConnection(int timeout) {
431
490
432
491
@ Override
433
492
public void setClientInfo (String name , String value ) throws SQLClientInfoException {
434
- throw new SQLClientInfoException ();
493
+ try {
494
+ checkNotClosed ();
495
+ } catch (SQLException cause ) {
496
+ throwUnknownReasonClientProperties ("Connection is closed" , Collections .singleton (name ), cause );
497
+ }
498
+ throwUnknownClientProperties (Collections .singleton (name ));
435
499
}
436
500
437
501
@ Override
438
502
public void setClientInfo (Properties properties ) throws SQLClientInfoException {
439
- throw new SQLClientInfoException ();
503
+ try {
504
+ checkNotClosed ();
505
+ } catch (SQLException cause ) {
506
+ throwUnknownReasonClientProperties ("Connection is closed" , properties .keySet (), cause );
507
+ }
508
+ throwUnknownClientProperties (properties .keySet ());
509
+ }
510
+
511
+ /**
512
+ * Throws an exception caused by {@code cause} and marks all properties
513
+ * as {@link ClientInfoStatus#REASON_UNKNOWN}.
514
+ *
515
+ * @param reason reason mesage
516
+ * @param properties client properties
517
+ * @param cause original cause
518
+ *
519
+ * @throws SQLClientInfoException wrapped exception
520
+ */
521
+ private void throwUnknownReasonClientProperties (String reason ,
522
+ Collection <Object > properties ,
523
+ SQLException cause ) throws SQLClientInfoException {
524
+ Map <String , ClientInfoStatus > failedProperties = new HashMap <>();
525
+ properties .forEach (property -> {
526
+ failedProperties .put (property .toString (), ClientInfoStatus .REASON_UNKNOWN );
527
+ });
528
+ throw new SQLClientInfoException (reason , cause .getSQLState (), failedProperties , cause );
529
+ }
530
+
531
+ /**
532
+ * Throws exception for unrecognizable properties.
533
+ *
534
+ * @param properties unknown property names.
535
+ *
536
+ * @throws SQLClientInfoException wrapped exception
537
+ */
538
+ private void throwUnknownClientProperties (Collection <Object > properties ) throws SQLClientInfoException {
539
+ Map <String , ClientInfoStatus > failedProperties = new HashMap <>();
540
+ properties .forEach (property -> {
541
+ failedProperties .put (property .toString (), ClientInfoStatus .REASON_UNKNOWN_PROPERTY );
542
+ });
543
+ throw new SQLClientInfoException (failedProperties );
440
544
}
441
545
442
546
@ Override
443
547
public String getClientInfo (String name ) throws SQLException {
548
+ checkNotClosed ();
444
549
throw new SQLFeatureNotSupportedException ();
445
550
}
446
551
447
552
@ Override
448
553
public Properties getClientInfo () throws SQLException {
554
+ checkNotClosed ();
449
555
throw new SQLFeatureNotSupportedException ();
450
556
}
451
557
452
558
@ Override
453
559
public Array createArrayOf (String typeName , Object [] elements ) throws SQLException {
560
+ checkNotClosed ();
454
561
throw new SQLFeatureNotSupportedException ();
455
562
}
456
563
457
564
@ Override
458
565
public Struct createStruct (String typeName , Object [] attributes ) throws SQLException {
566
+ checkNotClosed ();
459
567
throw new SQLFeatureNotSupportedException ();
460
568
}
461
569
462
570
@ Override
463
571
public void setSchema (String schema ) throws SQLException {
572
+ checkNotClosed ();
464
573
}
465
574
466
575
@ Override
467
576
public String getSchema () throws SQLException {
577
+ checkNotClosed ();
468
578
return null ;
469
579
}
470
580
471
581
@ Override
472
582
public void abort (Executor executor ) throws SQLException {
583
+ if (isClosed ()) {
584
+ return ;
585
+ }
473
586
throw new SQLFeatureNotSupportedException ();
474
587
}
475
588
0 commit comments