/
DrillConnectionImpl.java
831 lines (738 loc) · 26.5 KB
/
DrillConnectionImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.drill.jdbc.impl;
import java.io.IOException;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLNonTransientConnectionException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
import java.util.concurrent.Executor;
import net.hydromatic.avatica.AvaticaConnection;
import net.hydromatic.avatica.AvaticaFactory;
import net.hydromatic.avatica.AvaticaStatement;
import net.hydromatic.avatica.Helper;
import net.hydromatic.avatica.Meta;
import net.hydromatic.avatica.UnregisteredDriver;
import org.apache.drill.common.config.DrillConfig;
import org.apache.drill.common.exceptions.DrillRuntimeException;
import org.apache.drill.common.exceptions.UserException;
import org.apache.drill.exec.client.DrillClient;
import org.apache.drill.exec.memory.BufferAllocator;
import org.apache.drill.exec.memory.OutOfMemoryException;
import org.apache.drill.exec.memory.RootAllocatorFactory;
import org.apache.drill.exec.rpc.RpcException;
import org.apache.drill.exec.server.Drillbit;
import org.apache.drill.exec.server.RemoteServiceSet;
import org.apache.drill.exec.store.StoragePluginRegistry;
import org.apache.drill.exec.util.TestUtilities;
import org.apache.drill.jdbc.AlreadyClosedSqlException;
import org.apache.drill.jdbc.DrillConnection;
import org.apache.drill.jdbc.DrillConnectionConfig;
import org.apache.drill.jdbc.InvalidParameterSqlException;
import org.apache.drill.jdbc.JdbcApiSqlException;
import org.slf4j.Logger;
/**
* Drill's implementation of {@link Connection}.
*/
// (Was abstract to avoid errors _here_ if newer versions of JDBC added
// interface methods, but now newer versions would probably use Java 8's default
// methods for compatibility.)
class DrillConnectionImpl extends AvaticaConnection
implements DrillConnection {
private static final org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(DrillConnection.class);
final DrillStatementRegistry openStatementsRegistry = new DrillStatementRegistry();
final DrillConnectionConfig config;
private final DrillClient client;
private final BufferAllocator allocator;
private Drillbit bit;
private RemoteServiceSet serviceSet;
protected DrillConnectionImpl(DriverImpl driver, AvaticaFactory factory,
String url, Properties info) throws SQLException {
super(driver, factory, url, info);
// Initialize transaction-related settings per Drill behavior.
super.setTransactionIsolation( TRANSACTION_NONE );
super.setAutoCommit( true );
this.config = new DrillConnectionConfig(info);
try {
if (config.isLocal()) {
try {
Class.forName("org.eclipse.jetty.server.Handler");
} catch (final ClassNotFoundException e) {
throw new SQLNonTransientConnectionException(
"Running Drill in embedded mode using Drill's jdbc-all JDBC"
+ " driver Jar file alone is not supported.", e);
}
final DrillConfig dConfig = DrillConfig.create(info);
this.allocator = RootAllocatorFactory.newRoot(dConfig);
RemoteServiceSet set = GlobalServiceSetReference.SETS.get();
if (set == null) {
// We're embedded; start a local drill bit.
serviceSet = RemoteServiceSet.getLocalServiceSet();
set = serviceSet;
try {
bit = new Drillbit(dConfig, serviceSet);
bit.run();
} catch (final UserException e) {
throw new SQLException(
"Failure in starting embedded Drillbit: " + e.getMessage(),
e);
} catch (Exception e) {
// (Include cause exception's text in wrapping exception's text so
// it's more likely to get to user (e.g., via SQLLine), and use
// toString() since getMessage() text doesn't always mention error:)
throw new SQLException("Failure in starting embedded Drillbit: " + e, e);
}
} else {
serviceSet = null;
bit = null;
}
makeTmpSchemaLocationsUnique(bit.getContext().getStorage(), info);
this.client = new DrillClient(dConfig, set.getCoordinator());
this.client.connect(null, info);
} else if(config.isDirect()) {
final DrillConfig dConfig = DrillConfig.forClient();
this.allocator = RootAllocatorFactory.newRoot(dConfig);
this.client = new DrillClient(dConfig, true); // Get a direct connection
this.client.connect(config.getZookeeperConnectionString(), info);
} else {
final DrillConfig dConfig = DrillConfig.forClient();
this.allocator = RootAllocatorFactory.newRoot(dConfig);
// TODO: Check: Why does new DrillClient() create another DrillConfig,
// with enableServerConfigs true, and cause scanning for function
// implementations (needed by a server, but not by a client-only
// process, right?)? Probably pass dConfig to construction.
this.client = new DrillClient();
this.client.connect(config.getZookeeperConnectionString(), info);
}
} catch (OutOfMemoryException e) {
throw new SQLException("Failure creating root allocator", e);
} catch (RpcException e) {
// (Include cause exception's text in wrapping exception's text so
// it's more likely to get to user (e.g., via SQLLine), and use
// toString() since getMessage() text doesn't always mention error:)
throw new SQLException("Failure in connecting to Drill: " + e, e);
}
}
/**
* Throws AlreadyClosedSqlException <i>iff</i> this Connection is closed.
*
* @throws AlreadyClosedSqlException if Connection is closed
*/
private void throwIfClosed() throws AlreadyClosedSqlException {
if ( isClosed() ) {
throw new AlreadyClosedSqlException( "Connection is already closed." );
}
}
@Override
public DrillConnectionConfig getConfig() {
return config;
}
@Override
protected Meta createMeta() {
return new MetaImpl(this);
}
MetaImpl meta() {
return (MetaImpl) meta;
}
BufferAllocator getAllocator() {
return allocator;
}
@Override
public DrillClient getClient() {
return client;
}
@Override
public void setAutoCommit( boolean autoCommit ) throws SQLException {
throwIfClosed();
if ( ! autoCommit ) {
throw new SQLFeatureNotSupportedException(
"Can't turn off auto-committing; transactions are not supported. "
+ "(Drill is not transactional.)" );
}
assert getAutoCommit() : "getAutoCommit() = " + getAutoCommit();
}
@Override
public void commit() throws SQLException {
throwIfClosed();
if ( getAutoCommit() ) {
throw new JdbcApiSqlException( "Can't call commit() in auto-commit mode." );
}
else {
// (Currently not reachable.)
throw new SQLFeatureNotSupportedException(
"Connection.commit() is not supported. (Drill is not transactional.)" );
}
}
@Override
public void rollback() throws SQLException {
throwIfClosed();
if ( getAutoCommit() ) {
throw new JdbcApiSqlException( "Can't call rollback() in auto-commit mode." );
}
else {
// (Currently not reachable.)
throw new SQLFeatureNotSupportedException(
"Connection.rollback() is not supported. (Drill is not transactional.)" );
}
}
@Override
public boolean isClosed() {
try {
return super.isClosed();
}
catch ( SQLException e ) {
// Currently can't happen, since AvaticaConnection.isClosed() never throws
// SQLException.
throw new DrillRuntimeException(
"Unexpected exception from " + getClass().getSuperclass()
+ ".isClosed(): " + e,
e );
}
}
@Override
public Savepoint setSavepoint() throws SQLException {
throwIfClosed();
throw new SQLFeatureNotSupportedException(
"Savepoints are not supported. (Drill is not transactional.)" );
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
throwIfClosed();
throw new SQLFeatureNotSupportedException(
"Savepoints are not supported. (Drill is not transactional.)" );
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
throwIfClosed();
throw new SQLFeatureNotSupportedException(
"Savepoints are not supported. (Drill is not transactional.)" );
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
throwIfClosed();
throw new SQLFeatureNotSupportedException(
"Savepoints are not supported. (Drill is not transactional.)" );
}
private String isolationValueToString( final int level ) {
switch ( level ) {
case TRANSACTION_NONE: return "TRANSACTION_NONE";
case TRANSACTION_READ_UNCOMMITTED: return "TRANSACTION_READ_UNCOMMITTED";
case TRANSACTION_READ_COMMITTED: return "TRANSACTION_READ_COMMITTED";
case TRANSACTION_REPEATABLE_READ: return "TRANSACTION_REPEATABLE_READ";
case TRANSACTION_SERIALIZABLE: return "TRANSACTION_SERIALIZABLE";
default:
return "<Unknown transaction isolation level value " + level + ">";
}
}
@Override
public void setTransactionIsolation(int level) throws SQLException {
throwIfClosed();
switch ( level ) {
case TRANSACTION_NONE:
// No-op. (Is already set in constructor, and we disallow changing it.)
break;
case TRANSACTION_READ_UNCOMMITTED:
case TRANSACTION_READ_COMMITTED:
case TRANSACTION_REPEATABLE_READ:
case TRANSACTION_SERIALIZABLE:
throw new SQLFeatureNotSupportedException(
"Can't change transaction isolation level to Connection."
+ isolationValueToString( level ) + " (from Connection."
+ isolationValueToString( getTransactionIsolation() ) + ")."
+ " (Drill is not transactional.)" );
default:
// Invalid value (or new one unknown to code).
throw new JdbcApiSqlException(
"Invalid transaction isolation level value " + level );
//break;
}
}
@Override
public void setNetworkTimeout( Executor executor, int milliseconds )
throws AlreadyClosedSqlException,
JdbcApiSqlException,
SQLFeatureNotSupportedException {
throwIfClosed();
if ( null == executor ) {
throw new InvalidParameterSqlException(
"Invalid (null) \"executor\" parameter to setNetworkTimeout(...)" );
}
else if ( milliseconds < 0 ) {
throw new InvalidParameterSqlException(
"Invalid (negative) \"milliseconds\" parameter to"
+ " setNetworkTimeout(...) (" + milliseconds + ")" );
}
else {
if ( 0 != milliseconds ) {
throw new SQLFeatureNotSupportedException(
"Setting network timeout is not supported." );
}
}
}
@Override
public int getNetworkTimeout() throws AlreadyClosedSqlException
{
throwIfClosed();
return 0; // (No timeout.)
}
@Override
public DrillStatementImpl createStatement(int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
throwIfClosed();
DrillStatementImpl statement =
(DrillStatementImpl) super.createStatement(resultSetType,
resultSetConcurrency,
resultSetHoldability);
return statement;
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
throwIfClosed();
try {
DrillPrepareResult prepareResult = new DrillPrepareResult(sql);
DrillPreparedStatementImpl statement =
(DrillPreparedStatementImpl) factory.newPreparedStatement(
this, prepareResult, resultSetType, resultSetConcurrency,
resultSetHoldability);
return statement;
} catch (RuntimeException e) {
throw Helper.INSTANCE.createException("Error while preparing statement [" + sql + "]", e);
} catch (Exception e) {
throw Helper.INSTANCE.createException("Error while preparing statement [" + sql + "]", e);
}
}
@Override
public TimeZone getTimeZone() {
return config.getTimeZone();
}
// Note: Using dynamic proxies would reduce the quantity (450?) of method
// overrides by eliminating those that exist solely to check whether the
// object is closed. It would also eliminate the need to throw non-compliant
// RuntimeExceptions when Avatica's method declarations won't let us throw
// proper SQLExceptions. (Check performance before applying to frequently
// called ResultSet.)
// No isWrapperFor(Class<?>) (it doesn't throw SQLException if already closed).
// No unwrap(Class<T>) (it doesn't throw SQLException if already closed).
@Override
public AvaticaStatement createStatement() throws SQLException {
throwIfClosed();
return super.createStatement();
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
throwIfClosed();
return super.prepareStatement(sql);
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
throwIfClosed();
try {
return super.prepareCall(sql);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public String nativeSQL(String sql) throws SQLException {
throwIfClosed();
try {
return super.nativeSQL(sql);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public boolean getAutoCommit() throws SQLException {
throwIfClosed();
return super.getAutoCommit();
}
// No close() (it doesn't throw SQLException if already closed).
@Override
public DatabaseMetaData getMetaData() throws SQLException {
throwIfClosed();
return super.getMetaData();
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
throwIfClosed();
super.setReadOnly(readOnly);
}
@Override
public boolean isReadOnly() throws SQLException {
throwIfClosed();
return super.isReadOnly();
}
@Override
public void setCatalog(String catalog) throws SQLException {
throwIfClosed();
super.setCatalog(catalog);
}
@Override
public String getCatalog() {
// Can't throw any SQLException because AvaticaConnection's getCatalog() is
// missing "throws SQLException".
try {
throwIfClosed();
} catch (AlreadyClosedSqlException e) {
throw new RuntimeException(e.getMessage(), e);
}
return super.getCatalog();
}
@Override
public int getTransactionIsolation() throws SQLException {
throwIfClosed();
return super.getTransactionIsolation();
}
@Override
public SQLWarning getWarnings() throws SQLException {
throwIfClosed();
return super.getWarnings();
}
@Override
public void clearWarnings() throws SQLException {
throwIfClosed();
super.clearWarnings();
}
@Override
public Statement createStatement(int resultSetType,
int resultSetConcurrency) throws SQLException {
throwIfClosed();
return super.createStatement(resultSetType, resultSetConcurrency);
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
throwIfClosed();
return super.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
throwIfClosed();
try {
return super.prepareCall(sql, resultSetType, resultSetConcurrency);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public Map<String,Class<?>> getTypeMap() throws SQLException {
throwIfClosed();
try {
return super.getTypeMap();
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public void setTypeMap(Map<String,Class<?>> map) throws SQLException {
throwIfClosed();
try {
super.setTypeMap(map);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public void setHoldability(int holdability) throws SQLException {
throwIfClosed();
super.setHoldability(holdability);
}
@Override
public int getHoldability() throws SQLException {
throwIfClosed();
return super.getHoldability();
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
throwIfClosed();
try {
return super.prepareCall(sql, resultSetType, resultSetConcurrency,
resultSetHoldability);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public PreparedStatement prepareStatement(String sql,
int autoGeneratedKeys) throws SQLException {
throwIfClosed();
try {
return super.prepareStatement(sql, autoGeneratedKeys);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public PreparedStatement prepareStatement(String sql,
int columnIndexes[]) throws SQLException {
throwIfClosed();
try {
return super.prepareStatement(sql, columnIndexes);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public PreparedStatement prepareStatement(String sql,
String columnNames[]) throws SQLException {
throwIfClosed();
try {
return super.prepareStatement(sql, columnNames);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public Clob createClob() throws SQLException {
throwIfClosed();
try {
return super.createClob();
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public Blob createBlob() throws SQLException {
throwIfClosed();
try {
return super.createBlob();
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public NClob createNClob() throws SQLException {
throwIfClosed();
try {
return super.createNClob();
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public SQLXML createSQLXML() throws SQLException {
throwIfClosed();
try {
return super.createSQLXML();
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public boolean isValid(int timeout) throws SQLException {
throwIfClosed();
try {
return super.isValid(timeout);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
try {
throwIfClosed();
} catch (AlreadyClosedSqlException e) {
throw new SQLClientInfoException(e.getMessage(), null, e);
}
try {
super.setClientInfo(name, value);
}
catch (UnsupportedOperationException e) {
SQLFeatureNotSupportedException intended =
new SQLFeatureNotSupportedException(e.getMessage(), e);
throw new SQLClientInfoException(e.getMessage(), null, intended);
}
}
@Override
public void setClientInfo(Properties properties) throws SQLClientInfoException {
try {
throwIfClosed();
} catch (AlreadyClosedSqlException e) {
throw new SQLClientInfoException(e.getMessage(), null, e);
}
try {
super.setClientInfo(properties);
}
catch (UnsupportedOperationException e) {
SQLFeatureNotSupportedException intended =
new SQLFeatureNotSupportedException(e.getMessage(), e);
throw new SQLClientInfoException(e.getMessage(), null, intended);
}
}
@Override
public String getClientInfo(String name) throws SQLException {
throwIfClosed();
try {
return super.getClientInfo(name);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public Properties getClientInfo() throws SQLException {
throwIfClosed();
try {
return super.getClientInfo();
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
throwIfClosed();
try {
return super.createArrayOf(typeName, elements);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
throwIfClosed();
try {
return super.createStruct(typeName, attributes);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
@Override
public void setSchema(String schema) throws SQLException {
throwIfClosed();
super.setSchema(schema);
}
@Override
public String getSchema() {
// Can't throw any SQLException because AvaticaConnection's getCatalog() is
// missing "throws SQLException".
try {
throwIfClosed();
} catch (AlreadyClosedSqlException e) {
throw new RuntimeException(e.getMessage(), e);
}
return super.getSchema();
}
@Override
public void abort(Executor executor) throws SQLException {
throwIfClosed();
try {
super.abort(executor);
}
catch (UnsupportedOperationException e) {
throw new SQLFeatureNotSupportedException(e.getMessage(), e);
}
}
// do not make public
UnregisteredDriver getDriver() {
return driver;
}
// do not make public
AvaticaFactory getFactory() {
return factory;
}
private static void closeOrWarn(final AutoCloseable autoCloseable, final String message, final Logger logger) {
if (autoCloseable == null) {
return;
}
try {
autoCloseable.close();
} catch(Exception e) {
logger.warn(message, e);
}
}
// TODO this should be an AutoCloseable, and this should be close()
void cleanup() {
// First close any open JDBC Statement objects, to close any open ResultSet
// objects and release their buffers/vectors.
openStatementsRegistry.close();
// TODO all of these should use DeferredException when it is available from DRILL-2245
closeOrWarn(client, "Exception while closing client.", logger);
closeOrWarn(allocator, "Exception while closing allocator.", logger);
if (bit != null) {
bit.close();
}
closeOrWarn(serviceSet, "Exception while closing service set.", logger);
}
// TODO(DRILL-xxxx): Eliminate this test-specific hack from production code.
// If we're not going to have tests themselves explicitly handle making names
// unique, then at least move this logic into a test base class, and have it
// go through DrillConnection.getClient().
/**
* Test only code to make JDBC tests run concurrently. If the property <i>drillJDBCUnitTests</i> is set to
* <i>true</i> in connection properties:
* - Update dfs_test.tmp workspace location with a temp directory. This temp is for exclusive use for test jvm.
* - Update dfs.tmp workspace to immutable, so that test writer don't try to create views in dfs.tmp
* @param pluginRegistry
*/
private static void makeTmpSchemaLocationsUnique(StoragePluginRegistry pluginRegistry, Properties props) {
try {
if (props != null && "true".equalsIgnoreCase(props.getProperty("drillJDBCUnitTests"))) {
final String tmpDirPath = TestUtilities.createTempDir();
TestUtilities.updateDfsTestTmpSchemaLocation(pluginRegistry, tmpDirPath);
TestUtilities.makeDfsTmpSchemaImmutable(pluginRegistry);
}
} catch(Throwable e) {
// Reason for catching Throwable is to capture NoSuchMethodError etc which depend on certain classed to be
// present in classpath which may not be available when just using the standalone JDBC. This is unlikely to
// happen, but just a safeguard to avoid failing user applications.
logger.warn("Failed to update tmp schema locations. This step is purely for testing purpose. " +
"Shouldn't be seen in production code.");
// Ignore the error and go with defaults
}
}
}