Log SQL queries executed by Spring Batch - spring

I managed to log prepared statements executed by Spring Batch using:
logging.level.org.springframework.jdbc=DEBUG
But this won't show regular statements given to a JdbcCursorItemReader.
I tried adding:
logging.level.java.sql=DEBUG
logging.level.java.sql.Connection=DEBUG
logging.level.java.sql.Statement=DEBUG
logging.level.java.sql.PreparedStatement=DEBUG
logging.level.java.sql.ResultSet=DEBUG
But this didn' t do the trick.

Spring Batch uses JDBC, so adding the configuration below in your configuration enables SQL logging:
logging:
level
org.springframework.jdbc.core.JdbcTemplate: debug
And this is a sample output:
2021-01-19 14:51:37.570 DEBUG 14792 --- [ main] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL query
2021-01-19 14:51:37.571 DEBUG 14792 --- [ main] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL statement [SELECT VERSION FROM BATCH_JOB_EXECUTION WHERE JOB_EXECUTION_ID=?]

Package for JdbcCursorItemReader is org.springframework.batch.item.database.
Change your conf accordingly:
logging.level.org.springframework.batch.item.database=DEBUG

Related

How can I override the JPA properties for multiple datasources for Integration tests?

I have sucessfully configured two datasources for two different databases and schemas in my Spring Boot application. Now, for the integration tests I want to use an embedded database (HSQL) and execute the tests there. I tried overriding the properties using the following file (/src/test/resources/application-test.properties)
eot.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
eot.datasource.username=sa
eot.datasource.password=sa
eot.datasource.url=jdbc:hsqldb:mem:test;DB_CLOSE_DELAY=-1
eot.datasource.hikari.pool-name=ptest-eot
eot.datasource.jpa.show-sql=true
eot.datasource.jpa.generate-ddl=true
eot.datasource.jpa.database-platform=org.hibernate.dialect.HSQLDialect
eot.datasource.jpa.properties.hibernate.ddl-auto=create
eot.datasource.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialect
info.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
info.datasource.username=sa
info.datasource.password=sa
info.datasource.url=jdbc:hsqldb:mem:test;DB_CLOSE_DELAY=-1
info.datasource.hikari.pool-name=ptest-info
info.datasource.jpa.show-sql=true
info.datasource.jpa.generate-ddl=true
info.datasource.jpa.database-platform=org.hibernate.dialect.HSQLDialect
info.datasource.jpa.properties.hibernate.ddl-auto=create
info.datasource.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialect
In the log I can see it picks up some of the properties. In my application.yml the pool's name is 'eot-pool' and when I run the tests it shows correctly as 'ptest-eot'.
2022-05-27 17:09:03.855 INFO 6592 --- [ Test worker] mx.com.gnp.crm.adfe.EotJpaConfiguration : org.springframework.boot.autoconfigure.jdbc.DataSourceProperties#18a1fd92
2022-05-27 17:09:04.039 INFO 6592 --- [ Test worker] com.zaxxer.hikari.HikariDataSource : ptest-eot - Starting...
2022-05-27 17:09:04.845 INFO 6592 --- [ Test worker] com.zaxxer.hikari.pool.PoolBase : ptest-eot - Driver does not support get/set network timeout for connections. (característica no soportada)
2022-05-27 17:09:04.851 INFO 6592 --- [ Test worker] com.zaxxer.hikari.HikariDataSource : ptest-eot - Start completed.
But it's not overriding the JPA properties. It's not creating the schemas, tables, nor printing the SQL statements to the log.
I tried removing the 'properties' part. Using for example:
info.datasource.jpa.show-sql=true
info.datasource.jpa.hibernate.ddl-auto=create
info.datasource.jpa.hibernate.dialect=org.hibernate.dialect.HSQLDialect
But the JPA properties aren't being replaced.
When I run the tests, the log shows the wrong dialect (from the main application.yml file).
HHH000400: Using dialect: org.hibernate.dialect.DB2400Dialect
And when the tests runs:
SQL Error: -5501, SQLState: 42501
Because the schema and the table don't exist.
How can I override the JPA properties for integration tests when I have multiple datasources?
It seems you have a general application.yml file and a specific application-test.properties file for the test environment. I'm not sure if you can mix those file extensions, probably not. Either you choose to use .yml or .properties for both. Try to change the file name to application-test.yml. Also, to activate this specific test environment it's necessary to put this config in application.yml:
spring:
profiles:
active:test

How to disable spring batch console logging?

