I have created a restful API of the following where I have attempted to allow maximum connections asynchronously without blocking the HTTP thread.
@GET
@Path("sites")
@Produces(value = MediaType.APPLICATION_JSON)
public void getSites(@Suspended AsyncResponse response) {
System.out.println("calling sites");
CompletableFuture
.supplyAsync(() -> DatabaseManager.getInstance().getSites())
.exceptionally(this::handel)
.thenAccept(response::resume);
}
However, the problem occurs once the DatabaseManager method is called. Because a new thread is created for every HTTP connection. This in turn creates a new database connection to the database server.
This then causes the database to error "Too many connections"
So what I am trying to create is some kind of blocking function that will hold the new threads in a waiting state until the DatabaseManager is idle and only allow a single database connection at any time.