Can't run my Quarkus app after adding JPA - java

I'm trying to learn Quarkus, but after adding a JPA dependency the app doesn't initialize anymore.
This is the added dependency:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
Following are the errors I'm having:
[org.tes.uti.TestcontainersConfiguration] (build-47) Attempted to read Testcontainers configuration file at file:/home/fhb/.testcontainers.properties but the file was not found. Exception message: FileNotFoundException: /home/fhb/.testcontainers.properties (No such file or directory)
After that Quarkus keeps on and gets the following error:
Caused by: java.lang.RuntimeException: io.quarkus.runtime.configuration.ConfigurationException: Model classes are defined for the default persistence unit <default> but configured datasource <default> not found: the default EntityManagerFactory will not be created. To solve this, configure the default datasource. Refer to https://quarkus.io/guides/datasource for guidance.
This is my application.properties file:
quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=postgres
quarkus.datasource.password=admin
quarkus.datasource..jdbc.url=jdbc:postgresql://localhost:5432/quarkus-social
quarkus.datasource.jdbc.max-size=16
I think that Quarkus is trying to run tests and for that it needs the .testcontainers.properties file, which I've never created. Anyways I don't want to create that file in /home/fhb/, so theres a way to specify that file location?
Besides thatI would like to know if Testcontainers has something to do with unit tests, which I would like to add to my quarkus application.
Thanks in advance for your help.

I guess the problem is a small typo.
Change from
quarkus.datasource..jdbc.url=jdbc:postgresql://localhost:5432/quarkus-social
To
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/quarkus-social
If you don't specify the URL of the database and run in dev or test mode, Quarkus uses test containers to start one database for you.
There are tutorials on quarkus.io/guides/datasource.
About tests, you can use test containers or one in memory database as H2. You can find all this on Quarkus guides.

Related

Logging system failed to initialize using configuration from 'classpath:logger/logback-spring.xml'

I use Spring boot 2.3.0.RELEASE.
application.properties
spring.profiles.active=dev
logging.config=classpath:logger/logback-spring.xml
logging.file.dir=reception-electronic-docs
logging.file.name.var=reception-electronic-docs.log
logging.file.archive.format.name=reception-electronic-docs.%d{dd-MM-yyyy}.log
Previously this code worked (Srping boot 2.2.5.RELEASE).
I use multi module structure in the project. But there, the Central pom does not manage the entire project. I create microservices.
During start up an application encounter an error:
Logging system failed to initialize using configuration from 'classpath:logger/logback-spring.xml'
java.io.FileNotFoundException: class path resource [logger/logback-spring.xml] cannot be resolved to URL because it does not exist
at org.springframework.util.ResourceUtils.getURL(ResourceUtils.java:137)
What are any ideas to correct this?
I add an dependencies
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.2</version>
</dependency>
But I don't understand reason.
After I removed the dependency. But my app runs without errors. I'm at a loss.

Spring boot external config in a docker environment: application.yml does not exist

