Database Connections
In the last blog we touched upon the connection pooling concepts and understand different settings and tunning parameters. In this blog we will take a look at some sample code and try to replicate some issues that we can see in production and how we should be ready from code point of view.
The example is based on Spring boot and Hikari CP but the concept is application to any tech stack.
You can find the sample application
Understand the implementation
We have used spring boot auto configure to configure the postgres connection pooling.
Take a look at the properties
################### Database Configuration #####################
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url=jdbc:postgresql://host1:5432,host2:5432,host3:5432/store?targetServerType=primary&connectTimeout=10
spring.datasource.username=store
spring.datasource.password=store
spring.datasource.driverClassName=org.postgresql.Driver
#Allow suspending pool via JMX , mostly used for automated test like failover
#spring.datasource.hikari.allow-pool-suspension
#This property sets a SQL statement that will be executed after every new connection creation before adding it to the pool. If this SQL is not valid or throws an exception, it will be treated as a connection failure and the standard retry logic will be followed.
spring.datasource.hikari.connection-init-sql=SET search_path TO mystore; SET TIME ZONE 'UTC';
#This property controls the maximum number of milliseconds that a client (that's you) will wait for a connection from the pool. If this time is exceeded without a connection becoming available, a SQLException will be thrown. Lowest acceptable connection timeout is 250 ms. Default: 30000 (30 seconds)
spring.datasource.hikari.connection-timeout=30000
#Choose whether or not to evict a connection from the pool depending on specific SQL states
#spring.datasource.hikari.exception-override-class-name
#https://github.com/brettwooldridge/HikariCP/wiki/Dropwizard-HealthChecks
#spring.datasource.hikari.health-check-properties
spring.datasource.hikari.idle-timeout=600000
#spring.datasource.hikari.initialization-fail-timeout
#spring.datasource.hikari.isolate-internal-queries
#spring.datasource.hikari.jdbc-url=jdbc:postgresql://localhost:5432/aro
#spring.datasource.hikari.keepalive-time=
#spring.datasource.hikari.leak-detection-threshold
#spring.datasource.hikari.login-timeout
#spring.datasource.hikari.max-lifetime
#spring.datasource.hikari.maximum-pool-size
#spring.datasource.hikari.metrics-tracker-factory
#spring.datasource.hikari.minimum-idle
#spring.datasource.hikari.password=aro
spring.datasource.hikari.pool-name=conndemo
#spring.datasource.hikari.read-only
#spring.datasource.hikari.register-mbeans
#spring.datasource.hikari.scheduled-executor
spring.datasource.hikari.schema=aro
#spring.datasource.hikari.transaction-isolation
#spring.datasource.hikari.username=aro
#spring.datasource.hikari.validation-timeout
#################################################################
logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
we need to ensure we are running postgres in cluster mode with automatic failover. Production system runs with High Availability and more security. we will try to mimic some of them locally and then we will adjust our application code to see the difference in demo code that you see everywhere and preview of real production code.
Some consideration
Client side pooling vs server side pooling
password from vault
use roles instead of user with limited access
use SSL connection
automatic failover
About the author
Tech Enthusiast !!!!
Comments
Join the discussionNo comments yet. Be the first to comment.