Spring/Tomcat JDBC initialization - java

I have a Spring REST application deployed to a Tomcat server. I had placed a schema.sql file inside the resources folder that I use to recreate database on local test/development scenarios.
However, when Tomcat starts up, it seemed to run this script against the database.
In the logs, I have the following entry for this:
2016-01-26 17:30:33.543 INFO 2105 --- [ost-startStop-1] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from URL [file:/usr/share/tomcat8/apache-tomcat-8.0.30/webapps/xxxx/WEB-INF/classes/schema.sql]
I find it a bit strange and wonder why does this happen.
Can anybody provide insights into this?
I tried googling as usual, and could not find any satisfactory results.

See here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc
Spring JDBC has a DataSource initializer feature. Spring Boot enables it by default and loads SQL from the standard locations schema.sql and data.sql (in the root of the classpath).

Related

spring.datasource.platform is not working

I made a project from scratch just with the schema.sql and the data.sql just to try the schema.sql and the data.sql:
https://github.com/rmmcosta/TestSchema
Everything works fine. The table inside schema.sql is created in a MySql database (previously created and the grants were given to the user defined in application.properties) and the data.sql populates the data as it's supposed to do.
But, when I change schema.sql and data.sql to schema-mysql.sql and data-mysql.sql and I put in the application.properties the property spring.datasource.platform=mysql the schema-mysql.sql and the data-mysql.sql are not being executed.
No errors are being thrown, simple nothing happens on the database.
I tried with spring boot 2.2.4 and it works fine, but with spring boot 2.7.5 it isn't working.
Do you know if the spring.datasource.platform was deprecated? And if so, do you know how can I set the application.properties in order to run schema-mysql.sql?
Thank you in advance,
Ricardo
Note:
I tried without using spring.datasource.platform=mysql and with schema.sql and data.sql and everything works fine.
I tried with an old project, spring boot 2.2.4 and java 1.8, and works fine.
Do you know if the spring.datasource.platform was deprecated? And if so, do you know how can I set the application.properties in order to run schema-mysql.sql?
The property name changed to spring.sql.init.platform
https://github.com/spring-projects/spring-boot/blob/d870474fcd4899fac94d51311c4163832d6b109d/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json#L1148
Which occurred in Spring Boot 2.5: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.5.0-RC1-Configuration-Changelog
You need to use spring.sql.init.platform instead of spring.datasource.platform property in application.properties.
Please see the comment from latest documentation -
In addition, Spring Boot processes the schema-${platform}.sql and
data-${platform}.sql files (if present), where platform is the value
of spring.sql.init.platform

ERROR: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured

I've seen the other related posts for this topic, with none having an explanation for my problem unless I'm missing something. So, I'm developing a spring boot web app with 2 other developers. They are able to run the app locally, generate a war file, and connect to their local databases with no issues. However, I for some reason am not able to due to the following error report when I run the war file:
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).
I am able to run a ./gradlew clean build with a successful build, but I fail on ./gradlew bootRun. This is a gradle project, where we are using the following flyway configuration to create our user, password, and db connection:
flyway {
url = System.getenv('HW_DATABASE_URL')
user = System.getenv('HW_DATABASE_USER')
password = System.getenv('HW_DATABASE_PASSWORD')
}
I then check the .bashrc file, which is what I'm sourcing these values with, and each of the 3 fields has the correct values that I created. And then I run an env | grep HW command to confirm that the values do show up on the command line and are the ones being used, and they correctly do.
Here is the .bashrc file without the values being shown:
Another interesting note is that all of this was working and I was able to run the app on my Mac prior to updating to Big Sur. After the update, this became the issue.
Any ideas from anyone here? Would be much appreciated.

How to Remove HSQLDB Datasource in Tomee?

Apologies if I am posting a duplicate , if so please point me to original question.
I am running a tomee instance. I am configuring my data sources in context.xml and that are being pickup fine. But tomee also creates a HSQLDB datasource by default. And for few transaction it is being picked up and gives me error.
I would like to remove/ disable it from configuration so that it doesn't gives me those false error like:
user lacks privilege or object not found.
And I would be able to see actual issue with my application.
As Gimby said, you should define it in tomee.xml
If you are using eclipse check that your server locations is set to
"use Tomcat installation (take control...)"
and not
"use workspace metadata"
usefull links:
how-to-define-mysql-data-source-in-tomee
MySQL datasource in TomEE

Spring Boot Deployed in Tomcat gives 404 but works Stand-alone

