Multiple schema for SQL Server in Spring boot - java

My schema names are different for Test and Prod environments. How to access them from properties file? I want to read the schema from the properties file based on the environment. Tried the link suggested by adding in datasource config but it is not working. The database is SQL server.

you need to create a properties file for each profile (applications-test.properties and applications-prod.properties) and for each env you put your database configuration.
Make sure to use the right profile in your code.

Related

Connection to Any one Data base in SpringBoot

Is there any way to load the respective hibernate config based on the property configured in a properties file.
I have an application to connect to Any database with the same schema through hibernate configs. Right now, I have created two hibernate configs One for DynamoDB another for all hibernate supported SQL's
I wanted to load only respective config by ignoring other config's.
i think we can specify like below
#Profile("prod")
#Configuration
Is it possible to create another property like profile.
There can be multiple profiles active at the same time in your application.
So you can have profiles for specific databases along with the profiles responsible for environment type.
Then, you can define your properties in files called application-dynamodb.properties and application-other.properties
Yes you can use #profile concept here.You can maintain multiple properties file with different configuration and activate that specific configurations for example by adding
spring.profiles.active=dev in application.properties, if application-dev.properties contains the required db configurations.But then you need to add #profile("dev") in configuration class while you are initializing/creating DB connection working with profile

How to load configurations in spring application from two sources where configuration from first source is required to load second one

I have spring based java application, The application support different databases and can be deployed on various containers. We have two type of configuration sources:
- Properties file: Bare minimum configuration to start the application. i.e application datasource JNDI name and database type.
- Config stored in DB as JSON/ hocon: Other configurations used by application.
what is the best way to have it implemented in spring context file, so that these configurations can be made available to the spring beans. The key problem here is to extract configuration from DB I need values from properties file (to locate the datasource).
Is to possible to have overriding feature where properties file have some default configurations, which are overridden by configuration loaded from DB. I have tried using multiple properties file, but not sure how I can have two property sources.

How to use same jpa repository with multiple databases using spring boot [duplicate]

This question already has answers here:
How can I provide different database configurations with Spring Boot?
(2 answers)
Closed 4 years ago.
I have two database environments, one for development and one for testing.
Both databases having same table structures. I want to show data from specific environment. Environment name will be passed at runtime.
How can I use spring boot same jpa repositories with diff environment?
You can try with spring boot profiles : Spring Profiles provide a way to segregate parts of your application configuration and make it be available only in certain environments.
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
http://www.baeldung.com/spring-profiles
You need to externalize the database level properties to some property file that will be read from fixed file system path. This property file should not be part of war/jar created. You can change values of property file at time of starting the application. This way, you don't need to make build every time if there is any change in DB environment properties. At any time it is very easy to connect to any new environment.
You can use SPRING_PROFILES_ACTIVE env. variable to control your app profiles in run-time.
Don't forget to add to your app different props files related to your Spring profiles, for example:
/model
/repositories
/resources
- application.properties // common properties for both 'dev' and 'prod' profiles
- application-dev.properties // 'dev' profile properties, for example - of your H2 database
- application-prod.properties // 'prod' profile properties, for example - of your prod PostgreSQL database
Note that you can run your app also like this:
java -jar -Dspring.profiles.active=dev target/my-app.jar
java -jar target/my-app.jar --spring.profiles.active=dev
Set your active profile in your IDE (for example) to run app with this profile in IDE.
Set your 'default' profile in the application.properties:
spring.profiles.active=dev

Using environment based configuration properties in java library

I have one java spring boot library and it is using some configuration as below using zookeeper address for loadbalancer.
<user:registry regProtocol="zookeeper" name="testZk" address="${zookeeper.address}"/>
zookeeper.address will be different between development and production environments.
Users of this library can include zookeeper.address in their cloud config properties based on the environment but are there other ways so that library users don't need to include these in their properties and library in some way use different properties based on environment from user.
Serving Plain Text will resolve above problem.
http://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.0.0.M5/single/spring-cloud-config.html#_serving_plain_text
Just define multiple environments, you wish to in application properties and on the user side activate the properties, it will work.
I'm pretty new with java but I think that they could use an application.properties file to overwrite any environmental properties.
application.properties in spring

Store properties in database, but override locally

Currently, we store our application's environment properties in a .properties file in the WEB-INF. We want to move them to a database table. But we still want to specify the jndi name, and when running in our test environment locally, we want to be able to override certain properties just for our workspace for test and development.
Apache commons' DatabaseConfigurator seemed nice, but wouldn't play nice with the jndi name being defined as a property in the file. Nothing I did to ask it to look at the property file first worked.
I decided to subclass apache commons' AbstractConfiguration to try to create a single configurator that would check the file and database as I wished, but again, it didn't really work. Spring wants that jndi name absolutely first, probably because the data source has to be passed into the configurator as a parameter.
How can I get what I am after here? Mostly properties in the database, but those that are in the file override them. And jndi name for the datasource should not have to be hardcoded in the spring config.
Why don't you write a ApplicationContext listener that will read the configuration from your DB and inject them in the JNDI? Then you can override the configuration in the JNDI with a context.xml file that will be placed in the src/local/webapp/META-INF/.
This is how we get this working in our webapp.

Categories

Resources