AmbGup
AmbGup

Reputation: 771

How to re-establish lost KDB connection in Java

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

Answers (3)

abhi
abhi

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:

  1. To alert the rest of your application, do so in the catch block. One way to go about doing this is to invoke another call back method to alert that the connection is now down.
  2. To reconnect, do this as well in your catch block as well. Be careful to only do this at a specified interval, though. The reason why I mention that is because when a Java application tries to reconnect, it will create a new c object.

This should help your application handle reconnecting more efficiently, and notifying any other dependencies appropriately.

Upvotes: 0

Anton Dovzhenko
Anton Dovzhenko

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

  1. invoke initConnection method to re-establish connection
  2. re-execute query

Would 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

Tschallacka
Tschallacka

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

Related Questions