/
JdbcConnection.java
1683 lines (1585 loc) · 54.7 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
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.lealone.client.jdbc;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
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.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
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.concurrent.Executor;
import org.lealone.api.ErrorCode;
import org.lealone.client.ClientSession;
import org.lealone.common.exceptions.DbException;
import org.lealone.common.trace.TraceObject;
import org.lealone.common.util.CloseWatcher;
import org.lealone.common.util.Utils;
import org.lealone.db.Command;
import org.lealone.db.ConnectionInfo;
import org.lealone.db.Constants;
import org.lealone.db.Session;
import org.lealone.db.SysProperties;
import org.lealone.db.result.Result;
import org.lealone.db.value.CompareMode;
import org.lealone.db.value.Value;
import org.lealone.db.value.ValueInt;
import org.lealone.db.value.ValueNull;
import org.lealone.db.value.ValueString;
/**
* <p>
* Represents a connection (session) to a database.
* </p>
* <p>
* Thread safety: the connection is thread-safe, because access
* is synchronized. However, for compatibility with other databases, a
* connection should only be used in one thread at any time.
* </p>
*
* @author H2 Group
* @author zhh
*/
public class JdbcConnection extends TraceObject implements Connection {
private static boolean keepOpenStackTrace;
private final String url;
private final String user;
private int holdability = ResultSet.HOLD_CURSORS_OVER_COMMIT;
private Session session;
private Command commit, rollback;
private Command getReadOnly, getGeneratedKeys;
private Command setLockMode, getLockMode;
private Command setQueryTimeout, getQueryTimeout;
private int savepointId;
private String catalog;
private Statement executingStatement;
private final CompareMode compareMode = CompareMode.getInstance(null, 0, false);
private final CloseWatcher watcher;
private int queryTimeoutCache = -1;
/**
* INTERNAL
*/
public JdbcConnection(String url, Properties info) throws SQLException {
try {
ConnectionInfo ci = new ConnectionInfo(url, info);
if (!ci.isRemote()) {
String baseDir = SysProperties.getBaseDirSilently();
if (baseDir != null) {
ci.setBaseDir(baseDir);
}
}
// this will return an embedded or server connection
session = new ClientSession(ci).connectEmbeddedOrServer();
trace = session.getTrace();
int id = getNextTraceId(TraceObject.CONNECTION);
setTrace(trace, TraceObject.CONNECTION, id);
user = ci.getUserName();
url = ci.getURL(); // 不含参数
this.url = url;
if (isInfoEnabled()) {
String code = String.format("Connection %s = DriverManager.getConnection(%s, %s, \"\");",
getTraceObjectName(), quote(url), quote(user));
trace.infoCode(code);
}
closeOld();
watcher = CloseWatcher.register(this, session, keepOpenStackTrace);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* INTERNAL
*/
public JdbcConnection(Session session, String user, String url) {
this.session = session;
trace = session.getTrace();
int id = getNextTraceId(TraceObject.CONNECTION);
setTrace(trace, TraceObject.CONNECTION, id);
this.user = user;
this.url = url;
this.watcher = null;
}
private void closeOld() {
while (true) {
CloseWatcher w = CloseWatcher.pollUnclosed();
if (w == null) {
break;
}
try {
w.getCloseable().close();
} catch (Exception e) {
trace.error(e, "closing session");
}
// there was an unclosed object -
// keep the stack trace from now on
keepOpenStackTrace = true;
String s = w.getOpenStackTrace();
Exception ex = DbException.get(ErrorCode.TRACE_CONNECTION_NOT_CLOSED);
trace.error(ex, s);
}
}
/**
* Creates a new statement.
*
* @return the new statement
* @throws SQLException if the connection is closed
*/
@Override
public Statement createStatement() throws SQLException {
try {
int id = getNextTraceId(TraceObject.STATEMENT);
if (isDebugEnabled()) {
debugCodeAssign("Statement", TraceObject.STATEMENT, id, "createStatement()");
}
checkClosed();
return new JdbcStatement(this, id, ResultSet.TYPE_FORWARD_ONLY, Constants.DEFAULT_RESULT_SET_CONCURRENCY,
false);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Creates a statement with the specified result set type and concurrency.
*
* @param resultSetType the result set type (ResultSet.TYPE_*)
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*)
* @return the statement
* @throws SQLException
* if the connection is closed or the result set type or
* concurrency are not supported
*/
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
try {
int id = getNextTraceId(TraceObject.STATEMENT);
if (isDebugEnabled()) {
debugCodeAssign("Statement", TraceObject.STATEMENT, id, "createStatement(" + resultSetType + ", "
+ resultSetConcurrency + ")");
}
checkTypeConcurrency(resultSetType, resultSetConcurrency);
checkClosed();
return new JdbcStatement(this, id, resultSetType, resultSetConcurrency, false);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Creates a statement with the specified result set type, concurrency, and
* holdability.
*
* @param resultSetType the result set type (ResultSet.TYPE_*)
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*)
* @param resultSetHoldability the holdability (ResultSet.HOLD* / CLOSE*)
* @return the statement
* @throws SQLException if the connection is closed or the result set type,
* concurrency, or holdability are not supported
*/
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
try {
int id = getNextTraceId(TraceObject.STATEMENT);
if (isDebugEnabled()) {
debugCodeAssign("Statement", TraceObject.STATEMENT, id, "createStatement(" + resultSetType + ", "
+ resultSetConcurrency + ", " + resultSetHoldability + ")");
}
checkTypeConcurrency(resultSetType, resultSetConcurrency);
checkHoldability(resultSetHoldability);
checkClosed();
return new JdbcStatement(this, id, resultSetType, resultSetConcurrency, false);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Creates a new prepared statement.
*
* @param sql the SQL statement
* @return the prepared statement
* @throws SQLException if the connection is closed
*/
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
try {
int id = getNextTraceId(TraceObject.PREPARED_STATEMENT);
if (isDebugEnabled()) {
debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, "prepareStatement("
+ quote(sql) + ")");
}
checkClosed();
sql = translateSQL(sql);
return new JdbcPreparedStatement(this, sql, id, ResultSet.TYPE_FORWARD_ONLY,
Constants.DEFAULT_RESULT_SET_CONCURRENCY, false);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Prepare a statement that will automatically close when the result set is
* closed. This method is used to retrieve database meta data.
*
* @param sql the SQL statement
* @return the prepared statement
*/
PreparedStatement prepareAutoCloseStatement(String sql) throws SQLException {
try {
int id = getNextTraceId(TraceObject.PREPARED_STATEMENT);
if (isDebugEnabled()) {
debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, "prepareStatement("
+ quote(sql) + ")");
}
checkClosed();
sql = translateSQL(sql);
return new JdbcPreparedStatement(this, sql, id, ResultSet.TYPE_FORWARD_ONLY,
Constants.DEFAULT_RESULT_SET_CONCURRENCY, true);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Gets the database meta data for this database.
*
* @return the database meta data
* @throws SQLException if the connection is closed
*/
@Override
public DatabaseMetaData getMetaData() throws SQLException {
try {
int id = getNextTraceId(TraceObject.DATABASE_META_DATA);
if (isDebugEnabled()) {
debugCodeAssign("DatabaseMetaData", TraceObject.DATABASE_META_DATA, id, "getMetaData()");
}
checkClosed();
return new JdbcDatabaseMetaData(this, trace, id);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* INTERNAL
*/
public Session getSession() {
return session;
}
/**
* Closes this connection. All open statements, prepared statements and
* result sets that where created by this connection become invalid after
* calling this method. If there is an uncommitted transaction, it will be
* rolled back.
*/
@Override
public synchronized void close() throws SQLException {
try {
debugCodeCall("close");
if (session == null) {
return;
}
CloseWatcher.unregister(watcher);
session.cancel();
if (executingStatement != null) {
try {
executingStatement.cancel();
} catch (NullPointerException e) {
// ignore
}
}
synchronized (session) {
try {
if (!session.isClosed()) {
try {
closePreparedCommands();
} finally {
session.close();
}
}
} finally {
session = null;
}
}
} catch (Exception e) {
throw logAndConvert(e);
}
}
private void closePreparedCommands() {
commit = closeAndSetNull(commit);
rollback = closeAndSetNull(rollback);
getReadOnly = closeAndSetNull(getReadOnly);
getGeneratedKeys = closeAndSetNull(getGeneratedKeys);
getLockMode = closeAndSetNull(getLockMode);
setLockMode = closeAndSetNull(setLockMode);
getQueryTimeout = closeAndSetNull(getQueryTimeout);
setQueryTimeout = closeAndSetNull(setQueryTimeout);
}
private static Command closeAndSetNull(Command command) {
if (command != null) {
command.close();
}
return null;
}
/**
* Switches auto commit on or off. Enabling it commits an uncommitted
* transaction, if there is one.
*
* @param autoCommit true for auto commit on, false for off
* @throws SQLException if the connection is closed
*/
@Override
public synchronized void setAutoCommit(boolean autoCommit) throws SQLException {
try {
if (isDebugEnabled()) {
debugCode("setAutoCommit(" + autoCommit + ");");
}
checkClosed();
if (autoCommit && !session.isAutoCommit()) {
commit();
}
session.setAutoCommit(autoCommit);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Gets the current setting for auto commit.
*
* @return true for on, false for off
* @throws SQLException if the connection is closed
*/
@Override
public synchronized boolean getAutoCommit() throws SQLException {
try {
checkClosed();
debugCodeCall("getAutoCommit");
return session.isAutoCommit();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Commits the current transaction. This call has only an effect if auto
* commit is switched off.
*
* @throws SQLException if the connection is closed
*/
@Override
public synchronized void commit() throws SQLException {
try {
debugCodeCall("commit");
checkClosed();
commit = prepareCommand("COMMIT", commit);
commit.executeUpdate();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Rolls back the current transaction. This call has only an effect if auto
* commit is switched off.
*
* @throws SQLException if the connection is closed
*/
@Override
public synchronized void rollback() throws SQLException {
try {
debugCodeCall("rollback");
checkClosed();
rollbackInternal();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Returns true if this connection has been closed.
*
* @return true if close was called
*/
@Override
public boolean isClosed() throws SQLException {
try {
debugCodeCall("isClosed");
return session == null || session.isClosed();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Translates a SQL statement into the database grammar.
*
* @param sql the SQL statement with or without JDBC escape sequences
* @return the translated statement
* @throws SQLException if the connection is closed
*/
@Override
public String nativeSQL(String sql) throws SQLException {
try {
debugCodeCall("nativeSQL", sql);
checkClosed();
return translateSQL(sql);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* According to the JDBC specs, this setting is only a hint to the database
* to enable optimizations - it does not cause writes to be prohibited.
*
* @param readOnly ignored
* @throws SQLException if the connection is closed
*/
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
try {
if (isDebugEnabled()) {
debugCode("setReadOnly(" + readOnly + ");");
}
checkClosed();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Returns true if the database is read-only.
*
* @return if the database is read-only
* @throws SQLException if the connection is closed
*/
@Override
public boolean isReadOnly() throws SQLException {
try {
debugCodeCall("isReadOnly");
checkClosed();
getReadOnly = prepareCommand("CALL READONLY()", getReadOnly);
Result result = getReadOnly.executeQuery(0, false);
result.next();
boolean readOnly = result.currentRow()[0].getBoolean().booleanValue();
return readOnly;
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Set the default catalog name. This call is ignored.
*
* @param catalog ignored
* @throws SQLException if the connection is closed
*/
@Override
public void setCatalog(String catalog) throws SQLException {
try {
debugCodeCall("setCatalog", catalog);
checkClosed();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Gets the current catalog name.
*
* @return the catalog name
* @throws SQLException if the connection is closed
*/
@Override
public String getCatalog() throws SQLException {
try {
debugCodeCall("getCatalog");
checkClosed();
if (catalog == null) {
Command cat = prepareCommand("CALL DATABASE()", Integer.MAX_VALUE);
Result result = cat.executeQuery(0, false);
result.next();
catalog = result.currentRow()[0].getString();
cat.close();
}
return catalog;
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Gets the first warning reported by calls on this object.
*
* @return null
*/
@Override
public SQLWarning getWarnings() throws SQLException {
try {
debugCodeCall("getWarnings");
checkClosed();
return null;
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Clears all warnings.
*/
@Override
public void clearWarnings() throws SQLException {
try {
debugCodeCall("clearWarnings");
checkClosed();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Creates a prepared statement with the specified result set type and
* concurrency.
*
* @param sql the SQL statement
* @param resultSetType the result set type (ResultSet.TYPE_*)
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*)
* @return the prepared statement
* @throws SQLException
* if the connection is closed or the result set type or
* concurrency are not supported
*/
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
try {
int id = getNextTraceId(TraceObject.PREPARED_STATEMENT);
if (isDebugEnabled()) {
debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, "prepareStatement("
+ quote(sql) + ", " + resultSetType + ", " + resultSetConcurrency + ")");
}
checkTypeConcurrency(resultSetType, resultSetConcurrency);
checkClosed();
sql = translateSQL(sql);
return new JdbcPreparedStatement(this, sql, id, resultSetType, resultSetConcurrency, false);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Changes the current transaction isolation level. Calling this method will
* commit an open transaction, even if the new level is the same as the old
* one, except if the level is not supported. Internally, this method calls
* SET LOCK_MODE. The following isolation levels are supported:
* <ul>
* <li> Connection.TRANSACTION_READ_UNCOMMITTED = SET LOCK_MODE 0: no
* locking (should only be used for testing). </li>
* <li>Connection.TRANSACTION_SERIALIZABLE = SET LOCK_MODE 1: table level
* locking. </li>
* <li>Connection.TRANSACTION_READ_COMMITTED = SET LOCK_MODE 3: table
* level locking, but read locks are released immediately (default). </li>
* </ul>
* This setting is not persistent. Please note that using
* TRANSACTION_READ_UNCOMMITTED while at the same time using multiple
* connections may result in inconsistent transactions.
*
* @param level the new transaction isolation level:
* Connection.TRANSACTION_READ_UNCOMMITTED,
* Connection.TRANSACTION_READ_COMMITTED, or
* Connection.TRANSACTION_SERIALIZABLE
* @throws SQLException if the connection is closed or the isolation level
* is not supported
*/
@Override
public void setTransactionIsolation(int level) throws SQLException {
try {
debugCodeCall("setTransactionIsolation", level);
checkClosed();
int lockMode;
switch (level) {
case Connection.TRANSACTION_READ_UNCOMMITTED:
lockMode = Constants.LOCK_MODE_OFF;
break;
case Connection.TRANSACTION_READ_COMMITTED:
lockMode = Constants.LOCK_MODE_READ_COMMITTED;
break;
case Connection.TRANSACTION_REPEATABLE_READ:
case Connection.TRANSACTION_SERIALIZABLE:
lockMode = Constants.LOCK_MODE_TABLE;
break;
default:
throw DbException.getInvalidValueException("level", level);
}
commit();
setLockMode = prepareCommand("SET LOCK_MODE ?", setLockMode);
setLockMode.getParameters().get(0).setValue(ValueInt.get(lockMode), false);
setLockMode.executeUpdate();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* INTERNAL
*/
public void setQueryTimeout(int seconds) throws SQLException {
try {
debugCodeCall("setQueryTimeout", seconds);
checkClosed();
setQueryTimeout = prepareCommand("SET QUERY_TIMEOUT ?", setQueryTimeout);
setQueryTimeout.getParameters().get(0).setValue(ValueInt.get(seconds * 1000), false);
setQueryTimeout.executeUpdate();
queryTimeoutCache = seconds;
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* INTERNAL
*/
public int getQueryTimeout() throws SQLException {
try {
debugCodeCall("getQueryTimeout");
if (queryTimeoutCache == -1) {
checkClosed();
getQueryTimeout = prepareCommand("SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME=?",
getQueryTimeout);
getQueryTimeout.getParameters().get(0).setValue(ValueString.get("QUERY_TIMEOUT"), false);
Result result = getQueryTimeout.executeQuery(0, false);
result.next();
int queryTimeout = result.currentRow()[0].getInt();
result.close();
if (queryTimeout != 0) {
// round to the next second, otherwise 999 millis would return 0 seconds
queryTimeout = (queryTimeout + 999) / 1000;
}
queryTimeoutCache = queryTimeout;
}
return queryTimeoutCache;
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Returns the current transaction isolation level.
*
* @return the isolation level.
* @throws SQLException if the connection is closed
*/
@Override
public int getTransactionIsolation() throws SQLException {
try {
debugCodeCall("getTransactionIsolation");
checkClosed();
getLockMode = prepareCommand("CALL LOCK_MODE()", getLockMode);
Result result = getLockMode.executeQuery(0, false);
result.next();
int lockMode = result.currentRow()[0].getInt();
result.close();
int transactionIsolationLevel;
switch (lockMode) {
case Constants.LOCK_MODE_OFF:
transactionIsolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
break;
case Constants.LOCK_MODE_READ_COMMITTED:
transactionIsolationLevel = Connection.TRANSACTION_READ_COMMITTED;
break;
case Constants.LOCK_MODE_TABLE:
case Constants.LOCK_MODE_TABLE_GC:
transactionIsolationLevel = Connection.TRANSACTION_SERIALIZABLE;
break;
default:
throw DbException.throwInternalError("lockMode:" + lockMode);
}
return transactionIsolationLevel;
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Changes the current result set holdability.
*
* @param holdability
* ResultSet.HOLD_CURSORS_OVER_COMMIT or
* ResultSet.CLOSE_CURSORS_AT_COMMIT;
* @throws SQLException
* if the connection is closed or the holdability is not
* supported
*/
@Override
public void setHoldability(int holdability) throws SQLException {
try {
debugCodeCall("setHoldability", holdability);
checkClosed();
checkHoldability(holdability);
this.holdability = holdability;
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Returns the current result set holdability.
*
* @return the holdability
* @throws SQLException if the connection is closed
*/
@Override
public int getHoldability() throws SQLException {
try {
debugCodeCall("getHoldability");
checkClosed();
return holdability;
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Gets the type map.
*
* @return null
* @throws SQLException if the connection is closed
*/
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
try {
debugCodeCall("getTypeMap");
checkClosed();
return null;
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* [Partially supported] Sets the type map. This is only supported if the
* map is empty or null.
*/
@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
try {
debugCode("setTypeMap(" + quoteMap(map) + ");");
checkMap(map);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Creates a new callable statement.
*
* @param sql the SQL statement
* @return the callable statement
* @throws SQLException
* if the connection is closed or the statement is not valid
*/
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
try {
int id = getNextTraceId(TraceObject.CALLABLE_STATEMENT);
if (isDebugEnabled()) {
debugCodeAssign("CallableStatement", TraceObject.CALLABLE_STATEMENT, id, "prepareCall(" + quote(sql)
+ ")");
}
checkClosed();
sql = translateSQL(sql);
return new JdbcCallableStatement(this, sql, id, ResultSet.TYPE_FORWARD_ONLY,
Constants.DEFAULT_RESULT_SET_CONCURRENCY);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Creates a callable statement with the specified result set type and
* concurrency.
*
* @param sql the SQL statement
* @param resultSetType the result set type (ResultSet.TYPE_*)
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*)
* @return the callable statement
* @throws SQLException
* if the connection is closed or the result set type or
* concurrency are not supported
*/
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
try {
int id = getNextTraceId(TraceObject.CALLABLE_STATEMENT);
if (isDebugEnabled()) {
debugCodeAssign("CallableStatement", TraceObject.CALLABLE_STATEMENT, id, "prepareCall(" + quote(sql)
+ ", " + resultSetType + ", " + resultSetConcurrency + ")");
}
checkTypeConcurrency(resultSetType, resultSetConcurrency);
checkClosed();
sql = translateSQL(sql);
return new JdbcCallableStatement(this, sql, id, resultSetType, resultSetConcurrency);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Creates a callable statement with the specified result set type,
* concurrency, and holdability.
*
* @param sql the SQL statement
* @param resultSetType the result set type (ResultSet.TYPE_*)
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*)
* @param resultSetHoldability the holdability (ResultSet.HOLD* / CLOSE*)
* @return the callable statement
* @throws SQLException
* if the connection is closed or the result set type,
* concurrency, or holdability are not supported
*/
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
try {
int id = getNextTraceId(TraceObject.CALLABLE_STATEMENT);
if (isDebugEnabled()) {
debugCodeAssign("CallableStatement", TraceObject.CALLABLE_STATEMENT, id, "prepareCall(" + quote(sql)
+ ", " + resultSetType + ", " + resultSetConcurrency + ", " + resultSetHoldability + ")");
}
checkTypeConcurrency(resultSetType, resultSetConcurrency);
checkHoldability(resultSetHoldability);
checkClosed();
sql = translateSQL(sql);
return new JdbcCallableStatement(this, sql, id, resultSetType, resultSetConcurrency);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Creates a new unnamed savepoint.
*
* @return the new savepoint
*/
@Override
public Savepoint setSavepoint() throws SQLException {
try {
int id = getNextTraceId(TraceObject.SAVEPOINT);
if (isDebugEnabled()) {
debugCodeAssign("Savepoint", TraceObject.SAVEPOINT, id, "setSavepoint()");
}
checkClosed();
Command set = prepareCommand("SAVEPOINT " + JdbcSavepoint.getName(null, savepointId), Integer.MAX_VALUE);
set.executeUpdate();
JdbcSavepoint savepoint = new JdbcSavepoint(this, savepointId, null, trace, id);
savepointId++;
return savepoint;
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Creates a new named savepoint.
*
* @param name the savepoint name
* @return the new savepoint
*/
@Override
public Savepoint setSavepoint(String name) throws SQLException {
try {
int id = getNextTraceId(TraceObject.SAVEPOINT);
if (isDebugEnabled()) {
debugCodeAssign("Savepoint", TraceObject.SAVEPOINT, id, "setSavepoint(" + quote(name) + ")");
}
checkClosed();
Command set = prepareCommand("SAVEPOINT " + JdbcSavepoint.getName(name, 0), Integer.MAX_VALUE);
set.executeUpdate();
JdbcSavepoint savepoint = new JdbcSavepoint(this, 0, name, trace, id);
return savepoint;
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Rolls back to a savepoint.
*
* @param savepoint the savepoint
*/
@Override
public void rollback(Savepoint savepoint) throws SQLException {
try {
JdbcSavepoint sp = convertSavepoint(savepoint);
debugCode("rollback(" + sp.getTraceObjectName() + ");");
checkClosed();
sp.rollback();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Releases a savepoint.
*
* @param savepoint the savepoint to release
*/
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
try {
debugCode("releaseSavepoint(savepoint);");
checkClosed();
convertSavepoint(savepoint).release();
} catch (Exception e) {
throw logAndConvert(e);
}
}
private static JdbcSavepoint convertSavepoint(Savepoint savepoint) {
if (!(savepoint instanceof JdbcSavepoint)) {
throw DbException.get(ErrorCode.SAVEPOINT_IS_INVALID_1, "" + savepoint);
}
return (JdbcSavepoint) savepoint;
}
/**
* Creates a prepared statement with the specified result set type,
* concurrency, and holdability.
*
* @param sql the SQL statement
* @param resultSetType the result set type (ResultSet.TYPE_*)
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*)
* @param resultSetHoldability the holdability (ResultSet.HOLD* / CLOSE*)
* @return the prepared statement
* @throws SQLException if the connection is closed or the result set type,
* concurrency, or holdability are not supported
*/
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
try {
int id = getNextTraceId(TraceObject.PREPARED_STATEMENT);
if (isDebugEnabled()) {
debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, "prepareStatement("
+ quote(sql) + ", " + resultSetType + ", " + resultSetConcurrency + ", " + resultSetHoldability