Spring boot JPA error in gradle build from docker - mysql

I have a Gradle project, i added jpa to it with:
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.4.3'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.23'
And in application.properties i have added the following lines:
spring.datasource.url=jdbc:mysql://user:pass#localhost/dbname
spring.datasource.username=null
spring.datasource.password=null
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
### Hibernate Properties
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update
It's working fine when the mysql server is hosted on the machine. But when i am building my spring boot application inside the docker with ./gradlew build command, without hosting the mysql its giving me the exception:
oreApplicationTests > contextLoads() FAILED
java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:1796
Caused by: javax.persistence.PersistenceException at AbstractEntityManagerFactoryBean.java:403
Caused by: org.hibernate.exception.JDBCConnectionException at SQLStateConversionDelegate.java:112
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException at SQLError.java:174
Caused by: com.mysql.cj.exceptions.CJCommunicationsException at NativeConstructorAccessorImpl.java:-2
Caused by: java.net.UnknownHostException at InetAddress.java:797
I don't want these errors during the build, I only want to turn on the mysql server when i will be using the jars. So i don't want these errors during the compile time.

I ran into a similar error when using MySQL. The solution for me was to add in the dialect to the .properties folder. I figured out the error was coming from JPA not knowing which dialect to use based on the database.
spring.jpa.database-platform=org.hibernate.dialect.{your dialect here}
Also keep in mind tests want to run with an in memory database by default so you need to wire up your tests to run as expected.

Related

Spring Cloud Data Flow Kubernetes Deployment fails for external database - Cannot load driver class: com.mysql.cj.jdbc.Driver

I am following the developer guide to deploy SCDF to a minikube cluster on my local machine. Used the helm chart approach. Was able to get it working with the defaults. The default deploys a mariadb in the cluster. I wanted to change it to use an external mysql db that is running in a docker container in my machine (outside the cluster). Followed the recommendations to change the values.yaml to enable the external DB and attributes for external DB connection (URI, dbname, user/pwd etc.).
Then deployed using "helm install my-release -f values.yaml bitnami/spring-cloud-dataflow"
The SCDF pod (& skipper pod) errors out because it can't find the mysql jdbc driver. kubectl logs on the pods show the following error: "java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver"
How do I include the mysql jdbc driver in the image for SCDF that gets deployed (or resolve this problem). I read SCDF already includes the drivers for std databases (true ?). New to helm/k8s so apologies if solution is obvious... Other posts on similar error all talk about including this in the pom.xml . But this is not a dependency issue with my (task) app but SCDF itself.
thanks
-------------------- more detail on the exception stack ---------------
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:657)
...
Looks like I just need to specify the mariadb jdbc driver in the values.yaml file for mysql as well. That got past the load class error.
But still would like to know how to prevent class load error for a different driver specified when SCDF is deployed to k8s via the helm chart.

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured with Spring, MySql and Gradle

