grails 3.0.2 mysql connectivity - mysql

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.

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.

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

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

JMeter can't connect to local database

I am trying to run a .jmx script to run locally on my CentOS machine.. I have it configured to connect as:
jdbc:mysql//localhost:3306/test
With all the correct credentials (logging in as root). Everytime I run the script on CentOS command line:
./bin/jmeter.sh -Jusers=5000 -n -t ~/test.jmx -l results/test.log
I get this:
2015/12/14 20:11:28 WARN - jmeter.protocol.jdbc.config.DataSourceElement: Could not load driver: com.mysql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:278)
at org.apache.avalon.excalibur.datasource.ResourceLimitingJdbcDataSource.configure(ResourceLimitingJdbcDataSource.java:264)
at org.apache.jmeter.protocol.jdbc.config.DataSourceElement.initPool(DataSourceElement.java:235)
at org.apache.jmeter.protocol.jdbc.config.DataSourceElement.testStarted(DataSourceElement.java:108)
at org.apache.jmeter.engine.StandardJMeterEngine.notifyTestListenersOfStart(StandardJMeterEngine.java:214)
at org.apache.jmeter.engine.StandardJMeterEngine.run(StandardJMeterEngine.java:336)
at java.lang.Thread.run(Thread.java:745)
I've looked up a few things online and a lot of articles advised me to put the JDBC .jar file in it's lib/ folder. I have done that and it is still not working.. Is there anything else I should try?
Make sure you have placed correct mysql-connector file in Lib folder. Issues may also come in case of version incompatibility between your mysql and mysql-connector.
You can download latest mysql-connector-java-5.1.38-bin.jar from following URL: https://dev.mysql.com/downloads/connector/j/3.1.html
Just take the JAR file from zip and place it your JMETER lib folder.
To properly configure MySQL JDBC connection refer to official JMeter documentation: http://jmeter.apache.org/usermanual/component_reference.html#JDBC_Connection_Configuration