Closing jdbc client

155 views
Skip to first unread message

Buddha Shrestha

unread,
Oct 3, 2016, 2:10:26 AM10/3/16
to vert.x
Hi everyone,

It seems that there is a problem while closing the jdbc client.

My application requires me to create a new jdbc client on each request and close it after the request is done (request involves CRUD operations). But, as the requests go on increasing, the service stops to take any requests ( saying 'Too many files open') and it seems that jdbc client is not properly closed.


and heres the snippet of the function I'm using :

 public static void query(String sql, JsonObject databaseConfig, Handler<AsyncResult<JsonObject>> done) {

        JDBCClient client = JDBCClient.createShared(Holder.getInstance().getVertx(), databaseConfig);
        client.getConnection(connect -> {
            if (connect.failed())
                done.handle(Future.failedFuture(connect.cause()));

            SQLConnection connection = connect.result();
            connection.query(sql, result -> {
                if (result.failed())
                    done.handle(Future.failedFuture(result.cause()));
                else
                    done.handle(Future.succeededFuture(result.result().toJson()));
                connection.close(kil -> {
                    if (kil.failed()) {
                        throw new RuntimeException(kil.cause());
                    }
                });
                client.close();
            });
        });
    }


I'm closing the client after connection is closed.
Is this the right way to close the client? cause I'm having problem with it everytime.

Please suggest. Thanks!

omid pourhadi

unread,
Oct 3, 2016, 3:27:03 AM10/3/16
to vert.x
Hi,

I think this is better.

JDBCClient client = JDBCClient.createNonShared(vertx, config());
       
try {
             client
.getConnection(connect -> {
                 
//your business logic
             
});
       
} finally{
            client
.close();
       
}




Julien Viet

unread,
Oct 3, 2016, 3:37:46 AM10/3/16
to ve...@googlegroups.com
Hi,

we would need to have a smaller reproducer, what you pointed out seems to be an entire app. The reproducer should be usable as a unit test or command line directly or at least with a minimum of instructions to use it. From what you pointed out:

1/ at least the connect failed block should do a return after you call the done hander:

if (connect.failed()) {
  if (connect.failed()) {
    doneHandler.handler(Future.failedFuture(connect.cause()));
    client.close();
    return;
  }
}

2/ you should close the client in the connection.close callback.


--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/431bf92b-e76a-4867-9b31-a8de91133665%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Buddha Shrestha

unread,
Oct 3, 2016, 5:33:38 AM10/3/16
to vert.x
Thanks Julien.

I'll push a small reproducer. In the meanwhile, I'll close the client inside the connection close handler and post the my results later.

Thanks again for the reply.
Reply all
Reply to author
Forward
0 new messages