Reputation: 771
In my Java application I am establishing KDB connection using
private void initConnection() throws KException, IOException {
conn = new c(host, port);
conn.tz = TimeZone.getTimeZone(CONNECTION_TIMEZONE);
}
Then I use conn
for inserting data asynchronously in tables successfully.
KDB server is restarted once in week and at that time as connection is reset by server I am getting an exception java.net.SocketException
.
Using the above exception I can again re-establish lost connection but this is not a decent solution as I lose batch etc. and I don’t want connection exception to be thrown at the time of insertion.
Instead, is there a call-back/listener/Exception method in KDB API where API can inform java application upfront if a connection is closed by server / lost?
Upvotes: 3
Views: 778
Reputation: 1756
This answer is related (and possibly adds on) to Anton Dovzhenko's answer above, but hopefully the below helps you as well.
If we take a look at kx's documentation for reconnecting to a q process automatically, we can observe the following code snippet from ReconnectionExample.java
below.
//initiate reconnect loop (possibly within a catch block).
while (true) {
try {
System.err.println("Connection failed - retrying..");
//Wait a bit before trying to reconnect
Thread.sleep(5000);
qConnection = qConnFactory.getQConnection();
System.out.println("Connection re-established! Resuming..");
//Exit loop
break;
} catch (IOException | KException e1) {
//resume loop if it fails
continue;
}
…
}
That would be one way to try and reconnect; within a catch block. However, to answer your questions in greater detail:
c
object.This should help your application handle reconnecting more efficiently, and notifying any other dependencies appropriately.
Upvotes: 0
Reputation: 2569
If connection is lost, I would expect conn
object to throw IOException
or its successor, when you try to hit KDB.
Ie, you should catch IOException
and
initConnection
method to re-establish connectionWould be nice to set reasonable threshold of retries either. Otherwise, if KDb is down, the code will keep trying to connect to it.
Upvotes: 1
Reputation: 28742
Why don't you just do a
SELECT 1;
on your connection.
If it's open it'll run.
If it's closed it should throw an error because of the failing network connection.
You'd run this test only in the moments that your kbd server is scheduled for restart.
Personally, on the scheduled moments of restarting the kbd database I'd dump the data into an sqlite database for holding, and then when the frame for rebooting is over and connection is re-established dump the contents of the sqlite database into the kbd database.
Upvotes: 2