Search code examples
javakdbq-lang

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?


Solution

  • 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.