Skip to content

Commit

Permalink
[#3562] Migrate to Quarkus JDBC implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
harism committed Oct 10, 2023
1 parent a3bd800 commit b843c46
Show file tree
Hide file tree
Showing 19 changed files with 616 additions and 112 deletions.
6 changes: 5 additions & 1 deletion services/base-jdbc/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2020, 2021 Contributors to the Eclipse Foundation
Copyright (c) 2020, 2023 Contributors to the Eclipse Foundation
See the NOTICE file(s) distributed with this work for additional
information regarding copyright ownership.
Expand Down Expand Up @@ -86,6 +86,10 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
</dependency>

<!-- testing -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*******************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

package org.eclipse.hono.service.base.jdbc.client;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.agroal.api.AgroalDataSource;
import io.vertx.codegen.annotations.Nullable;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.ext.sql.ResultSet;

/**
* Quarkus Agroal DataSource based JDBC client.
*/
public class JdbcClient implements JdbcOperations {

private final Vertx vertx;
private final AgroalDataSource dataSource;
private final ExecutorService pool;

/**
* JdbcClient constructor.
* @param vertx Vert.x instance to use
* @param dataSource Quarkus Argoal data source
*/
public JdbcClient(final Vertx vertx, final AgroalDataSource dataSource) {
this.vertx = vertx;
this.dataSource = dataSource;
this.pool = Executors.newCachedThreadPool();
}

/**
* Open connection asynchronously.
* @param handler Asynchronous listener
*/
public synchronized void getConnection(final Handler<AsyncResult<JdbcConnection>> handler) {
getConnectionFuture().onComplete(handler);
}

/**
* Close connection asynchronously.
* @param handler Asynchronous listener
*/
public void close(final Handler<AsyncResult<Void>> handler) {
vertx.runOnContext(ignore -> {
dataSource.close();
handler.handle(Future.succeededFuture());
});
};

/**
* Close connection synchronously.
*/
public void close() {
dataSource.close();
}

@Override
public void queryWithParams(final String sql, final JsonArray jsonArray, final Handler<AsyncResult<ResultSet>> handler) {
vertx.runOnContext(ignore -> {
getConnectionFuture().flatMap(connection -> {
final Promise<ResultSet> promise = Promise.promise();
connection.queryWithParams(sql, jsonArray, promise);
promise.future().compose(result -> {
connection.close();
return Future.succeededFuture(result);
}).onComplete(handler);
return Future.succeededFuture();
}).onFailure(t -> handler.handle(Future.failedFuture(t)));
});
}

@Override
public void querySingle(final String sql, final Handler<AsyncResult<@Nullable JsonArray>> handler) {
vertx.runOnContext(ignore -> {
getConnectionFuture().flatMap(connection -> {
final Promise<@Nullable JsonArray> promise = Promise.promise();
connection.querySingle(sql, promise);
promise.future().compose(result -> {
connection.close();
return Future.succeededFuture(result);
}).onComplete(handler);
return Future.succeededFuture();
}).onFailure(t -> handler.handle(Future.failedFuture(t)));
});
}

@Override
public void querySingleWithParams(final String sql, final JsonArray arguments, final Handler<AsyncResult<@Nullable JsonArray>> handler) {
vertx.runOnContext(ignore -> {
getConnectionFuture().flatMap(connection -> {
final Promise<@Nullable JsonArray> promise = Promise.promise();
connection.querySingleWithParams(sql, arguments, promise);
promise.future().compose(result -> {
connection.close();
return Future.succeededFuture(result);
}).onComplete(handler);
return Future.succeededFuture();
}).onFailure(t -> handler.handle(Future.failedFuture(t)));
});
}

@Override
public void updateWithParams(final String sql, final JsonArray jsonArray, final Handler<AsyncResult<Integer>> handler) {
vertx.runOnContext(ignore -> {
getConnectionFuture().flatMap(connection -> {
final Promise<Integer> promise = Promise.promise();
connection.updateWithParams(sql, jsonArray, promise);
promise.future().compose(result -> {
connection.close();
return Future.succeededFuture(result);
}).onComplete(handler);
return Future.succeededFuture();
}).onFailure(t -> handler.handle(Future.failedFuture(t)));
});
}

@Override
public void call(final String sql, final Handler<AsyncResult<Void>> handler) {
vertx.runOnContext(ignore -> {
getConnectionFuture().flatMap(connection -> {
final Promise<Void> promise = Promise.promise();
connection.call(sql, promise);
promise.future().compose(result -> {
connection.close();
return Future.succeededFuture(result);
}).onComplete(handler);
return Future.succeededFuture();
}).onFailure(t -> handler.handle(Future.failedFuture(t)));
});
}

private Future<JdbcConnection> getConnectionFuture() {
final Promise<JdbcConnection> promise = Promise.promise();
this.pool.execute(() -> {
try {
final Connection connection = dataSource.getConnection();
promise.complete(new JdbcConnection(pool, connection));
} catch (SQLException ex) {
promise.fail(ex);
}
});
return promise.future();
}

}
Loading

0 comments on commit b843c46

Please sign in to comment.