Question

There is possible to let Ormlite check the connection before it will be handed over to the user when using JdbcPooledConnectionSource like this:

JdbcPooledConnectionSource connectionSource =
     new JdbcPooledConnectionSource("jdbc:h2:mem:account");
connectionSource.setTestBeforeGet(true);

What is the performance cost to let Ormlite do the checking? Why would I not let the Ormlite check the connection?

Was it helpful?

Solution

What is the performance cost to let Ormlite do the checking?

You can take a look at the source for the JdbcPooledConnectionSource. In the testConnection(...) method it effectively does:

connMetaData.connection.queryForLong(`SELECT 1`);

As to what the performance is, that would depend on a number of factors. This is doing a RPC with your database server but should not be hitting any disk on the server. So on a modern system we are probably talking about 10s of milliseconds – maybe less.

I think the best way to evaluate the cost of this is to turn it on and test your application and then turn it off and see if you notice a difference.

Why would I not let the Ormlite check the connection?

The reason why the setting is there is to protect against a pooled connection failing right when you go to use it. I would say that using it is conservative.

The JdbcPooledConnectionSource gives you other ways to verify the connection that aren't every time you go to get a pooled one. For example, if you call setCheckConnectionsEveryMillis(...) to some non-0 value, it will fork a thread to test the connections every so often. 60000 will have the thread do the SELECT 1 in the background which should not slow your application down.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top