package io.vertx.ext.asyncsql.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.core.shareddata.Shareable;
import io.vertx.ext.asyncsql.AsyncSQLClient;
class ClientHolder implements Shareable {
private final Vertx vertx;
private final JsonObject config;
private final boolean mySQL;
private final Runnable closeRunner;
private AsyncSQLClient client;
private int refCount = 1;
ClientHolder(Vertx vertx, JsonObject config, boolean mySQL, Runnable closeRunner) {
this.vertx = vertx;
this.config = config;
this.mySQL = mySQL;
this.closeRunner = closeRunner;
}
synchronized AsyncSQLClient client() {
if (client == null) {
client = new AsyncSQLClientImpl(vertx, config, mySQL);
}
return client;
}
synchronized void incRefCount() {
refCount++;
}
synchronized void close(Handler<AsyncResult<Void>> whenDone) {
if (--refCount == 0) {
if (client != null) {
client.close(whenDone);
}
if (closeRunner != null) {
closeRunner.run();
}
}
}
}