Silly Freak
Silly Freak

Reputation: 4231

Configuring data source URL in Spring without XML

I have a simple Web MVC application using Spring Boot that communicates with a database; the DB is H2 and has been in memory until now. I want to change that, and thus use a jdbc:h2:file:... URL.

Up until now, I have not needed to add any XML to configure my application, and I'd prefer it to stay that way if possible. But I can't figure out how to specify a different JDBC URL. I obtained and inspected the data source by passing it to an @Bean method:

org.apache.tomcat.jdbc.pool.DataSource@745e6f01{ConnectionPool[
defaultAutoCommit=null; 
defaultReadOnly=null; 
defaultTransactionIsolation=-1; 
defaultCatalog=null; 
driverClassName=org.h2.Driver; 
maxActive=100; 
maxIdle=100; 
minIdle=10; 
initialSize=10; 
maxWait=30000; 
testOnBorrow=false; 
testOnReturn=false; 
timeBetweenEvictionRunsMillis=5000; 
numTestsPerEvictionRun=0; 
minEvictableIdleTimeMillis=60000; 
testWhileIdle=false; 
testOnConnect=false; 
password=********; 
url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE; 
username=sa; 
validationQuery=null; 
validationQueryTimeout=-1; 
validatorClassName=null; 
validationInterval=30000; 
accessToUnderlyingConnectionAllowed=true; 
removeAbandoned=false; 
removeAbandonedTimeout=60; 
logAbandoned=false; 
connectionProperties=null; 
initSQL=null; 
jdbcInterceptors=null; 
jmxEnabled=true; 
fairQueue=true; 
useEquals=true; 
abandonWhenPercentageFull=0; 
maxAge=0; 
useLock=false; 
dataSource=null; 
dataSourceJNDI=null; 
suspectTimeout=0; 
alternateUsernameAllowed=false; 
commitOnReturn=false; 
rollbackOnReturn=false; 
useDisposableConnectionFacade=true; 
logValidationErrors=false; 
propagateInterruptState=false; 
ignoreExceptionOnPreLoad=false; 
}

(newlines mine)

The setup of that bean seems rather intricate, so I want to interfere with it as little as possible - just replace the default JDBC URL.

How can I configure individual properties for Spring to create the datasource? Preferably in Java, but if there is a concise XML way I'm happy as well. I just want to avoid adding 100 lines of boilerplate for something equivalent to url=...

Upvotes: 0

Views: 1760

Answers (1)

M. Deinum
M. Deinum

Reputation: 124441

A DataSource is auto configured by Spring Boot for you. To influence how and what there are several properties you can set. Those are prefixed with spring.datasource, for a list take a look at the Spring Boot Reference Guide for a full list.

In your case simply add the following to the application.properties file

spring.datasource.url=jdbc:h2:file:...

This will tell Spring Boot to use this URL instead of the default.

As H2 is considered an in-memory database and not a regular database, when using JPA this will lead to your database to be dropped when the application is stopped. To fix this simply add the following

spring.jpa.hibernate.ddl-auto=update

To specify a dialect simply add the following

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

or even simpler

spring.jpa.database=H2

Upvotes: 1

Related Questions