Import schema.sql into a specificic H2 database with Spring Boot - 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?

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

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.

Log SQL queries executed by Spring Batch

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

Spring boot Embedded Derby not working in the latest version.

I have used Embedded derby earlier in my spring boot projects. But now when i created the project through Spring Initializr with the derby dependency. I get the below error :
Schema 'SA' does not exist
followed by org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
When i tried running the earlier project that i had created, the Derby is working just fine.
PFB the console for the earlier project :
2018-03-18 15:34:44.346 INFO 16560 --- [ restartedMain]
org.hibernate.cfg.Environment : HHH000021: Bytecode
provider name : javassist 2018-03-18 15:34:44.391 INFO 16560 --- [
restartedMain] o.hibernate.annotations.common.Version : HCANN000001:
Hibernate Commons Annotations {5.0.1.Final} 2018-03-18 15:34:44.490
**INFO 16560 --- [ restartedMain] org.hibernate.dialect.Dialect
: HHH000400: Using dialect: org.hibernate.dialect.DerbyDialect
2018-03-18 15:34:44.497 WARN 16560 --- [ restartedMain]
org.hibernate.dialect.DerbyDialect : HHH000430: The DerbyDialect
dialect has been deprecated; use one of the version-specific dialects
** instead 2018-03-18 15:34:45.094 INFO 16560 --- [ restartedMain]
org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl
schema export 2018-03-18 15:34:45.099 ERROR 16560 --- [
restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389:
Unsuccessful: drop table book 2018-03-18 15:34:45.099 ERROR 16560 ---
[ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : Schema
'SA' does not exist 2018-03-18 15:34:45.129 WARN 16560 --- [
restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning
Code: 10000, SQLState: 01J01 2018-03-18 15:34:45.129 WARN 16560 --- [
restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper : Database
'memory:testdb' not created, connection made to existing database
instead. 2018-03-18 15:34:45.129 INFO 16560 --- [ restartedMain]
org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export
complete 2018-03-18 15:34:45.152 INFO 16560 --- [ restartedMain]
j.LocalContainerEntityManagerFactoryBean : Initialized JPA
EntityManagerFactory for persistence unit 'default'
The Console Log for my current spring boot with embedded derby :
2018-03-18 15:42:23.234 INFO 11312 --- [ main]
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-03-18 15:42:23.237 WARN 11312 --- [ main]
**com.zaxxer.hikari.util.DriverDataSource : Registered driver with
driverClassName=org.apache.derby.jdbc.EmbeddedDriver was not found,
trying direct instantiation. 2018-03-18 15:42:23.844 INFO 11312 --- [
** main] com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver
does not support get/set network timeout for connections. (Feature not
implemented: No details.) 2018-03-18 15:42:23.847 INFO 11312 --- [
main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start
completed. 2018-03-18 15:42:23.937 INFO 11312 --- [ main]
j.LocalContainerEntityManagerFactoryBean : Building JPA container
EntityManagerFactory for persistence unit 'default' 2018-03-18
15:42:23.969 INFO 11312 --- [ main]
o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing
PersistenceUnitInfo [ name: default ...] 2018-03-18 15:42:24.136
INFO 11312 --- [ main] org.hibernate.Version
: HHH000412: Hibernate Core {5.2.14.Final} 2018-03-18 15:42:24.138
INFO 11312 --- [ main] org.hibernate.cfg.Environment
: HHH000206: hibernate.properties not found 2018-03-18 15:42:24.199
INFO 11312 --- [ main]
o.hibernate.annotations.common.Version : HCANN000001: Hibernate
Commons Annotations {5.0.1.Final} 2018-03-18 15:42:24.380 INFO 11312
--- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.DerbyTenSevenDialect
2018-03-18 15:42:25.572 WARN 11312 --- [ main]
o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget
encountered exception accepting command : Error executing DDL via JDBC
Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error
executing DDL via JDBC Statement at
org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
~[hibernate-core-5.2.14.Final.jar:5.2.14.Final] at
org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375)
[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
The difference i can find between the two is that in the current log(abv log) it says that the
com.zaxxer.hikari.util.DriverDataSource : Registered driver with
driverClassName=org.apache.derby.jdbc.EmbeddedDriver was not found,
trying direct instantiation.
Let me know if we need to configure something apart from the below dependency for enabling this derby. Note- I dint do anything apart from maven dependency in the previous project.
Maven dependency :
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
Please configure you JPA Configuration as per your requirement. I've configured as below. You need to add below configuration in application.properties file.
# PROFILES
spring.profiles.active=dev
# JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration)
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=default
spring.jpa.show-sql=true
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.continue-on-error=false
spring.datasource.generate-unique-name=false
add
spring.jpa.hibernate.ddl-auto=update
to your application.properties
it is by default create-drop.
The default driver used by derby is org.apache.derby.jdbc.AutoloadedDriver, but springboot and other framework choice org.apache.derby.jdbc.EmbeddedDriver.
It causes the driver to be unable to find the driver when using the datasource for the first time.
About Schema 'SA' does not exist.
I think that you use the other tool connect the derby, such as ij. And the jdbc url they use is the same. It looks like the same database connected, but it's not actually.
This is the key to the problem.
Added this in appplication.properties and worked
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=default
spring.jpa.show-sql=true
I've added below property in application.properties, it worked!
spring.jpa.hibernate.ddl-auto=update

Resources