There is spring boot application with h2 database which is used as primary database. Also there is a resource/schema.sql wich is loaded at startup by spring boot.
But during integration tests with #SpringBootTest spring boot does not load this schema.sql. Instead it requires to setup embeded database while there is h2 db already.
Is there a way to execute schema.sql without embeded datasource configuration? And do it only once for all tests (e.g. using #Sql for schema creation for all test is not a solution)?
Annotate your class or method with #Sql(scripts = "classpath:schema.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) where your schema.sql is on resources folder.
You need this if your tests are marked as integration tests. Spring boot will do this for you if you are running a unit test or just compiling with unit tests enabled which is the default.
Set this in your properties file and then rename schema.sql to schema-test.sql
spring.datasource.platform=test
Spring boot automatically configures the embedded database for you as long as you have the it in the classpath (h2, hsqldb or derby)
Related
I have quite large legacy multi module Java Spring boot project which connects to one MySql database and two Mongo databases (lets say codebook and report).
I'm trying to setup Mongock to be able to do Mongo database migrations. I need it on only one Mongodb database (report).
I added Mongock dependencies and configuration class (MongockApplicationRunner, MongoTransactionManager) to report database.
When I run the application it gives me exception: Scan package for changeLogs is not set: use appropriate setter (exception from RunnerBuilderBase class).
I debugged it and found that Mongock is trying to set configuration for codebook database.
I created another Mongock configuration, this time for codebook database. Run the application. Mongock loads my new configuration for codebook, but then tries to set another configuration for codebook database which failes because of config.getChangeLogsScanPackage().isEmpty().
It seems that Mongock is always setting some default configuration on codebook database.
When I debug it Mongock loads first codebook configuration from my mongockApplicationRunner (line .buildApplicationRunner();). The second codebook configuration comes from MongockContextBase.applicationRunner and that configuration has empty MongockSpringConfiguration.migrationScanPackage.
Why is this default configuration appearing and how to stop it from loading?
My Spring boot is 2.2.13.RELEASE and Mongock is 5.0.24.
After a lot of wasted time I found that besides Mongock configuration on report database I was also doing autoconfiguration (#EnableMongock).
I had annotation #EnableMongock on my MongockConfig class. Since codebook mongo template had #Primary annotation autocofiguration was loading that database and there were no properties to set changelog directory inside application.properties so ChangeLogsScanPackages were empty.
Simple solution, I just removed #EnableMongock annotation and it started working.
Don't mix autoconfiguration (#EnableMongock) with creating mongockApplicationRunner and transactionManager or you will end up with problem like me.
I have ,probably a basic question,related to unit testing a spring boot application that uses RestControllers,JPA repositories.Oracle is used in the backend.Now to unit test the JPA repositories using junit,mockito and probably DataJPA,do the tables that are present in the backend,need to be recreated in the H2 in-memory db?
In my spring boot application, there is a persistent context class for data source connectivity. While I write junit for this application, it always call this configuration class and fails when database connection is off. The junit didn't run if I exclude this configuration class. What is the solution to run the junit,even the database connectivity fails.
Hi I'm using Spring Boot version 1.5.9.
When using Spring Boot to initialize schema.sql for mysql database, it works all fine and the database schema is getting created successfully. But on restart of the application this schema.sql script is executing again and the application fails to start because the tables already exist.
I tried spring.jpa.hibernate.ddl-auto=create-drop option in application.properties but it does not have any effect (probably because it only works for Hibernate entities which I'm not using)
Is there a way to have Spring Boot to re-create schema from schema.sql every time on restart if the database is not in-memory one?
GitHub:
https://github.com/itisha/spring-batch-demo/tree/database-input
According to the documentation you can simply ignore exceptions by setting spring.datasource.continue-on-error property to true
Spring Boot enables the fail-fast feature of the Spring JDBC
initializer by default, so if the scripts cause exceptions the
application will fail to start. You can tune that using
spring.datasource.continue-on-error.
or even turn it off with spring.datasource.initialize set to false
You can also disable initialization by setting spring.datasource.initialize to false.
A workaround could be, to change the create statements in your schema.sql
from
CREATE TABLE test .....
to
CREATE TABLE IF NOT EXISTS test ...
use the IF NOT EXISTS statements
turn off automatic schema creation to avoid conflicts: add this line in your application.properties
spring.jpa.hibernate.ddl-auto=none
I'm trying to deploy My application on weblogic server, My application has an in-memory DB and since I use HSQLDB for JUNIT, I want to keep HSQL as my in-memory DB(primarily a slight better performance over derby). Since weblogic has already derby, when I try to deploy the application, The derby is getting started rather than HSQL. On Preliminary investigation I find that Since Derby is define above HSQL in EmbeddedDatabaseConnection.java, Derby database is being started first. Is there any specific configuration, where I can explicitly embedded database type to HSQL rather than allowing spring boot to start database based on library/classes
First try I can think of is to remove Derby from class path and leave there only HSQL. Spring Boot docs:
If HSQLDB is on your classpath, and you have not manually configured
any database connection beans, then we will auto-configure an
in-memory database.
If that's not an option, you can specify
connection type for Hibernate (JPA):
An embedded database is detected by looking at the Connection type: hsqldb, h2 and derby are embedded, the rest are not.
HSQL explicitly as primary data source:
#Configuration
public class DataSourceConfig {
#Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.build();
}
}
define spring.datasource.type property