Flyway Invocation of init method failed - spring

I'm trying to set up Flyway to work with Spring JPA. I'm stuck at:
Error creating bean with name 'org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration': Invocation of init method failed; nested exception is java.lang.IllegalArgu: name
Stack trace isn't giving me much more to work with. I have a previous database where many entities have a name column. I'm guessing Flyway is having some trouble extracting the existing database schema? I looked trough Spring, Spring Boot, and Flyway docs, but couldn't find anything for this.
I have a PostgreSQL database. Here are the related dependencies from my pom.xml:
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.2-1002.jdbc4</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>4.0.3</version>
</dependency>
Here is my Flyway configuration in application.properties:
flyway.baseline-version=1
flyway.baseline-on-migrate=true
flyway.check-location=true
flyway.locations=${MIGPATH}
I think locations is correct, because I had a mistake with it earlier and after fixing that, the error message changed. It's a path to a folder with 1 file called V1__f.sql - here are its contents:
ALTER TABLE category ADD hidden boolean NOT NULL;
ALTER TABLE expense ADD details VARCHAR(256);
Since the error was related to name and my migrations don't concern that field, I'm guessing this error is not caused by the contents of the migration file. In any case I believe the contents of the migration file is correct (my previously existing database + those 2 changes should be compatible with the schema implicitly defined by my JPA entity definitions).

Related

Start spring-boot project without datasource configuration on application.properties

I have a spring-boot project where the database configuration should be saved in the first time the user execute the application, after ask for the connection data (server url, username and password). the application.properties file embbed in the WAR file of the application should be something like that:
security.basic.enabled=false
# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
# Multipart Configuration
spring.servlet.multipart.maxFileSize = 150MB
spring.servlet.multipart.maxRequestSize = 150MB
spring.servlet.multipart.fileSizeThreshold = 5MB
# Setting max size of post requests to 6MB (default: 2MB)
server.tomcat.max-http-post-size=157286400
After the first run, a second application.properties should be created with the database configuration. But right now, when I try run the project with the configuration above, it do not start, showing the following error message:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
without give the application the change to generate the second configuration file, with the database properties.
How I could force the application to run this first time, even without the database stuff?
update
Ok, then I get rid of this error message by keeping this 2 dependencies on my pom.xml:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.4.1</version>
</dependency>
and now the applications runs without error, and I am able to open the installation page to create the database on the postgresql server and import the initial data (user data included).
But I notice that this initial data is not being stored on the postgresql server (despite the datababse and all the tables are being created normally). I suspect they are being created on the hyperSql database on memory, what do not help me at all.
My guess is that's happen because I autowire some Dao classes for this entities on my InstallService class, and the code only create the data on the postgresql where I directly make the connection with this specific server.
Considering the method responsible by populate de initial data on my database is something like that:
public void criarUsuario(String user, String pass, String nome, String sobrenome, String email) {
Usuario novo = new Usuario();
novo.setUsername(user);
novo.setPassword(pass);
novo.setFirstName(nome);
novo.setLastName(sobrenome);
novo.setEmail(email);
novo.setEnabled(true);
novo.setLocked(false);
novo.setCredenciais(new ArrayList<Credencial>());
novo.getCredenciais().add( credencialDao.findBy("nome", "admin") );
usuarioDao.insert(novo);
...
}
What I could change here to persist this data on the postgresql database, instead of the hyperSql database?
update 2
Now I managed to run the application even with only the postgresql dependency om my pom.xml. I did that by adding a initial configuration on my first application.properties (locatd on /src/main/resources):
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/appdata
spring.datasource.username=...
spring.datasource.password=..
spring.datasource.continue-on-error=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.generate-ddl=false
But when I try access the application on the browser, I got an error saying the database not exist (of course not, when I run the application the first time I have a specific flow to create database, tables and populate initial data).
is there any way to supress this "database not exist" error when I run the project?

spring-cloud-azure-feature-management with application.yaml

I am attempting to implement a quick feature flag solution for my teams that run both .net core and java spring boot. I was able to easily get a solution running with .net core using feature-management and was excited to see the spring equivalent because I was hoping for essentially the same tooling for all teams.However, I am having a problem because I just want to start by providing the flag values in the application/bootstrap.yaml and it keeps yelling at me with the following error.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.cloud.azure.appconfiguration-com.microsoft.azure.spring.cloud.config.AppConfigurationProperties': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one config store has to be configured.
Caused by: java.lang.IllegalArgumentException: At least one config store has to be configured.
So, my question is, how do I specify a provider for a local file rather than spring cloud config? I do not have that implemented and it will take too long for me to do so in my environment.
I found my problem! I had an extra dependency that was not needed for what I was doing. Once I removed the following it worked fine.
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>spring-cloud-azure-appconfiguration-config</artifactId>
<version>1.2.2</version>
</dependency>

Using ServiceTasks with TimerCatchingEvents in activiti bpmn workflow behaving inconsistently

