Configure hibernate to use "show-sql=false" temporarily - java

I have a java progam using Maven, JPA, Eclipse, Jenkins. While developing I have the setting
spring:
jpa:
show-sql: true
in my application.yml file which works fine. Now, for a load test I have huge chunk of data. If I execute the test it works fine in Eclipse, but fails in Maven as the SureFire Plugin fails on such large console output. I can make it work by redirecting the console to a file, but that won't work for Jenkins and it won't work if I start the tests altogether because I want to see the result on the console obviously. So I would like to have this setting (show-sql) be switched off temporarily. I suppose it must live somewhere in the JPA / Hibernate configuration classes, but I couldn't find any combination yet that would allow me to change it.
Any advise is appreciated,
Stephan
The closest I suppose I came to it was by this:
entityManager.setProperty( "hibernate.show_sql", false );
entityManager.setProperty( "spring.jpa.hibernate.show_sql", false );
entityManager.setProperty( "javax.persistence.hibernate.show_sql", false );
Where the entityManager is autowired to the component. But when reading those properties, the return is some values from a completely different namespace (some timeout values), so I reckon I am in the wrong corner...

I assume you are also using spring, what you can do is using profiles which is available since version 3. See this link for more information: Spring Profiles. You can active profiles during runtime. For example in application-loadtest.yml would then be your configuration for your loadtests.
Or as alternative you can add this properties as parameter environment variables or as command line argument: Externalized configuration
I hope this helps.
regards, WiPu

Related

How can I externalize some config variables in spring boot?

I have a yaml file that contains some credentials for azure keyvault. This project is I am working on is a shared git repo so I would like to set these values as environment variables for the whole project not just for myself
application.yml:
azure:
keyvault:
uri:someUri
client-id:someClientId
client-key:someClientKey
but I want to have them set up like this:
azure:
keyvault:
uri: ${uri}
client-id:${clientId}
client-key:${clientKey}
Is there a way to set those values and have this work for others without them having to manually set these values in their environment?
You can ignore the .yaml in gitignore so you can use your config in your environment. And if you pull from others their config won't overwrite your config.
If you want if like dynamically
java -jar myapp.jar --spring.application.json='{"foo":"bar"}'
You can add variables like this in start command. Configure in your Run configuration.
The syntax you've presented above is supposed to work.
I usually use capital letters for 'convention' but other than that its fine.
However providing such a syntax effectively means that you don't want to define property values in your application so you'll have to get them from elsewhere.
From your question I understand is that your primary concern is your teammates and in your setup each one of your peers has clientId, key, etc.
In this case you can create a script that will "export" all the variables to be environment variables automatically, they'll run it only one time and it will work since than.
Another option which is kind of similar is providing a property SPRING_APPLICATION_JSON that will refer to the json with a configuration, spring boot application can read it as written in the official documentation

Quarkus log output when running tests in Maven

If I run unit tests for Quarkus from inside Eclipse, the Eclipse console view shows Quarkurs' log output. However, when I run the same tests in Maven, the Quarkus output is completely swallowed and does not appear anywhere. If there is a test failure due to an exception in the application code I get the test failure message but I cannot see what actually went wrong inside the application. The Java log manager is configured to use JBoss logging by using the surefire system property.
Does anyone know where one can find the Quarkus log output or how it can be enabled?
You haven't posted any code or configuration files, so there is no way to know what may be causing your problem.
Potentially you may want to check out a few things:
In the application configuration file, make sure quarkus.log.console.enable is set to true (if you can't find it don't worry about it, since by default it's true)
Is quarkus.log.file.enable set to true? (by default it's false)
While I can't tell you what the problem is exactly, since there is no code to review, I will put links to the official Quarkus logging guide. What may interest you is under Runtime configuration and Logging configuration reference.

Spring Boot with AspectJ and Aspectweaver not persisting singleton when started with IntelliJ

So, I have a really weird problem which did cost me hours to find.
I have a simple Spring Boot application which I start with a normal Spring Boot run configuration in IntelliJ.
I've added a special Aspect configuration (see https://callistaenterprise.se/blogg/teknik/2020/09/20/multi-tenancy-with-spring-boot-part2/) which requires two Java Agents (spring-instrument, aspectweaver).
I've added them separately to the run configuration "VM options" as following:
-javaagent:target/dependency/spring-instrument-5.3.8.jar -javaagent:target/dependency/aspectjweaver-1.9.6.jar
They successfully get loaded on start.
Wherever the Aspect function is called, a singleton that has been already used/instantiated before, will magically get reinstantiated and is then of course empty.
This does not happen / works as expected, when I use mvn spring-boot:run (or just java -jar -javaagent:.... app.jar) to run the application. In this case, the maven configuration in pom.xml takes care to add the needed agents.
What I think is that somehow IntelliJ adds special stuff to the run command or something else interferes with the application, so that the Aspect function is not called in the same context/instance.
It should not be related to threads, since it's prepared for it.
Does anybody have an idea, what's happening here? I'd like to keep the Spring Boot run configuration and thus the Debug option, since debugging does not seem to work out of the box with mvn spring-bootn:run.

How to disable static content caching of micronaut?

I've created an application that is serving some static files, however while I'm development it's really annoying that the server caches the static content instead of returning what's on disk. And yes, using the development mode with ./gradlew run --continuous IS sub-optimal.
I would like to know if there's a property, configuration or something that I can do to disable static content caching so that in my development profile I can set it.
As discussed int he comments of the other answer, using netty.responses.file.cache-seconds and similar properties doesn't work 'cause that only controls the browser cache.
The problem is that when you're using ./gradlew run --continuous and you have your micronaut.router.static-resources.my-resources.paths set to something like classpath:public/, what will happen is that micronaut, of course, will read from the classpath, which gets changed only when the build is run. In order for you take your current development version without having to rebuild your application, you should use something like:
micronaut:
router:
static-resources:
shared-static:
enabled: true
mapping: /public/**
paths:
- file:src/main/resources/public
By doing this in your development profile, you'll always get the latest version rendered by micronaut.
Should the browser cache also be a problem, then you should combine this answer with https://stackoverflow.com/a/60763922/3073044.
You can control the cache seconds and cache control headers with
https://docs.micronaut.io/latest/guide/configurationreference.html#io.micronaut.http.server.netty.types.files.FileTypeHandlerConfiguration
and
https://docs.micronaut.io/latest/guide/configurationreference.html#io.micronaut.http.server.netty.types.files.FileTypeHandlerConfiguration$CacheControlConfiguration

Load Values from configuration File using spring

I am looking for a clean way to load values from properties file using the environment value passed from the command line (maven project). I then plan to use the loaded values in Java cucumber test.
Usage : mvn test -Denvironment=staging (it then loads the values from the staging file i.e. application-staging.properties, etc).
I know spring boot offers profiles, wondering if I can do this use a clean approach using the generic spring framework (annotations).
Thanks #racraman. I am able to get this to work using your suggestion around PropertySource. In order to get different environment working i've used something like :
#PropertySource({"classpath:${env}.properties"}).
Cheers !!!

Categories

Resources