I have been testing Spring Boot with embedded Tomcat for about a month now to build a REST API. Everything was working fine. We now want to deploy the API in a separate development environment which has a couple of other (non-Spring) applications running on a Tomcat container.
I made the changes specified in Converting a Spring Boot JAR Application to a WAR using Maven and Spring Boot Docs.
The deployment goes well (logs are fine, no errors) and looking at Tomcat management i see my application deployed. However, when I attempt to access http://localhost:8080/sophia/users in curl I get 404.
Any help is much appreciated.
UPDATE:
Here are my logs:
Netbeans:
NetBeans: Deploying on Apache Tomcat 8.0.17
profile mode: false
debug mode: false
force redeploy: true
In-place deployment at /home/bugz/workspace/pdcore/sophiaserver/target/sophia-server-1.0.0-SNAPSHOT
Deployment is in progress...
deploy?config=file%3A%2Ftmp%2Fcontext1845402702541504208.xml&path=/sophia
OK - Deployed application at context path /sophia
Start is in progress...
start?path=/sophia
OK - Started application at context path /sophia
Tomcat:
INFO 10:47:52:703 org.springframework.boot.context.embedded.ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/sophia/*]
INFO 10:47:54:042 org.springframework.boot.SpringApplication - Started application in 8.285 seconds (JVM running for 12087.301)
22-Jan-2015 10:47:54.060 INFO [http-nio-8080-exec-99] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor /home/bugz/workspace/server/apache-tomcat-8.0.17/conf/Catalina/localhost/sophia.xml has finished in 12,091 ms
And in sophia.xml for Catalina localhost:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" docBase="/home/bugz/workspace/pdcore/sophiaserver/target/sophia-server-1.0.0-SNAPSHOT" path="/sophia"/>
I've tried accessing
http://localhost:8080/sophia/users
http://localhost:8080/sophia-server-1.0.0-SNAPSHOT/users (name of WAR)
The first returns a 404 but with the CORS information from my CORS filter bean. The second return 404 without CORS information (which indicates the application has started and is configured but I do not seem to have access to the Controllers).
When running an application the path to call consists of a couple of parts.
The first is the base URL on which the application is deployed, in your case that is /sophia.
The second is the servlet mapping of the DispatcherServlet in your case that is /sohpia/*.
The third is the mapping of the controller inside the DispatcherServlet, in your example that is /users.
All those things combined create the URL /sophia/sophia/users.
The difference between the deployment as a WAR is that you included a separate URL to deploy on, when running as a jar it, by default, is deployed to / (the root).
You could fix it by putting /sophia as the server.context-path in the application.properties and map the DispatcherServlet to /* or /. That will in both situations give you the URL you want (and expected).
check java -version means the if you complied war in java 8 and tomcat is running on java 7 then it doesn't work.
Have you tried extending the SpringBootServletInitializer and overriding the configure method? As stated here
The entry point for jar file is different [Main Class] when compared to running your app as war file inside a container.
Hope this helps.
check the java version
version that exists in pom.xml must be the same that the version installed on the server
openjdk 11 works fine for me
https://adoptopenjdk.net/

Liquibase JPA configuration of the referenceUrl

I am struggling to build the JPA annotated classes diff changesets against my database, using liquibase.
Still, I am very confused about few things.
I use the following :
liquibase.properties
#liquibase.properties
driver: org.postgresql.Driver
classpath: real_path/.m2/repository/org/postgresql/postgresql/9.2-1002-jdbc4/postgresql-9.2-1002-jdbc4.jar
url: jdbc:postgresql://localhost:5432/diquiz
username: postgres
password: postgres
referenceUrl: hibernate:ejb3:diQuiz
referenceUsername: postgres
referencePassword: postgres
changeLogFile: changelog-master.xml
and
java -jar real_path\liquibase-core-3.0.6.jar diffChangeLog
and a normal persistence.xml file with standard JPA configuration.
I get an error which says : Liquibase diffChangeLog Failed: java.lang.RuntimeException: Cannot find database driver: Driver class was not specified and could not be determined from the url (hibernate:ejb3:unit)
I am confused because the below answer says that we need a hibernate.cfg.xml file (even I have persistence.xml instead), but then he says that we can use some url's which are defined on wiki page.
Hibernate using JPA (annotated Entities) and liquibase
Wiki page says that if we need to use JPA, we can choose between three types of URL's.
hibernate:ejb3:myPersistenceUnit
hibernate:ejb3:com.example.MyConfigFactory
hibernate:ejb3:myPersistenceUnit?hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
So, I choose the first one, and I've set in the liquidbase.properties this as referenceUrl.
Also, on the wiki page it is mentioned : (https://github.com/liquibase/liquibase-hibernate/wiki)
If you are using the command line version of Liquibase, you simply
have to add the liquibase-hibernate[3|4].jar file to the
LIQUIBASE_HOME/lib directory.
I did this too.
Still, doesnt work. Can someone explain me somewhow what am I missing ?
Thanks a lot!
The problem is that you are calling liquibase with "java -jar". The Class-path line in the jar can't pick up additional jars from the lib directory and so the liquibase-hibernate.jar is not being included in the classpath.
Run liquibase using the sh/bat file included in the liquibase jar: real_path/liquibase[.bat] diffChangeLog

Categories