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
@@ -382,9 +383,40 @@ public SQLXML createSQLXML() throws SQLException {
382
383
throw new SQLFeatureNotSupportedException ();
383
384
}
384
385
386
+ /**
387
+ * {@inheritDoc}
388
+ *
389
+ * @param timeout temporally ignored param
390
+ *
391
+ * @return connection activity status
392
+ */
385
393
@ Override
386
394
public boolean isValid (int timeout ) throws SQLException {
387
- return true ;
395
+ if (timeout < 0 ) {
396
+ throw new SQLNonTransientException (
397
+ "Timeout cannot be negative" ,
398
+ SQLStates .INVALID_PARAMETER_VALUE .getSqlState ()
399
+ );
400
+ }
401
+ if (isClosed ()) {
402
+ return false ;
403
+ }
404
+ return checkConnection (timeout );
405
+ }
406
+
407
+ private boolean checkConnection (int timeout ) {
408
+ ResultSet resultSet = null ;
409
+ try (Statement pingStatement = createStatement ()) {
410
+ // todo: before use timeout we need to provide query timeout per statement
411
+
412
+ resultSet = pingStatement .executeQuery (PING_QUERY );
413
+ boolean isValid = resultSet .next () && resultSet .getInt (1 ) == 1 ;
414
+ resultSet .close ();
415
+
416
+ return isValid ;
417
+ } catch (SQLException e ) {
418
+ return false ;
419
+ }
388
420
}
389
421
390
422
@ Override
Original file line number Diff line number Diff line change @@ -114,6 +114,15 @@ public void execute() throws Throwable {
114
114
assertEquals (5 , i );
115
115
}
116
116
117
+ @ Test
118
+ void testIsValidCheck () throws SQLException {
119
+ assertTrue (conn .isValid (2000 ));
120
+ assertThrows (SQLException .class , () -> conn .isValid (-1000 ));
121
+
122
+ conn .close ();
123
+ assertFalse (conn .isValid (2000 ));
124
+ }
125
+
117
126
@ Test
118
127
public void testConnectionUnwrap () throws SQLException {
119
128
assertEquals (conn , conn .unwrap (SQLConnection .class ));
You can’t perform that action at this time.
0 commit comments