= Managing web server connections =

I have a JavaFx application thatrequests to a db through http. I intermittently encounter problems where all available connections to the webserver (191) are used up, causing a crash

I'm trying to work out why this happens, as it seems to be that the actual connections should be much lower. There are at maximum 40 instances of the application running at a time

Here is a typical usage method (Kotlin):
fun myHTTPFunction(user: String, phone: String, customCID: Boolean, cID: String) { val httpClient = HttpClients.createDefault() val response: CloseableHttpResponse try { val useCID = when(customCID) { true -> "&group_alias=$cID" else -> "" } val httpPost = HttpPost(PHP ADDRESS) response = httpClient.execute(httpPost) EntityUtils.consume(response.entity) try {response.closecatch (e: Exception) {exception.showStackTrace(e)} try {httpClient.closecatch (e: Exception) {exception.showStackTrace(e)} } catch(e: Exception) { exception.showStackTrace(e) } }
Note: Originally only a single instance of HttpClients was created, and reused for method calls. However, when checking connections on the webserver, this method results in about 30% more connections per active client. (Up to 20 despite only a single 'createDefault method call.)
Using a method like the attached code results in 1-4 connections per client, but this can still cause issues, particularly when 20+ clients are activated at once. Most connections self terminate after a few seconds, but if the 191 connection cap is hit, connections will no longer close and the server crashes

By my understanding, the recommended usage of apache httpclient is to utilize a single client instance, but this does not seem to result in a single