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

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.

Related

Spring boot JPA error in gradle build from docker

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.

Spring Boot DataSource: 'url' attribute is not specified

I am trying to follow this guide
https://spring.io/guides/gs/accessing-data-mysql/
but this guide is for the maven and i am trying the gradle
and getting this error
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
in application.properties i have these things only.
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
I tried all the possible things there is on the SO.
the only deps i have are these
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
Or You will have to prevent Spring boot from automatically configuring the data source by adding this line to the file application.properties.
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
This is kind of an old question but for anyone else coming across this, IF by any chance you are using IntelliJ (as OP does here based on his/her comments) make sure that our beloved IDE recognises your application.properties/application.yml as such by going to File -> Project Structure -> Modules then select your resources file and click on "Resources" from the Mark as: header thing (based on IntelliJ Community Edition 2019.1). Also keep in mind, as no doubt the IDE will certainly notify you, that by reimporting any Maven changes you will need to do this procedure again.
Check the target classpath directory. If the application.properties file didn't exist, then delete the target and rebuild.
I would search the reason somewhere else.
Your error log tells you Reason: Failed to determine a suitable driver class. Please build your project, and check if mysql driver is included in a built jar file. If jar is missing try to change your mysql dependency scope from runtimeOnly to implementation.
The same problem can occur (at least I think so) if you try to run this project from IDE and solution should also help in that case.
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. error is unfortunately very generic and can happen in many different situations. For example last week I had received this error while using oracle Oracle when there where special signs in password which was specified in properties.yaml without double-quotes.
I was having the same problem and solved it by migrating my project that was in version 2.4.x, to 2.3.x.
in IDE / STS (spring tool suit), every thing was working fine
but when made a "project.jar" file,
this was thrown.
unnecessary spaces " " in the "application.yml" file can cause this.
server:
port: 8085
spring:
datasource:
url: jdbc:mysql://localhost:3306/studentdb
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
application:
name: STUDENT-SERVICE
instead of tweaking my "application.yml" file
i simply moved all my statements in "application.yml" file to
"application.properties" file and formatted the statements like required in ".properties".
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/studentdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format.sql=true
spring.application.name=student-service
server.port=8085
and voilĂ 
(you can add params at the end of url)
(spring.datasource.url=jdbc:mysql://localhost:3306/studentdb?allowPublicKeyRetrieval=true&useSSL=false)
Make sure your pom.xml has the war packaging. It worked for me.
<packaging>war</packaging>
Spring boot has chanegd the url to jdbc-url.
You need to use as below
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/db_example
Document Link:
https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#data-properties
Stressing that, of course, we would search the documentation for "spring.cloud.config.jdbc-url", but our search should be directly through spring.datasource.hikari.jdbc-url.

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.

Tomcat JDBC MySQL ClassNotFoundException

I would like to use springMVC and JPA (using hibernate) on my tomcat 7 server (running locally on my Mac).
I was able to set up everything successfully with an embedded H2 database.
Now I switched to mysql and am getting the following error
java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver"
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
This suggests tomcat is having trouble finding the mysql-connector for java.
There are a multitude of tutorials on how to add the connector to $CATALINA_HOME/lib.
After trying to use a maven dependency for my project, I followed the advice and copied the .jar file into the lib directory:
$ ls $CATALINA_HOME/lib/mysql*.jar
/Users/david/Applications/tomcat/lib/mysql-connector-java-5.1.20-bin.jar
I have read and execute permissions on the directory and file.
At the moment I can't figure out how to make tomcat aware of the jar. The folder is included in
$CATALINA_HOME/conf/catalina.properties
and I have restarted the server multiple times.
Thanks for you help.
Normally, the output of a CNFE is as follows:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
whereas your error message shows
java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver"
I think you've still got quotes around the driver class name in your config.

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).