I am working with Spring Boot and MySql. When I launch the application, I get the following error:
***************************
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).
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/*****
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
build.gradle
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "mysql:mysql-connector-java:$sqlClientVersion"
}
dependencies {
implementation 'mysql:mysql-connector-java:8.0.20'
}
Any help would be really appreciated

Grails: ClassNotFoundException: com.mysql.jdbc.Driver in WAR deployment

This is an issue that just started happening recently and its only with specific projects and WAR deployment. I am not able to deploy a WAR file because I get the following exception
ERROR org.apache.tomcat.jdbc.pool.ConnectionPool - Unable to create initial connections of pool.
java.sql.SQLException: com.mysql.jdbc.Driver
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:712)
[.....]
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246)
Nothing in my project has changed since my last successful deploy (I verify with git) and I can run the project fine using Spring Boot.
The mysql-connector is in my gradle build
runtime 'mysql:mysql-connector-java:5.1.46' (I have also tried changing to compile)
I have the driver declared properly in my application.yml
dataSource:
pooled: true
jmxExport: true
driverClassName: com.mysql.jdbc.Driver
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
username: ****
password: ****
environments:
production:
dataSource:
password: *****
dbCreate: none
url: jdbc:mysql://localhost:3306/myapp
I am using Intellij IDE and can confirm that the mysql-connector is listed in my classpath and when I explode my WAR file mysql-connector-java-5.1.46.jar is listed in my WEB-INF/lib
I have done multiple cleans and rebuilds of the project. I am really at a loss for what is happening and as mentioned its only specific projects. I have other grails projects with identical config setups and deploy just fine but a handful of others give me this error. Any ideas?
Tomcat: 7.0.70
Java: 1.8.0_91
MySQL: 5.6 and 5.7 (fails on both)
Grails: 3.3.1
Update:
So if I put my mysql jar in my Tomcat lib folder instead of my project lib folder then it works. So that proves that the JAR file is valid. When I put the JAR file back into the WEB-INF/lib folder of my app then it doesn't work. So it seems for some reason the JAR file is being ignored or not loaded when its packaged with my app. Again though, its just some apps. I have other apps that works just fine with the lib packaged with the app.
I'm sure. It's not issue of jar file.
You can check here in repo and here for all versions of MySQL connectors
Please verify your dependency runtime "mysql:mysql-connector-java:5.1.46"
Verify your system MySQL server user and password and dataSource username , password.
Verify your MySQL running fine with valid credentials.
Because ERROR org.apache.tomcat.jdbc.pool.ConnectionPool - Unable to create initial connections of pool. this error will come when MySQL is not running on system or invalid username password.
One more possibility of error:
If you have updated your MySQL version. It will try to connect securely.
So by you need to pass some parameters to URL mysql://localhost:3306/yourDb?autoReconnect=true&useSSL=false&useUnicode=true
Last option if you are not resolved problem:
Update your dependency and configuration with latest version.
Dependency for your reference:
runtime "mysql:mysql-connector-java:6.0.6"
and Application.yml
dataSource:
dbCreate: update
pooled: true
url: jdbc:mysql://localhost:3306/YourDb?autoReconnect=true&useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
driverClassName: com.mysql.cj.jdbc.Driver
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
username: yourUsername
password: yourPassword
properties:
...
driverClassName have been changed in latest version.
Hope this will helps you.

grails 3.0.2 mysql connectivity

I have installed grails 3.0.2 and xampp server on my system. I want to connect to MySql of Xampp through grails. So made some changes in application.yml file located at grails-app/conf folder now it looks like
dataSource:
pooled: true
jmxExport: true
driverClassName: "com.mysql.jdbc.JDBC4MySQLConnection"//changed driver class
username: root //username
password: 123456 //password
//now in develoment environments i changed dbCreat->update and
//url-> jdbc:mysql://localhost:3306/myDB
//i have not made any changes to test and production environments
environments:
development:
dataSource:
dbCreate: update
url: jdbc:mysql://localhost:3306/myDB;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
production:
dataSource:
dbCreate: update
url: jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
I am new to grails so i don't know where to place mysql-connector.jar but i have added path of my mysql-connector.jar in .dependencies file located at myProject/build folder.
When i try to run my app it gives a lot of errors and in end it gives exception as
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.JDBC4MySQLConnection
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(Poole
dConnection.java:246)
... 83 more
i am not able to understand error.
If their is any step my step tutorial for mysql connection with grails 3.0.2 and jdk 1.8 please post link.
Thanks in advance.
Grails 3.0 uses the Gradle build system for:
build related tasks such as compilation, runnings tests and producing binary distrubutions of your project
So you can add your dependency in the build.gradle file, inside the dependencies block:
dependencies {
// other dependencies ...
runtime 'mysql:mysql-connector-java:5.1.36'
}
See the Grails Gradle Build documentation and Gradle Dependency Management Basics to learn more about this.

Hibernate Reverse Engineering with Eclipse and MySql

I'm having the hardest time getting Eclipse to connect and reverse engineer from a MySQL5 database. I can see Eclipse connecting to my MySQL database and can even see the tables through the "Data Source Explorer" view but when I try it after creating Hibernate Console and Configuration files, I get the error:
org.hibernate.console.HibernateConsoleRuntimeException:
Problems while loading database
driverclass (com.mysql.jdbc.Driver)
Problems while loading database
driverclass (com.mysql.jdbc.Driver)
java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
From my simple java project, I start by creating a Hibernate Configuration File (cfg.xml).
Name of file: hibernate.cfg.xml
Session Factory name: org.hibernate.SessionFactory
Database Dialect: org.hibernate.dialect.MySQL5Dialect
Driver Class: com.mysql.jdbc.Driver
Connection Url: jdbc:mysql://localhost:3306/<myDatabaseName>
Default Schema: <myDatabaseName>
Username: correct username
Password: correct password
I also have selected the option to "Create a console Configuration"
At this point, I can see the new configuration listed in my "Hibernate Configuration" perspective/workbench pane. If I try to expand it, I get the earlier listed error.
I don't understand why I can see the database through the 'Data Source Explorer" and even though I'm using the DB connection profile listed in there as part of my configuration, I still get this error.
I also tried to create a new database profile using a manual mysql connector jar (mysql-connector-java-5.1.13-bin.jar) and same end result.
Versions of what I have:
Eclipse version: 3.6.0 aka Helios, Build 20100617 - 1415
MySQL: 5.1.34
Hibernate Tools (from JBoss): HibernateTools-3.3.1.v201006011046R-H111-GA
(placed into Eclipse's 'dropins' folder)
What am I doing wrong in my hibernate configuration setup?
Help!
Your Hibernate Console Configuration doesn't have the MySQL JDBC driver on its classpath, hence the java.lang.ClassNotFoundException:com.mysql.jdbc.Driver. From the reference guide of the Hibernate Tools:
3.4. Creating a Hibernate Console Configuration
...
alt text http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html_single/images/plugins/plugins_3.png
...
Classpath: The classpath for loading POJO and JDBC drivers; only needed if
the default classpath of the Project
does not contain the required classes.
Do not add Hibernate core libraries or
dependencies, they are already
included. If you get ClassNotFound
errors then check this list for
possible missing or redundant
directories/jars.
Include default classpath from project: When enabled the project
classpath will be appended to the
classpath specified above
Does the "associated project" have the MySQL JDBC driver declared as library? If not, then you must add it (either as a project library or in the above tab).