Spring Boot JPA Database Choice - postgresql

How can I start a stand-alone Spring Boot JPA application -- not via cli -- with a choice of databases to get data, e.g., localhost:5432/my_db; or 192.168.1.100:5432/our_db, or example.com:5432/their_db?
Mine currently uses the one in the application.properties file that contains:
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/my_db
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
Thanks in advance

Since you probably need to configure username and password as well, I recommend creating separate application-mydatasource.properties files for each data source configuration. You will then activate the datasource you want to use based on setting the active profile. You can set the active profile either in application.properties (spring.profiles.active) or via a command line argument:
$ java -jar -Dspring.profiles.active=mydatasource demo-0.0.1-SNAPSHOT.jar
The application-mydatasource.properties will then override any properties in your application.properties. I believe you will also need to set spring.profiles= to the list of profiles available.
See Profile specific properties.

Another options besides the #Profile label, that you will have to declare in every enviroment that you will deploy the application, you could use in Spring Boot the label:
#ConditionalOnProperty(name="propertyName", havingValue="propertyValue")
And declare a property to decide wich database you want to load in each case!
Hope being helping!!

Related

How to Configure SpringBootApp with a Mongo Production Database

I am creating a Spring Boot App with Mongo DB and scratching my head a bit with how to set up the production database configuration.
With a SQL-based Database, I'd be used to setting up a data source bean like this
#Bean
public DataSource getDataSource()
{
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.h2.Driver");
dataSourceBuilder.url("jdbc:h2:file:C:/temp/test");
dataSourceBuilder.username("sa");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
However,
It doesn't seem to be needed - my local app connects to a spun up instance of mongo db without any explicit configuration.
It doesn't seem to be a standard with mongo according to [this post][1]
I figured I'd give it a go to see if it would automagically configure in production, but I'm getting a DataAccessResourceFailureException. Info: heroku, did the mLab MongoDB add on.
I have no problem getting the url and I can certainly throw that in an environment variable, but I'm just not sure what I need to add to my app to configure it.
Set values in application.properties file like below
spring.data.mongodb.database = ${SPRING_DATA_MONGODB_DATABASE}
spring.data.mongodb.host = ${SPRING_DATA_MONGODB_HOST}
spring.data.mongodb.port = ${SPRING_DATA_MONGODB_PORT}
You can use the #Value annotation and access the property in whichever Spring bean you're using
#Value("${userBucket.path}")
private String userBucketPath;
The Externalized Configuration section of the Spring Boot docs, explains all the details that you might need.

Spring cloud config properties not honouring config properties

I wish to use consul strictly as a config source.
I am using spring-cloud-consul-config to get my config.
I am using git2consul to load files into consul and read them.
As per the spring cloud documentation I have added the following to my build.gradle
compile ("org.springframework.cloud:spring-cloud-starter-consul-config")
and have the following in my application.properties
spring.application.name=test-service
spring.cloud.consul.config.enabled=true
spring.cloud.consul.enabled=true
spring.cloud.consul.config.format=FILES
The problem I am facing is that the expected properties are not being loaded into the ConfigurationProperties beans. On further debugging in the ConsulPropertySourceLocator::locate(Environment environment) method, I see that the this.properties object is still loaded with KEY_VALUE enum.
This led me to ConsulConfigBootstrapConfiguration class, where the ConsulConfigProperties bean is being instantiated using a constructor.
Is this the problem or do I have something wrong in my setup.
If someone has a working setup of git2consul and spring cloud config, please can you point me to it for reference.
These values that you have in application.properties
spring.application.name=test-service
spring.cloud.consul.config.enabled=true
spring.cloud.consul.enabled=true
spring.cloud.consul.config.format=FILES
need to be in bootstrap.properties.

Configure MongoDB in Spring Boot using environment variables

I'm new to Spring Boot and am trying to configure a connection to MongoDB using environment variables - i.e. I have followed an example online showing how to configure my mongo database/host/port via application.properties, but I want my configuration to come from environment variables - what is the best approach to do this?
Thanks
After some digging, I've managed to get it working by using an application.properties file, but specifying the env variable names within the file, e.g.
spring.data.mongodb.database = ${SPRING_DATA_MONGODB_DATABASE}
spring.data.mongodb.host = ${SPRING_DATA_MONGODB_HOST}
spring.data.mongodb.port = ${SPRING_DATA_MONGODB_PORT}
You can also define a fallback-value if necessary
spring.data.mongodb.host = ${SPRING_DATA_MONGODB_HOST:localhost}

Configuring evolutions script for different environment

These are my configuration files for both development and testing environments. I'm displaying only the db configuration section.
dev.conf
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost/mydb"
db.default.user=admin
db.default.password=admin
applyEvolutions.default=true
evolutionplugin=disabled
test.conf
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost/mytestdb"
db.default.user=admin
db.default.password=admin
applyEvolutions.default=true
evolutionplugin=enabled
Basically I'm planning to execute the evolutions db script only to the testing database. So I will clean up the records before triggering the test-script.
Based on the documentation the evolution scripts has to be put in folder with the same name as the datasource, which is default in this case:
~/conf/evolutions/default/
My question:
Is there a way for me to put the scripts in different location and set the configuration file to refer to that one instead? I'd love to put the test scripts in this path:
~/conf/evolutions/test/
It'll be troublesome for me if in one way or another someone accidentally enable the evolutions in the dev.conf file and since both configuration files share the same datasource name(default) then all the clean-up queries in the default folder are executed.
Another workaround that I can think of right now is by using different datasource name for different environments but this will imply code change because then the application doesn't use the default datasource anymore. I'd like to avoid that.
Maybe you could use the evolutions logic directly from a text fixture of some kind?
play.api.db.evolutions.Evolutions.applyFor(dbName, path) seems like it might do the trick.

How to perform direct HQL queries on grails hibernate DB for test purposes in Eclipse

For testing purposes, I'd like to have a console where I can just enter an HQL command and see what it returns on the grails hibernate DB (in my case a MySQL DB) while it's running e.g. in the test environment. What's the best way to do that?
I'm using Eclipse and already came across the JBoss Hibernate Tools, but I'm not sure how to configure them to use my grails MySQL DB. What Type (Core, Annotations, JPA) do I have to choose there and what to fill in the 'Configuration File' / 'Persistence unit' fields? I already set up a property file (see below).
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc:mysql://localhost/myproject
hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider
But is this the best approach anyway?
I am not 100% sure if your close, but the Driver_class property is wrong for MySQL. Try
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost/myproject
hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider
The Class needs to be com.mysql.jdbc.Driver
Hope this helps