How to set "max_allowed_packet" property from spring application.yml - java

I need to change "max_allowed_packet" property for mySQL data base from SPRING property file (application.yml). I found some topics about that, like this.
They proposed to use mySQL command line:
$>mysql --max_allowed_packet={some_value}
But maybe someone know new solution for this task? It would be great to have this ability.

You cannot change --max_allowed_packet property from spring application.yml file, because this is a mysql server property. Therefore you should set this property when you start the mysql server.
You could add it in my.cnf, check this answer

Related

Transfer spring properties file to Database

Can we read the property values from the DB instead of setting up it on application.properties ? or even can we update the property value from code if a default value is set up in the application.properties file
You're probably looking for the Spring Cloud Config Server.
The spring context is initialized with the values from application.properties to which your database config belongs as well. You would need to establish the database connection prior to constructing the whole application context and then reading properties from the database. This can probably be achieved by modifiying how Spring works, but I'd rather recommend using a means officially supported by Spring.

Set application specific properties within the application

Is there any way by which we can set application specific properties within the application? Like server port. Searching solution other than Spring boot and Server.xml. I am using Spring MVC in my web application. Any help is highly appreciated.
You need to use -D option to specify the explicit configuration parameters if you are executing from command line as follows.
java -Dyour_config_param="value" -jar your_app.jar
Have a look at Spring application properties: https://www.tutorialspoint.com/spring_boot/spring_boot_application_properties.htm.
You can also have multiple properties files for your different environments such as E1, E2 or E3 but appending your properties file name like application-e1.properties and then passing the --Dspring.profiles.active=e1 flag to your application.
You can declare property sources in your application.
Here you have a Spring (not Spring Boot) specific blog entry explaining all you need to know about property management:
https://spring.io/blog/2011/02/15/spring-3-1-m1-unified-property-management/
Also, you can check some alternative solutions in this related question:
How to read values from properties file?

Dynamically change application.properties values in spring boot

Currently i am working on a REST based project in Spring Boot.
I have added the api url in 'application.properties' file.
i.e.
application.properties
api-base-url=http://localhost:8080/RestServices/v1
And also this 'api-base-url' value access from java.
In some situations i need to change the 'api-base-url' dynamically.
I have change 'api-base-url' value dynamically & working fine.
But my problem is that
when wildfly restart then the configuration will be reset to default.
i.e
This is my default value
api-base-url=http://localhost:8080/RestServices/v1
dynamically change to
api-base-url=http://10.34.2.3:8080/RestServices/v1
when wildfly restart then the configuration will be reset to default.
i.e.
api-base-url=http://localhost:8080/RestServices/v1
Have any solution for this?
You might want to consider using a cloud config server to host your config. Two examples are Spring Cloud Config and Consul.
These servers will host your application's configuration and your spring boot application will make a call out to the config server on start up to get it's config.
spring-boot-actuator exposes the endpoint /refresh which forces the application to refresh it's configuration. In this case, it will call out to the config server to get the latest version.
This way you can change the config hosted in the config server then hit the /refresh endpoint and the changes will be picked up by your application.
As #moilejter suggests, one possible way is to persist in database table and at start time you simply read from that table instead of application.properties file. Your application.properties files can hold information necessary for database connection.
You would also need a JMX method or a REST API to trigger in your application that the url has changed and which inturn, would simply read from same table. This way you would be safe even if app restarts and you won't lose the override.
You can use BeanFactoryPostProcessor coupled with Environment bean to leverage spring placeholder concept.
#user2214646
Use spring expression language

Spring Boot - How to set the default schema for PostgreSQL?

What I am currently doing in my application.properties file is:
spring.datasource.url=jdbc:postgresql://localhost:5432/myDB?currentSchema=mySchema
Isn't there another property for this? Since it looks hacky and according to a post (still finding the link sorry, will update later), it is only applicable to PostgreSQL 9.4.
Since this is quite old and has no proper answer yet, the right property to set is the following:
spring.jpa.properties.hibernate.default_schema=yourschema
You can try setting the default schema for the jdbc user.
1)
ALTER USER user_name SET search_path to 'schema'
2) Did you try this property?
spring.datasource.schema
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
With SpringBoot 2.5.5 the only thing that work for me:
aplication.properties
spring.datasource.hikari.schema=my_schema
aplication.yml version:
spring:
datasource:
hikari:
schema: my_schema

Does spring boot support using both properties and yml files at the same time?

I have a spring boot application and I want to use both a yml file for my application properties and also a plain application-${profile}.properties file set to configure my application.
So my question is can this be done and if so, how do you configure spring boot to look for both the yml file and the properties and merge them into one set per environment?
As to why I want/need to use both, it is because I like the flexibility and ease of use of yml files but an internal component (for encryption) requires using the properties file set.
I did see this point being made YAML files can’t be loaded via the #PropertySource annotation
but nothing stating whether both can be used together.
Please provide detailed configuration (XML or Java config) on how to get this working.
TIA,
Scott
I can answer my own question, as it just works as you would expect. The application.yml file and the appropriate application-${profile}.properties both get loaded and merged into the environment. Spring boot just makes this work naturally.
Yes You can use both at same time in same project.
When you use both YML and properties at same time, say for example
application.yml and application.properties at same time in same
project, first application.yml will be loaded, later
application.properties will be loaded.
Important point to be noted is that if application.yml and
application.properties have same keys for example in
application.yml has spring.app.name = testYML and
application.properties has spring.app.name = testProperties at same
time in same project, then application.yml value will be overwritten
by application.properties value since it is loading at last.
And the value in spring.app.name = testProperties.
Yes, you can run both without doing any configuration.
In Spring Boot, it picks .properties or .yaml files in the following sequences :
application-{profile}.{properties|yml}
application.{properties|yml}

Categories

Resources