Before start explaining my issue, it's worth mentioning that although I am trying to work how to use Maven Jib plugin in conjunction with Spring Boot and Kubernetes, the issue is the same even if I try to use a normal docker.
I have used Kubernetes configMap to create the application.yml file and mount it as an external file to the Pod (/config/application.yml). The issue I have been facing is my application does not able to find the application.yml file and throw a typical exception:
ERROR [main] org.springframework.boot.SpringApplication: Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.example.Application]; nested exception is java.io.FileNotFoundException: class path resource [application.yml] cannot be opened because it does not exist
I have tried different approaches with Jib by using different arguments such as below.
<container>
<extraClasspath>/config/*</extraClasspath>
<args>
<arg>--spring.config.location=file:/config/application.yml</arg>
</args>
<jvmFlags>
<jvmFlag>-Dspring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:/config/</jvmFlag>
</jvmFlags>
</container>
None of them has worked for me. I have also tried to use the SPRING_CONFIG_NAME environment variable and set it to file:/config/application.yml for the pod, but still the same issue.
I can verify that the config file exists in the specified location and there are no permission issues (as far as I can tell).
Interestingly, when I create just an empty application.yml file in the default classpath (src/main/resources) then it passes the first verification and the application loads successfully (with using the actual values from /config/application), so whatever the issue is it is being impacted by an initial verification of Spring Boot before even passing the application file to the corresponding classes.
It turns out that the only item I need to have is the following:
<container>
<extraClasspath>/config</extraClasspath> <!-- No need to have * at the end -->
</container>

Flyway can't find classpath:db/migrations

I just started right now a new project in Intellij using Spring Boot ver 2.1.3 and Flyway 5.2.4 with Java 11.
After try to start my project i got :
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is java.lang.IllegalStateException: Cannot find migrations location in: [classpath:db/migration] (please add migrations or check your Flyway configuration)
I have the following folders:
As you can see i have "db/migration" but without any migration, i just started right now. Debugging the class FlywayAutoConfiguration i got the following:
So, i tried to return all files in "classpath:", see:
Note that i just have "application.properties" file.
It is not that much useful or accurate answer.
But This issue make you frustrated so that i give this solution.
Note: Strange but it's true, Sometime it's not allow copy paste because your folder created db.migration and it expact db->migration(It's not same in this scenario). So whenever you start from scratch. Go to the resource folder -> Create DB folder -> Create migration folder -> Create database file with Version_SubVersion__Name(As defined below).
Normally this happens in following cases,
Path is not proper try using set locations param value.
db.migrate folder not contain any file.
Check name of file : V1_1__(short_desc)
Try to run using, mvn compile flyway:migrate
In my case i already place sql file over there but still it gives same error,
Basically i place this sql file using copy paste from somewhere.
When i try to add one new file on same place using IDE (Intellij : Right click on migration folder -> new -> Flyway migration -> versioned migration), then it ask me(warning) about some delicate allowance(normally we mention in database configuration i also place there still), and it start working.
Flyway requires at-least one script, disable it until u need it by using following command in application.properties file
spring.flyway.enabled=false
I believe that Flyway requires at least one migration script to initialize. Try adding a simple sql creation script into your migration folder and give it another try. Alternatively you can disable the flyway dependency until you need it.
I had a similar error, and solved it as follows: I added these commands
spring.flyway.baselineOnMigrate=true
spring.flyway.check-location=true
spring.flyway.locations=classpath:db/migration
spring.flyway.schemas=public
spring.flyway.enabled=true
to application.properties
I had the same issue. When I created the directory, I simply typed db.migration - the same way one would do with package names. InteliJ will display both db.migration and db/migration directories as db.migration, so while it may look correct in IntelliJ, flyway requires the latter.
Even when having your migration files in the db/migration folder, flyway won't detect it.
Then you will have to fix this by explicitly setting the locations in your application.properties (or appliocation.yml) by adding:
spring.flyway.locations=classpath:db/migration
N.B: Also you need to have at least one script to initialize flyway, you can even put an empty one. But you need to have at least one script
As #Guy suggested in his answer, I created an SQL file in the migration folder and left it empty. Named v1_0__mock_flyway.sql in the migration folder classpath:db/migration. Error solved.
If you have tried everything above and it still does not apply the migrations.
Be sure you have followed the steps in the previous answers
A db.migration folder exists in your resources folder
The sql scripts are named with respect to the correct naming convention, such as V1__init.sql
The following config exists in your application.properties/application.yml
spring.flyway.locations=classpath:db/migration
Do a clean build
-> mvn clean
Then restart your spring boot app, this worked for me.

Flyway Init Order

I've got a Java Spring Boot application, with Flyway configured as a dependency in my Maven pom.xml (I have a parent pom and a project pom... Flyway is defined in my project pom).
<dependencies>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>4.2.0</version>
</dependency>
...
and just a couple of entries in application.properties:
flyway.enabled=true
flyway.out-of-order=true
I can run a maven task to get Flyway to run migrate to create/update my database and then run my application against that, but I'm having trouble getting it to call migrate at the right time just by running my application (which is obviously important in prod). It looks like all of my Spring classes are being instantiated first, some of which involves looking at the database, and then Flyway migration happens after, so for instance if you run the application against an empty database the application crashes when trying to access anything in the database.
Any tips on where to look to see where I'm going wrong to get Flyway to do its migration earlier in the start-up process of my Spring Boot application?
I am not sure how your data source configuration looks like, but you could declare your JPA config in such a way that makes it dependent on the flyway migration.
You could declare a #DependsOn("flyway") annotation on whatever #Config class or datasource bean, "flyway" being the declared name of your flyway configuration bean. Then, on your flyway configuration bean, qualify the bean annotation with an initMethod property like the following: #Bean(initMethod = "migrate").
Try change "flyway.out-of-order=false"
I would suggest you to try using event listners like ApplicationStartedEvent.
#EventListener
public void migrate(ApplicationStartedEvent applicationStartedEvent) {
//do some checks
flyway.migrate();
}

How to run Spring 3.0 PetClinic in tomcat with Hibernate backed JPA

OK, this probably is supposed to be the easiest thing in the world, but I've been trying for the entire day, and it's still not working.. Any help is highly appreciated!
EDIT: For the correct procedure, please see Pascal's answer.
My wrong (since I did not disabled LoadTimeWeaving) procedure is left for reference..:
What I did:
Downloaded Tomcat 6.0.26 & Spring 3.0.1
Downloaded PetClinic from https://src.springframework.org/svn/spring-samples/petclinic
Built & deployed petclinic.war. Ran fine with default JDBC persistence.
Edited webapps/WEB-INF/spring/applicationContext-jpa.xml and set jpaVendorAdaptor to Hibernate.
Edited webapps/WEB-INF/web.xml and changed context-param from applicationContext-jdbc.xml to applicationContext-jpa.xml
Copied everything in the Spring 3.0.1 distribution to TOMCAT_HOME/lib.
Launched tomcat. Saw
Caused by: java.lang.IllegalStateException: ClassLoader [org.apache.catalina.loader.WebappClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:spring-agent.jar
Uncommented line <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/> in webapps/META-INF/context.xml.
Same error. Added that line to TOMCAT_HOME/context.xml
Deployed without error. However, when I do something it will issue an error saying
java.lang.NoClassDefFoundError: javax/transaction/SystemException at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:39)
11.Changed scope of javax.transaction from test to default (just deleted test), as suggested by scaffman.
12.Runs fine!! Thank you!
Ok, here is what I did:
Get Tomcat 6.0.26
Checkout the petclinic sample:
svn co https://src.springframework.org/svn/spring-samples/petclinic/trunk/ petclinic
cd into the petclinic directory
Modify src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml to use Hibernate:
COMMENT the <context:load-time-weaver> (load-time weaver SHOULD NOT be used with Hibernate, this is for Toplink)
Declare Hibernate as jpaVendorAdapter (comment the Toplink part, uncomment the Hibernate part)
Modify the src/main/webapp/WEB-INF/web.xml to use the applicationContext-jpa.xml
Modify the pom.xml to bundle jta.jar in the war (as pointed out by #skaffman):
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>com.springsource.javax.transaction</artifactId>
<version>1.1.0</version>
<!--scope>test</scope-->
</dependency>
Build the war
mvn install
Deploy it to Tomcat
cp target/petclinic.war $TOMCAT_HOME/webapps
Browse
http://localhost:8080/petclinic
Looks like a problem with PetClinic packaging:
http://forum.springsource.org/showthread.php?t=85042
and
http://jira.springframework.org/browse/SPR-6880
There's fix in there for the pom.xml

Categories

Resources