File tree 2 files changed +42
-1
lines changed
main/java/org/tarantool/jdbc
test/java/org/tarantool/jdbc
2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 44
44
public class SQLConnection implements Connection {
45
45
46
46
private static final int UNSET_HOLDABILITY = 0 ;
47
+ private static final String PING_QUERY = "SELECT 1" ;
47
48
48
49
private final TarantoolConnection connection ;
49
50
@@ -374,9 +375,40 @@ public SQLXML createSQLXML() throws SQLException {
374
375
throw new SQLFeatureNotSupportedException ();
375
376
}
376
377
378
+ /**
379
+ * {@inheritDoc}
380
+ *
381
+ * @param timeout temporally ignored param
382
+ *
383
+ * @return connection activity status
384
+ */
377
385
@ Override
378
386
public boolean isValid (int timeout ) throws SQLException {
379
- return true ;
387
+ if (timeout < 0 ) {
388
+ throw new SQLNonTransientException (
389
+ "Timeout cannot be negative" ,
390
+ SQLStates .INVALID_PARAMETER_VALUE .getSqlState ()
391
+ );
392
+ }
393
+ if (isClosed ()) {
394
+ return false ;
395
+ }
396
+ return checkConnection (timeout );
397
+ }
398
+
399
+ private boolean checkConnection (int timeout ) {
400
+ ResultSet resultSet = null ;
401
+ try (Statement pingStatement = createStatement ()) {
402
+ // todo: before use timeout we need to provide query timeout per statement
403
+
404
+ resultSet = pingStatement .executeQuery (PING_QUERY );
405
+ boolean isValid = resultSet .next () && resultSet .getInt (1 ) == 1 ;
406
+ resultSet .close ();
407
+
408
+ return isValid ;
409
+ } catch (SQLException e ) {
410
+ return false ;
411
+ }
380
412
}
381
413
382
414
@ Override
Original file line number Diff line number Diff line change @@ -113,6 +113,15 @@ public void execute() throws Throwable {
113
113
assertEquals (5 , i );
114
114
}
115
115
116
+ @ Test
117
+ void testIsValidCheck () throws SQLException {
118
+ assertTrue (conn .isValid (2000 ));
119
+ assertThrows (SQLException .class , () -> conn .isValid (-1000 ));
120
+
121
+ conn .close ();
122
+ assertFalse (conn .isValid (2000 ));
123
+ }
124
+
116
125
@ Test
117
126
public void testConnectionUnwrap () throws SQLException {
118
127
assertEquals (conn , conn .unwrap (SQLConnection .class ));
You can’t perform that action at this time.
0 commit comments