When using ServiceTasks with IntermediateTimerEvents sometimes it did not execute the service task due to exception raised in ACT_RU_JOB table. EXCEPTION_MSG_ column in ACT_RU_JOB table is populated with Unknown property used in expression: #{serviceTask1.execute(execution). value for that particular job.
Please refer the activiti workflow image i have attached below for clear picture of the usecase.
NOTE: I am using spring-boot microservice using maven to run the workflow.
spring-boot version: 1.5.5.RELEASE
activity version: 5.22.0
Activity and Connection Pool dependencies in pom.xml:
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>${activiti.version}</version>
</dependency>
Connection Pooling dependency:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
application.properties configuration attached below:
spring.activiti.job-executor-activate=false
spring.activiti.async-executor-enabled=true
spring.activiti.async-executor-activate=true
spring.datasource.tomcat.initial-size=15
spring.datasource.tomcat.max-active=100
ANALYSIS: The error i am getting above is not consistent.
activiti workflow
Project link on Github:
https://github.com/cloudnativeview/spring-activiti-bpmn-kickstart
The exception is totally expected because you don't set the appropriate process variable when starting your process instance. Also Activiti engine is not able to resolve named bean serviceTask1.
From the Activiti 5.22.0 userguide docs (inside the file activiti-5.22.0.zip), more precisely section 8.5.3. Java Service Task you have 4 ways of declaring how to invoke Java logic.
Specifying a class that implements JavaDelegate or ActivityBehavior.
Evaluating an expression that resolves to a delegation object.
Invoking a method expression.
Evaluating a value expression.
In your BPMN process definition file you are invoking service task Java logic with method expression technique. If you want to successfully invoke your method you have to make sure your bean serviceTask1 is a process variable from your execution or a named object in Spring context.
Unfortunately you don't provide necessary information to your BPM engine on where to find your bean with Java logic.
I suggest you the following steps.
Read the technical and userguide documentation (chapter 8.5.3. Java Service Task), from the provided links.
Try to invoke Java logic with the way of specifying a class that implements JavaDelegate or ActivityBehavior. In my humble opinion it is far more easier to get the first touch with Activiti BPM engine.

How use java.time.LocalDate using Hibernate5 and Vaadin8?

I'm new on Vaadin and I'm trying to do a simple crud application.
I was starting from Vaadin CRUD sample; then I modify the backend project to use Hibernate and the ui project to manage the various tables.
I can do that for simple data type like varchar and int, now I would add a date's field but it returns me this error:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x07\x03\x00\x00\x07\xE1\x06\x1Ax' for column 'data_nascita' at row 1
"DATA_NASCITA" is a date column on my mysql DB and the entity has a LocalDate linked to this field.
I found somewhere that this new java api is compatible only with Hibernate5 so in my pom file I add:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.1.0.Final</version>
</dependency>
and into the entity class I replace java.util.Date with java.time.LocalDate and delete the annotation #temporal.
The input class for this field is a DateField of Vaadin framework.
Which could be the problem?
P.S.: I'm blind, so I'm compiling the code with Netbeans8 but I modify the code with notepad++ so, if someone use the same technologies and hasn't any problem please tell me because I can't be sure that the problem is something with build process.
IF your hibernate version is greater than 5.2.+ you can just using column as below:
#Column
private LocalDate date;
Otherwise you need install hibernate-java8 module into your project.
On the other hand, you shouldn't install hibernate-java8 module into your project if the hibernate version >= 5.2, maybe it will makes some conflict, since the hibernate-java8 module is merged into hibernate.

Spring Boot. #DataJpaTest H2 embedded database create schema

I have couple of entities in my data layer stored in particular schema. For example:
#Entity
#Table(name = "FOO", schema = "DUMMY")
public class Foo {}
I'm trying to setup H2 embedded database for integration testing of my data layer.
I'm using #DataJpaTest annotation for my tests to get H2 embedded database configured automatically. However, the creation of tables fails because schema DUMMY is not created at DB initialization.
Any ideas on how to create schema before creation of tables in test cases?
I've tried to use #Sql(statements="CREATE SCHEMA IF NOT EXISTS DUMMY") but didn't succeed.
Also, I've tried to set spring.datasource.url = jdbc:h2:mem:test;INIT=CREATE SCHEMA IF NOT EXISTS DUMMY in my test.properties file together with TestPropertySource("classpath:test.properties"), but that didn't work too.
I had the same issue, I managed to resolve by creating schema.sql (in resources folder) with the content
CREATE SCHEMA IF NOT EXISTS <yourschema>
Documentation can be found here but imho the lack of real examples make it very complex.
Warning: this script is also executed within the normal (not test) environment.
Not mandatory, but good practice, add h2 dependency only in test scope
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
I think you are looking for this annotation:
#AutoConfigureTestDatabase(replace=Replace.NONE)
example:
#DataJpaTest
#AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
class UserRepoTest {...}
After couple hours of struggling I've found a workaround.
You can define spring.jpa.properties.hibernate.default_schema = DUMMY in your application.properties.
And then set spring.jpa.properties.hibernate.default_schema = in your test.properties and use together with #TestPropertySource("classpath:test.properties")
So, in this way the schema DUMMY won't be created and the entities will be created in default schema.
In my case schema.sql under test/resources din't worked.
The following configuration in test/resources/application.yml file worked.
spring:
datasource:
username: sa
password: sa
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:usrmgmt;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL;INIT=CREATE SCHEMA IF NOT EXISTS DUMMY;
liquibase:
change-log: classpath:db/changelog/db.changelog-master.xml
In the above configuration, provided the below additional configuration
INIT=CREATE SCHEMA IF NOT EXISTS DUMMY extension to the existing DB url. In the absence of this, faced the exception Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "DUMMY" not found;.
spring.liquibase.change-log property. In the absence of this, faced the exception Caused by: liquibase.exception.ChangeLogParseException: classpath:/db/changelog/db.changelog-master.yaml does not exist.

Resources