Cloudant - Connection Pooling

2019-07-27 16:03发布

问题:

I am trying connection pooling with cloudant database. From what I understand, the cloudant database inherently does connection pooling with the default parameter of max_connections = 6. max_connections code. The code snippet required is at line 130.

I have a working application written in scala using akka-http with the following initialisation :

val client: CloudantClient = ClientBuilder.account(<accountdetails>)
.username(<my user name>)
.password(<my password>).maxConnections(20)
.build()
println("creating database")
val db: Database = client.database("exampledb", false)
println("created");

My question is how do I confirm if the connection pooling works as intended for a basic get function from lets say an example student Database.

回答1:

I don’t think akka-http will have an impact here unless it replaces the java.netHttpURLConnection. The CloudantClient uses the java.net.HttpURLConnection class to communicate with the Cloudant database. The implementation of HttpURLConnection is provided by the JVM by default or by OkHttp if you added that dependency. As per the documentation for connection pooling the behaviour changes if you are using the optional OkHttp client or not. The maxConnections method only changes the connection pool size if you are using the OkHttp dependency. For the default HttpURLConnection the pool size is configured by JVM properties.

As per this documentation it is possible to enable logging for the java-cloudant client. As the pool use is transparent to the client code if you want to validate whether a connection is created or leased from the pool you will need logging for the underlying HttpURLConnection. For more information about the log string for the default HttpURLConnection see for example this answer.

If you are concerned whether pooling is working or not, you may also find the discussion on this issue relevant. The default JVM implementation purges connections from the pool after a very short idle time.