Thursday, July 16, 2015

Spring Boot with non JDBC database url

Using Spring Boot a DataSource usually is configured using spring.datasource.* properties as shown in the following example:

However database as a service providers (like Heroku Postgres, Compose PostgreSQL, ClearDB MySQL) provide the connection parameters in a format of
scheme://user:password@host:port/path
Would be nice to have a single URI property in Spring Boot to configure database connection properties, similar how we can configure a MongoDB connection via spring.data.mongodb.uri or a Redis connection via spring.redis.uri

Below you find how you could use a single URI property, in this case spring.datasource.uri to specify the database connection parameters to a Heroku PostgreSQL database service. The demo application is running on Heroku and using three datastore services:



The connection properties are managed on Heroku:



In the application we just reference the Heroku config variables, encapsulating them in a heroku profile.

Then we just need to extract the connection properties from the spring.datasource.uri and create a DataSource. In this example we use the Tomcat JDBC Connection Pool to create the DataSource instance.

The demo application is available on my github profile.