I'm using sprig batch with spring boot (2.5.5). How do I disable console logging created by batch jobs and steps? Is there any configuration prorerty to directly disable this
INFO 1 --- [ scheduling-1] o.s.b.c.l.support.SimpleJobOperator : Locating parameters for next instance of job with name=myJob
INFO 1 --- [ scheduling-1] o.s.b.c.l.support.SimpleJobOperator : Attempting to launch job with name=myJob and parameters={run.id=3970, now=2021-10-05T15:30:02.154224900Z}
INFO 1 --- [cTaskExecutor-3] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=myJob]] launched with the following parameters: [{run.id=3970, now=2021-10-05T15:30:02.154224900Z}]
You can manage this by changing the log level to ERROR OR DEBUG
org.springframework.batch.level=ERROR

Spring / IntelliJ doesn't clear session data on stop

I ran in a weird situation I never had before.
I've got a spring boot application running on intelliJ on ubuntu 18.
My spring boot application runs in a dev environment connected to a h2 in memory database. I have a session based login with spring security.
The weird thing is if after I stopped and restarted the application I am still logged in. And even if I completely restart IntelliJ the session is not cleared. Even the database keeps its state and is not getting dropped.
Only if I restart the whole system everything will be cleared.
Inside my application.properties there are no special properties set.
Everything's on default.
If I stop the application the following console output comes up:
2018-12-22 11:11:19.385 INFO 10000 --- [ Thread-35] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#73abcbfa: startup date [Sat Dec 22 11:11:00 CET 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext#12e6f48a
2018-12-22 11:11:19.391 INFO 10000 --- [ Thread-35] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2018-12-22 11:11:19.391 INFO 10000 --- [ Thread-35] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans
2018-12-22 11:11:19.395 INFO 10000 --- [ Thread-35] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2018-12-22 11:11:19.395 INFO 10000 --- [ Thread-35] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed drop of schema as part of SessionFactory shut-down'
2018-12-22 11:11:19.415 WARN 10000 --- [ Thread-35] o.s.b.f.support.DisposableBeanAdapter : Invocation of destroy method failed on bean with name 'inMemoryDatabaseShutdownExecutor': org.h2.jdbc.JdbcSQLException:Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-197]
2018-12-22 11:11:19.417 INFO 10000 --- [ Thread-35] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2018-12-22 11:11:19.419 INFO 10000 --- [ Thread-35] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
https://github.com/spring-projects/spring-boot/wiki/spring-boot-1.3.0-m3-release-notes
Update Tomcat, Jetty and Undertow to serialize session data when the
application is stopped and load it again when the application
restarts. Persistent session are opt-in; either by setting
persistentSession on the ConfigurableEmbeddedServletContainer or by
using the property server.session.persistent=true (Persistent sessions
are enabled by default with Devtools).
In spring boot 2, the property is:
server.servlet.session.persistent=false # Whether to persist session data between restarts.
I suppose you are talking about web login via browser (or REST-client). Also I suppose you're using default web security settings.
Then your login request creates a cookie with session id which is returned to browser and is being used in next requests until you close browser.

Spring boot 2.1.0 Hikari CP bad password

So I updated my Spring Boot to 2.1.0, and now hikari is the default CP. According to some SO questions related to this I no longer needed to use the .hikari in my application.properties file. My properties file now looks like this:
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:~/deployHistory/deployHistory
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
Now if I use the driver manager directly, like this:
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection(databaseUrl);
Statement stat = conn.createStatement();
stat.execute("create table ...)");
stat.close();
conn.close();
It all works fine, However, when I use the Spring Boot JDBC template, and do a simple:
jdbcTemplate.update(...);
I get an error:
2018-11-26 14:27:54.772 INFO 7349 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-11-26 14:27:56.059 ERROR 7349 --- [nio-8080-exec-1] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
org.h2.jdbc.JdbcSQLException: Wrong user name or password [28000-197]
What is the way to fix this? All the pre-2.0.4 answers don't seem to work.

Import schema.sql into a specificic H2 database with Spring Boot

I'm struggling to understand if I can import the data defined inside my schema.sql and data.sql into a specific H2 database.
I use Spring Boot 2.0.6 and this is the configuration inside my application.properties:
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:mem:assignment
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
And I correctly placed both schema.sql and data.sql inside the resources folder.
The application starts up correctly and imports the files:
2018-10-26 20:39:24.834 INFO 9448 --- [main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from URL [file:/C:/Users/Gabriele/code/java/assignment/target/classes/schema.sql]
2018-10-26 20:39:24.848 INFO 9448 --- [main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from URL [file:/C:/Users/Gabriele/code/java/assignment/target/classes/schema.sql] in 13 ms.
2018-10-26 20:39:24.850 INFO 9448 --- [main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from URL [file:/C:/Users/Gabriele/code/java/assignment/target/classes/data.sql]
2018-10-26 20:39:24.868 INFO 9448 --- [main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from URL [file:/C:/Users/Gabriele/code/java/assignment/target/classes/data.sql] in 18 ms.
But when I open the browser console, the assignment database is empty
while the test one is not.
What am I missing?

Resources