-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
JdbcConnection.java
1059 lines (838 loc) · 34.4 KB
/
JdbcConnection.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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.ignite.internal.jdbc2;
import java.net.URL;
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.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteClientDisconnectedException;
import org.apache.ignite.IgniteDataStreamer;
import org.apache.ignite.IgniteException;
import org.apache.ignite.IgniteJdbcDriver;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.cluster.ClusterGroup;
import org.apache.ignite.compute.ComputeTaskTimeoutException;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.IgniteEx;
import org.apache.ignite.internal.IgniteKernal;
import org.apache.ignite.internal.IgnitionEx;
import org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor;
import org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode;
import org.apache.ignite.internal.processors.odbc.SqlStateCode;
import org.apache.ignite.internal.processors.query.GridQueryIndexing;
import org.apache.ignite.internal.processors.query.IgniteSQLException;
import org.apache.ignite.internal.processors.query.QueryUtils;
import org.apache.ignite.internal.processors.resource.GridSpringResourceContext;
import org.apache.ignite.internal.util.future.GridFutureAdapter;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.lang.IgniteBiTuple;
import org.apache.ignite.lang.IgniteCallable;
import org.apache.ignite.lang.IgniteProductVersion;
import org.apache.ignite.resources.IgniteInstanceResource;
import static java.sql.ResultSet.CONCUR_READ_ONLY;
import static java.sql.ResultSet.HOLD_CURSORS_OVER_COMMIT;
import static java.sql.ResultSet.TYPE_FORWARD_ONLY;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.ignite.IgniteJdbcDriver.PROP_CACHE;
import static org.apache.ignite.IgniteJdbcDriver.PROP_CFG;
import static org.apache.ignite.IgniteJdbcDriver.PROP_COLLOCATED;
import static org.apache.ignite.IgniteJdbcDriver.PROP_DISTRIBUTED_JOINS;
import static org.apache.ignite.IgniteJdbcDriver.PROP_ENFORCE_JOIN_ORDER;
import static org.apache.ignite.IgniteJdbcDriver.PROP_LAZY;
import static org.apache.ignite.IgniteJdbcDriver.PROP_LOCAL;
import static org.apache.ignite.IgniteJdbcDriver.PROP_MULTIPLE_STMTS;
import static org.apache.ignite.IgniteJdbcDriver.PROP_NODE_ID;
import static org.apache.ignite.IgniteJdbcDriver.PROP_SCHEMA;
import static org.apache.ignite.IgniteJdbcDriver.PROP_SKIP_REDUCER_ON_UPDATE;
import static org.apache.ignite.IgniteJdbcDriver.PROP_STREAMING;
import static org.apache.ignite.IgniteJdbcDriver.PROP_STREAMING_ALLOW_OVERWRITE;
import static org.apache.ignite.IgniteJdbcDriver.PROP_STREAMING_FLUSH_FREQ;
import static org.apache.ignite.IgniteJdbcDriver.PROP_STREAMING_PER_NODE_BUF_SIZE;
import static org.apache.ignite.IgniteJdbcDriver.PROP_STREAMING_PER_NODE_PAR_OPS;
import static org.apache.ignite.IgniteJdbcDriver.PROP_TX_ALLOWED;
import static org.apache.ignite.cache.query.SqlFieldsQuery.DFLT_LAZY;
import static org.apache.ignite.internal.GridClosureCallMode.BALANCE;
import static org.apache.ignite.internal.jdbc2.JdbcUtils.convertToSqlException;
import static org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.createJdbcSqlException;
import static org.apache.ignite.internal.processors.task.TaskExecutionOptions.options;
/**
* JDBC connection implementation.
*/
public class JdbcConnection implements Connection {
/** Null stub. */
private static final String NULL = "null";
/** Multiple statements supported since version. */
private static final IgniteProductVersion MULTIPLE_STATEMENTS_SUPPORTED_SINCE =
IgniteProductVersion.fromString("2.4.0");
/** Multiple statements V2 task supported since version. */
private static final IgniteProductVersion MULTIPLE_STATEMENTS_TASK_V2_SUPPORTED_SINCE =
IgniteProductVersion.fromString("2.8.0");
/** Close remote cursor task is supported since version. {@link JdbcCloseCursorTask}*/
private static final IgniteProductVersion CLOSE_CURSOR_TASK_SUPPORTED_SINCE =
IgniteProductVersion.fromString("2.11.0");
/** Multiple statements V3 task supported since version. */
private static final IgniteProductVersion MULTIPLE_STATEMENTS_TASK_V3_SUPPORTED_SINCE =
IgniteProductVersion.fromString("2.11.0");
/**
* Ignite nodes cache.
*
* The key is result of concatenation of the following properties:
* <ol>
* <li>{@link IgniteJdbcDriver#PROP_CFG}</li>
* </ol>
*/
private static final ConcurrentMap<String, IgniteNodeFuture> NODES = new ConcurrentHashMap<>();
/** Ignite ignite. */
private final Ignite ignite;
/** Node key. */
private final String cfg;
/** Cache name. */
private final String cacheName;
/** Schema name. */
private String schemaName;
/** Closed flag. */
private boolean closed;
/** URL. */
private String url;
/** Node ID. */
private UUID nodeId;
/** Local query flag. */
private boolean locQry;
/** Collocated query flag. */
private boolean collocatedQry;
/** Distributed joins flag. */
private boolean distributedJoins;
/** Enforced join order flag. */
private boolean enforceJoinOrder;
/** Lazy query execution flag. */
private boolean lazy;
/** Transactions allowed flag. */
private boolean txAllowed;
/** Current transaction isolation. */
private int txIsolation;
/** Make this connection streaming oriented, and prepared statements - data streamer aware. */
private final boolean stream;
/** Auto flush frequency for streaming. */
private final long streamFlushTimeout;
/** Node buffer size for data streamer. */
private final int streamNodeBufSize;
/** Parallel ops count per node for data streamer. */
private final int streamNodeParOps;
/** Allow overwrites for duplicate keys on streamed {@code INSERT}s. */
private final boolean streamAllowOverwrite;
/** Allow queries with multiple statements. */
private final boolean multipleStmts;
/** Skip reducer on update flag. */
private final boolean skipReducerOnUpdate;
/** Statements. */
final Set<JdbcStatement> statements = new HashSet<>();
/**
* Describes the client connection:
* - thin cli: "cli:host:port@user_name"
* - thin JDBC: "jdbc-thin:host:port@user_name"
* - ODBC: "odbc:host:port@user_name"
*
* Used by the running query view to display query initiator.
*/
private final String clientDesc;
/**
* Creates new connection.
*
* @param url Connection URL.
* @param props Additional properties.
* @throws SQLException In case Ignite node failed to start.
*/
public JdbcConnection(String url, Properties props) throws SQLException {
assert url != null;
assert props != null;
this.url = url;
cacheName = props.getProperty(PROP_CACHE);
locQry = Boolean.parseBoolean(props.getProperty(PROP_LOCAL));
collocatedQry = Boolean.parseBoolean(props.getProperty(PROP_COLLOCATED));
distributedJoins = Boolean.parseBoolean(props.getProperty(PROP_DISTRIBUTED_JOINS));
enforceJoinOrder = Boolean.parseBoolean(props.getProperty(PROP_ENFORCE_JOIN_ORDER));
lazy = Boolean.parseBoolean(props.getProperty(PROP_LAZY, String.valueOf(DFLT_LAZY)));
txAllowed = Boolean.parseBoolean(props.getProperty(PROP_TX_ALLOWED));
stream = Boolean.parseBoolean(props.getProperty(PROP_STREAMING));
if (stream && cacheName == null) {
throw new SQLException("Cache name cannot be null when streaming is enabled.",
SqlStateCode.CLIENT_CONNECTION_FAILED);
}
streamAllowOverwrite = Boolean.parseBoolean(props.getProperty(PROP_STREAMING_ALLOW_OVERWRITE));
streamFlushTimeout = Long.parseLong(props.getProperty(PROP_STREAMING_FLUSH_FREQ, "0"));
streamNodeBufSize = Integer.parseInt(props.getProperty(PROP_STREAMING_PER_NODE_BUF_SIZE,
String.valueOf(IgniteDataStreamer.DFLT_PER_NODE_BUFFER_SIZE)));
// If value is zero, server data-streamer pool size multiplied
// by IgniteDataStreamer.DFLT_PARALLEL_OPS_MULTIPLIER will be used
streamNodeParOps = Integer.parseInt(props.getProperty(PROP_STREAMING_PER_NODE_PAR_OPS, "0"));
multipleStmts = Boolean.parseBoolean(props.getProperty(PROP_MULTIPLE_STMTS));
skipReducerOnUpdate = Boolean.parseBoolean(props.getProperty(PROP_SKIP_REDUCER_ON_UPDATE));
schemaName = QueryUtils.normalizeSchemaName(null, props.getProperty(PROP_SCHEMA));
String nodeIdProp = props.getProperty(PROP_NODE_ID);
if (nodeIdProp != null)
nodeId = UUID.fromString(nodeIdProp);
try {
String cfgUrl = props.getProperty(PROP_CFG);
cfg = cfgUrl == null || cfgUrl.isEmpty() ? NULL : cfgUrl;
ignite = getIgnite(cfg);
if (!isValid(2)) {
throw new SQLException("Client is invalid. Probably cache name is wrong.",
SqlStateCode.CLIENT_CONNECTION_FAILED);
}
if (cacheName != null) {
DynamicCacheDescriptor cacheDesc = ignite().context().cache().cacheDescriptor(cacheName);
if (cacheDesc == null) {
throw createJdbcSqlException("Cache doesn't exist: " + cacheName,
IgniteQueryErrorCode.CACHE_NOT_FOUND);
}
if (schemaName == null)
schemaName = QueryUtils.normalizeSchemaName(cacheName, cacheDesc.cacheConfiguration().getSqlSchema());
}
else {
if (schemaName == null)
schemaName = QueryUtils.DFLT_SCHEMA;
}
clientDesc = "jdbc-v2:" + F.first(ignite.cluster().localNode().addresses()) + ":" + ignite.name();
}
catch (Exception e) {
close();
throw convertToSqlException(e, "Failed to start Ignite node. " + e.getMessage(), SqlStateCode.CLIENT_CONNECTION_FAILED);
}
}
/**
* @param cfgUrl Config url.
* @return Ignite client node.
* @throws IgniteCheckedException On error.
*/
private Ignite getIgnite(String cfgUrl) throws IgniteCheckedException {
while (true) {
IgniteNodeFuture fut = NODES.get(cfg);
if (fut == null) {
fut = new IgniteNodeFuture();
IgniteNodeFuture old = NODES.putIfAbsent(cfg, fut);
if (old != null)
fut = old;
else {
try {
final IgniteBiTuple<IgniteConfiguration, ? extends GridSpringResourceContext> cfgAndCtx;
String jdbcName = "ignite-jdbc-driver-" + UUID.randomUUID().toString();
if (NULL.equals(cfg)) {
URL url = U.resolveIgniteUrl(IgnitionEx.DFLT_CFG);
if (url != null)
cfgAndCtx = loadConfiguration(IgnitionEx.DFLT_CFG, jdbcName);
else {
U.warn(null, "Default Spring XML file not found (is IGNITE_HOME set?): "
+ IgnitionEx.DFLT_CFG);
IgniteConfiguration cfg = new IgniteConfiguration()
.setIgniteInstanceName(jdbcName)
.setClientMode(true);
cfgAndCtx = new IgniteBiTuple<>(cfg, null);
}
}
else
cfgAndCtx = loadConfiguration(cfgUrl, jdbcName);
fut.onDone(IgnitionEx.start(cfgAndCtx.get1(), cfgAndCtx.get2()));
}
catch (IgniteException e) {
fut.onDone(e);
}
return fut.get();
}
}
if (fut.acquire())
return fut.get();
else
NODES.remove(cfg, fut);
}
}
/**
* @param cfgUrl Config URL.
* @param jdbcName Appended to instance name or used as default.
* @return Ignite config and Spring context.
*/
private IgniteBiTuple<IgniteConfiguration, ? extends GridSpringResourceContext> loadConfiguration(String cfgUrl,
String jdbcName) {
try {
IgniteBiTuple<Collection<IgniteConfiguration>, ? extends GridSpringResourceContext> cfgMap =
IgnitionEx.loadConfigurations(cfgUrl);
IgniteConfiguration cfg = F.first(cfgMap.get1());
if (cfg.getIgniteInstanceName() == null)
cfg.setIgniteInstanceName(jdbcName);
else
cfg.setIgniteInstanceName(cfg.getIgniteInstanceName() + "-" + jdbcName);
cfg.setClientMode(true); // Force client mode.
return new IgniteBiTuple<>(cfg, cfgMap.getValue());
}
catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
}
/** {@inheritDoc} */
@Override public Statement createStatement() throws SQLException {
return createStatement(TYPE_FORWARD_ONLY, CONCUR_READ_ONLY, HOLD_CURSORS_OVER_COMMIT);
}
/** {@inheritDoc} */
@Override public PreparedStatement prepareStatement(String sql) throws SQLException {
ensureNotClosed();
return prepareStatement(sql, TYPE_FORWARD_ONLY, CONCUR_READ_ONLY, HOLD_CURSORS_OVER_COMMIT);
}
/** {@inheritDoc} */
@Override public CallableStatement prepareCall(String sql) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Callable functions are not supported.");
}
/** {@inheritDoc} */
@Override public String nativeSQL(String sql) throws SQLException {
ensureNotClosed();
return sql;
}
/** {@inheritDoc} */
@Override public void setAutoCommit(boolean autoCommit) throws SQLException {
ensureNotClosed();
if (!txAllowed && !autoCommit)
throw new SQLFeatureNotSupportedException("Transactions are not supported.");
}
/** {@inheritDoc} */
@Override public boolean getAutoCommit() throws SQLException {
ensureNotClosed();
return true;
}
/** {@inheritDoc} */
@Override public void commit() throws SQLException {
ensureNotClosed();
if (!txAllowed)
throw new SQLFeatureNotSupportedException("Transactions are not supported.");
}
/** {@inheritDoc} */
@Override public void rollback() throws SQLException {
ensureNotClosed();
if (!txAllowed)
throw new SQLFeatureNotSupportedException("Transactions are not supported.");
}
/** {@inheritDoc} */
@Override public void close() throws SQLException {
if (closed)
return;
closed = true;
for (Iterator<JdbcStatement> it = statements.iterator(); it.hasNext();) {
JdbcStatement stmt = it.next();
stmt.closeInternal();
it.remove();
}
IgniteNodeFuture fut = NODES.get(cfg);
if (fut != null && fut.release()) {
NODES.remove(cfg);
if (ignite != null)
ignite.close();
}
}
/** {@inheritDoc} */
@Override public boolean isClosed() throws SQLException {
return closed;
}
/** {@inheritDoc} */
@Override public DatabaseMetaData getMetaData() throws SQLException {
ensureNotClosed();
return new JdbcDatabaseMetadata(this);
}
/** {@inheritDoc} */
@Override public void setReadOnly(boolean readOnly) throws SQLException {
ensureNotClosed();
}
/** {@inheritDoc} */
@Override public boolean isReadOnly() throws SQLException {
ensureNotClosed();
return true;
}
/** {@inheritDoc} */
@Override public void setCatalog(String catalog) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Catalogs are not supported.");
}
/** {@inheritDoc} */
@Override public String getCatalog() throws SQLException {
ensureNotClosed();
return null;
}
/** {@inheritDoc} */
@Override public void setTransactionIsolation(int level) throws SQLException {
ensureNotClosed();
if (txAllowed)
txIsolation = level;
else
throw new SQLFeatureNotSupportedException("Transactions are not supported.");
}
/** {@inheritDoc} */
@Override public int getTransactionIsolation() throws SQLException {
ensureNotClosed();
if (txAllowed)
return txIsolation;
else
throw new SQLFeatureNotSupportedException("Transactions are not supported.");
}
/** {@inheritDoc} */
@Override public SQLWarning getWarnings() throws SQLException {
ensureNotClosed();
return null;
}
/** {@inheritDoc} */
@Override public void clearWarnings() throws SQLException {
ensureNotClosed();
}
/** {@inheritDoc} */
@Override public Statement createStatement(int resSetType, int resSetConcurrency) throws SQLException {
return createStatement(resSetType, resSetConcurrency, HOLD_CURSORS_OVER_COMMIT);
}
/** {@inheritDoc} */
@Override public PreparedStatement prepareStatement(String sql, int resSetType,
int resSetConcurrency) throws SQLException {
ensureNotClosed();
return prepareStatement(sql, resSetType, resSetConcurrency, HOLD_CURSORS_OVER_COMMIT);
}
/** {@inheritDoc} */
@Override public CallableStatement prepareCall(String sql, int resSetType,
int resSetConcurrency) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Callable functions are not supported.");
}
/** {@inheritDoc} */
@Override public Map<String, Class<?>> getTypeMap() throws SQLException {
throw new SQLFeatureNotSupportedException("Types mapping is not supported.");
}
/** {@inheritDoc} */
@Override public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Types mapping is not supported.");
}
/** {@inheritDoc} */
@Override public void setHoldability(int holdability) throws SQLException {
ensureNotClosed();
if (!txAllowed && holdability != HOLD_CURSORS_OVER_COMMIT)
throw new SQLFeatureNotSupportedException("Invalid holdability (transactions are not supported).");
}
/** {@inheritDoc} */
@Override public int getHoldability() throws SQLException {
ensureNotClosed();
return HOLD_CURSORS_OVER_COMMIT;
}
/** {@inheritDoc} */
@Override public Savepoint setSavepoint() throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Savepoints are not supported.");
}
/** {@inheritDoc} */
@Override public Savepoint setSavepoint(String name) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Savepoints are not supported.");
}
/** {@inheritDoc} */
@Override public void rollback(Savepoint savepoint) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Savepoints are not supported.");
}
/** {@inheritDoc} */
@Override public void releaseSavepoint(Savepoint savepoint) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Savepoints are not supported.");
}
/** {@inheritDoc} */
@Override public Statement createStatement(int resSetType, int resSetConcurrency,
int resSetHoldability) throws SQLException {
ensureNotClosed();
if (resSetType != TYPE_FORWARD_ONLY)
throw new SQLFeatureNotSupportedException("Invalid result set type (only forward is supported.)");
if (resSetConcurrency != CONCUR_READ_ONLY)
throw new SQLFeatureNotSupportedException("Invalid concurrency (updates are not supported).");
if (!txAllowed && resSetHoldability != HOLD_CURSORS_OVER_COMMIT)
throw new SQLFeatureNotSupportedException("Invalid holdability (transactions are not supported).");
JdbcStatement stmt = new JdbcStatement(this);
statements.add(stmt);
return stmt;
}
/** {@inheritDoc} */
@Override public PreparedStatement prepareStatement(String sql, int resSetType, int resSetConcurrency,
int resSetHoldability) throws SQLException {
ensureNotClosed();
if (resSetType != TYPE_FORWARD_ONLY)
throw new SQLFeatureNotSupportedException("Invalid result set type (only forward is supported.)");
if (resSetConcurrency != CONCUR_READ_ONLY)
throw new SQLFeatureNotSupportedException("Invalid concurrency (updates are not supported).");
if (!txAllowed && resSetHoldability != HOLD_CURSORS_OVER_COMMIT)
throw new SQLFeatureNotSupportedException("Invalid holdability (transactions are not supported).");
JdbcPreparedStatement stmt;
if (!stream)
stmt = new JdbcPreparedStatement(this, sql);
else {
GridQueryIndexing idx = ignite().context().query().getIndexing();
if (!idx.isStreamableInsertStatement(schemaName(), new SqlFieldsQuery(sql)))
throw new IgniteSQLException("Streaming mode supports only INSERT commands without subqueries.",
IgniteQueryErrorCode.UNSUPPORTED_OPERATION).toJdbcException();
IgniteDataStreamer streamer = ignite().dataStreamer(cacheName);
streamer.autoFlushFrequency(streamFlushTimeout);
streamer.allowOverwrite(streamAllowOverwrite);
if (streamNodeBufSize > 0)
streamer.perNodeBufferSize(streamNodeBufSize);
if (streamNodeParOps > 0)
streamer.perNodeParallelOperations(streamNodeParOps);
stmt = new JdbcStreamedPreparedStatement(this, sql, streamer);
}
statements.add(stmt);
return stmt;
}
/** {@inheritDoc} */
@Override public CallableStatement prepareCall(String sql, int resSetType, int resSetConcurrency,
int resSetHoldability) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Callable functions are not supported.");
}
/** {@inheritDoc} */
@Override public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Updates are not supported.");
}
/** {@inheritDoc} */
@Override public PreparedStatement prepareStatement(String sql, int[] colIndexes) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Updates are not supported.");
}
/** {@inheritDoc} */
@Override public PreparedStatement prepareStatement(String sql, String[] colNames) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("Updates are not supported.");
}
/** {@inheritDoc} */
@Override public Clob createClob() throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("SQL-specific types are not supported.");
}
/** {@inheritDoc} */
@Override public Blob createBlob() throws SQLException {
ensureNotClosed();
return new JdbcBlob(new byte[0]);
}
/** {@inheritDoc} */
@Override public NClob createNClob() throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("SQL-specific types are not supported.");
}
/** {@inheritDoc} */
@Override public SQLXML createSQLXML() throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("SQL-specific types are not supported.");
}
/** {@inheritDoc} */
@Override public boolean isValid(int timeout) throws SQLException {
ensureNotClosed();
if (timeout < 0)
throw new SQLException("Invalid timeout: " + timeout);
try {
JdbcConnectionValidationTask task = new JdbcConnectionValidationTask(cacheName,
nodeId == null ? ignite : null);
if (nodeId != null) {
ClusterGroup grp = ignite.cluster().forServers().forNodeId(nodeId);
if (grp.nodes().isEmpty())
throw new SQLException("Failed to establish connection with node (is it a server node?): " +
nodeId);
assert grp.nodes().size() == 1;
return ((IgniteEx)ignite).context().closure().callAsync(
BALANCE,
task,
options(grp.nodes())
).get(timeout, SECONDS);
}
else
return task.call();
}
catch (IgniteClientDisconnectedException | ComputeTaskTimeoutException e) {
throw new SQLException("Failed to establish connection.", SqlStateCode.CONNECTION_FAILURE, e);
}
catch (IgniteCheckedException | IgniteException ignored) {
return false;
}
}
/** {@inheritDoc} */
@Override public void setClientInfo(String name, String val) throws SQLClientInfoException {
throw new UnsupportedOperationException("Client info is not supported.");
}
/** {@inheritDoc} */
@Override public void setClientInfo(Properties props) throws SQLClientInfoException {
throw new UnsupportedOperationException("Client info is not supported.");
}
/** {@inheritDoc} */
@Override public String getClientInfo(String name) throws SQLException {
ensureNotClosed();
return null;
}
/** {@inheritDoc} */
@Override public Properties getClientInfo() throws SQLException {
ensureNotClosed();
return new Properties();
}
/** {@inheritDoc} */
@Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("SQL-specific types are not supported.");
}
/** {@inheritDoc} */
@Override public Struct createStruct(String typeName, Object[] attrs) throws SQLException {
ensureNotClosed();
throw new SQLFeatureNotSupportedException("SQL-specific types are not supported.");
}
/** {@inheritDoc} */
@Override public <T> T unwrap(Class<T> iface) throws SQLException {
if (!isWrapperFor(iface))
throw new SQLException("Connection is not a wrapper for " + iface.getName());
return (T)this;
}
/** {@inheritDoc} */
@Override public boolean isWrapperFor(Class<?> iface) throws SQLException {
return iface != null && iface == Connection.class;
}
/** {@inheritDoc} */
@Override public void setSchema(String schemaName) throws SQLException {
this.schemaName = JdbcUtils.normalizeSchema(schemaName);
}
/** {@inheritDoc} */
@Override public String getSchema() throws SQLException {
return schemaName;
}
/**
* @return Normalized schema name.
*/
public String schemaName() {
return F.isEmpty(schemaName) ? QueryUtils.DFLT_SCHEMA : schemaName;
}
/** {@inheritDoc} */
@Override public void abort(Executor executor) throws SQLException {
close();
}
/** {@inheritDoc} */
@Override public void setNetworkTimeout(Executor executor, int ms) throws SQLException {
throw new SQLFeatureNotSupportedException("Network timeout is not supported.");
}
/** {@inheritDoc} */
@Override public int getNetworkTimeout() throws SQLException {
throw new SQLFeatureNotSupportedException("Network timeout is not supported.");
}
/**
* @return Ignite node.
*/
IgniteKernal ignite() {
return (IgniteKernal)ignite;
}
/**
* @return Cache name.
*/
String cacheName() {
return cacheName;
}
/**
* @return URL.
*/
String url() {
return url;
}
/**
* @return Node ID.
*/
UUID nodeId() {
return nodeId;
}
/**
* @return {@code true} if target node has DML support, {@code false} otherwise.
*/
boolean isDmlSupported() {
return ignite.version().greaterThanEqual(1, 8, 0);
}
/**
* @return {@code true} if multiple statements allowed, {@code false} otherwise.
*/
boolean isMultipleStatementsAllowed() {
return multipleStmts;
}
/**
* @return {@code true} if multiple statements allowed, {@code false} otherwise.
*/
boolean isMultipleStatementsSupported() {
return U.isOldestNodeVersionAtLeast(MULTIPLE_STATEMENTS_SUPPORTED_SINCE, ignite.cluster().nodes());
}
/**
* @return {@code true} if multiple statements allowed, {@code false} otherwise.
*/
boolean isMultipleStatementsTaskV2Supported() {
return U.isOldestNodeVersionAtLeast(MULTIPLE_STATEMENTS_TASK_V2_SUPPORTED_SINCE, ignite.cluster().nodes());
}
/**
* @return {@code true} if close remote cursor is supported.
*/
boolean isCloseCursorTaskSupported() {
return U.isOldestNodeVersionAtLeast(CLOSE_CURSOR_TASK_SUPPORTED_SINCE, ignite.cluster().nodes());
}
/**
* @return {@code true} if multiple statements allowed, {@code false} otherwise.
*/
boolean isMultipleStatementsTaskV3Supported() {
return U.isOldestNodeVersionAtLeast(MULTIPLE_STATEMENTS_TASK_V3_SUPPORTED_SINCE, ignite.cluster().nodes());
}
/**
* @return {@code true} if update on server is enabled, {@code false} otherwise.
*/
boolean skipReducerOnUpdate() {
return skipReducerOnUpdate;
}
/**
* @return Local query flag.
*/
boolean isLocalQuery() {
return locQry;
}
/**
* @return Collocated query flag.
*/
boolean isCollocatedQuery() {
return collocatedQry;
}
/**
* @return Distributed joins flag.
*/
boolean isDistributedJoins() {
return distributedJoins;
}
/**
* @return Enforce join order flag.
*/
boolean isEnforceJoinOrder() {
return enforceJoinOrder;
}
/**
* @return Lazy query execution flag.
*/
boolean isLazy() {
return lazy;
}
/**
* Ensures that connection is not closed.
*
* @throws SQLException If connection is closed.
*/
void ensureNotClosed() throws SQLException {
if (closed)
throw new SQLException("Connection is closed.", SqlStateCode.CONNECTION_CLOSED);
}
/**
* @return Internal statement.
* @throws SQLException In case of error.
*/
JdbcStatement createStatement0() throws SQLException {
return (JdbcStatement)createStatement();
}
/**
* Describes the client connection:
* - thin cli: "cli:host:port@user_name"
* - thin JDBC: "jdbc-thin:host:port@user_name"
* - ODBC: "odbc:host:port@user_name"
*
* Used by the running query view to display query initiator.
*
* @return Client descriptor string.
*/
String clientDescriptor() {
return clientDesc;
}
/**
* JDBC connection validation task.
*/
private static class JdbcConnectionValidationTask implements IgniteCallable<Boolean> {
/** Serial version uid. */
private static final long serialVersionUID = 0L;
/** Cache name. */