I have been trying to Connect from a Grails project to a MySQL Database but I have not managed it yet.
I am working on IntelliJ IDEA 17 with the following versions:
| Grails Version: 3.3.0.RC1
| Groovy Version: 2.4.12
| JVM Version: 1.8.0_131
build.gradle
dependencies{
compile files ("lib/mysql-connector-java-5.1.43-bin")
}
application.yml
hibernate:
cache:
queries: false
use_second_level_cache: false
use_query_cache: false
dataSource:
pooled: true
jmxExport: true
driverClassName: com.mysql.jdbc.Driver
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
username: root
password: ''
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:mysql:3306//localhost/mydatabase
test:
dataSource:
dbCreate: update
url: jdbc:mysql:3306//localhost/mydatabase
production:
dataSource:
dbCreate: none
url: jdbc:mysql:3306//localhost/mydatabase
Errors
"C:\Program Files\Java\jdk1.8.0_131\bin\java" -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -XX:CICompilerCount=3 -Djline.WindowsTerminal.directConsole=false -Dfile.encoding=UTF-8 -classpath C:\Users\Vaggelis\AppData\Local\Temp\classpath.jar org.grails.cli.GrailsCli run-app
|Resolving Dependencies. Please wait...
CONFIGURE SUCCESSFUL
Total time: 3.824 secs
|Running application...
2017-08-07 19:42:52.921 ERROR --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
java.sql.SQLException: Unable to load class: com.mysql.jdbc.Driver from ClassLoader:sun.misc.Launcher$AppClassLoader#18b4aac2;ClassLoader:sun.misc.Launcher$AppClassLoader#18b4aac2
2017-08-07 19:42:55.391 ERROR --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : Unable to load class: com.mysql.jdbc.Driver from ClassLoader:sun.misc.Launcher$AppClassLoader#18b4aac2;ClassLoader:sun.misc.Launcher$AppClassLoader#18b4aac2
2017-08-07 19:42:55.405 ERROR --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Destroy method on bean with name 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor' threw an exception
2017-08-07 19:42:55.431 ERROR --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'methodValidationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Unsatisfied dependency expressed through method 'methodValidationPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastoreServiceRegistry': Cannot resolve reference to bean 'hibernateDatastore' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is org.hibernate.exception.GenericJDBCException: Unable to check JDBC Connection auto-commit in preparation for DDL execution
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastoreServiceRegistry': Cannot resolve reference to bean 'hibernateDatastore' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is org.hibernate.exception.GenericJDBCException: Unable to check JDBC Connection auto-commit in preparation for DDL execution
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is org.hibernate.exception.GenericJDBCException: Unable to check JDBC Connection auto-commit in preparation for DDL execution
Caused by: org.hibernate.exception.GenericJDBCException: Unable to check JDBC Connection auto-commit in preparation for DDL execution
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':bootRun'.
Process 'command 'C:\Program Files\Java\jdk1.8.0_131\bin\java.exe'' finished with non-zero exit value 1
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Error |
Failed to start server (Use --stacktrace to see the full trace)
Process finished with exit code 1
The MySQLConnector is being correctly imported, so somewhere I must have entered wrong data inside the datasource.
In grails 3 dialect is specified under hibernate section and not under datasource.
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
I'm using this configuration and it's working fine:
build.gradle
dependencies {
runtime 'mysql:mysql-connector-java:5.1.29'
}
application.yml
hibernate:
cache:
queries: false
use_second_level_cache: true
use_query_cache: false
region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
dataSource:
pooled: true
jmxExport: true
environments:
development:
dataSource:
dbCreate: create-drop
url: "jdbc:mysql://localhost:3306/mydb?autoReconnect=true"
driverClassName: "com.mysql.jdbc.Driver"
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
username: "yyyyy"
password: "xxxxx"
I fixed a similar issue in my Grails 3.2.9 project by removing the tablePerConcreteClass mapping from two of my abstract domain classes:
static mapping = {
tablePerConcreteClass true
}
Related
I am learning new stuff and I am trying to run a spring boot app. When I click on submit request on UI:
Error could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
in intelliJ, below error is showing
authenticated user user, setting security context
2021-04-20 23:52:54.052 WARN 11796 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1364, SQLState: HY000
2021-04-20 23:52:54.052 ERROR 11796 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : Field 'request_id' doesn't have a default value
2021-04-20 23:52:54.055 ERROR 11796 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement] with root cause
probably, application is trying to save the field request_id without a value.
Provide a default value for this field in database or source code.
I am developling a Springboot Application with an active database. First, I tried configuring my database by adding the following in application.properties:
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/schema_name?characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username = root
spring.datasource.password = password
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hdm2ddl.auto = update
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql = true
The Springboot Application can run perfectly fine with this configuration. However, as I try to move the above configuration lines in application-dev.properties and add spring.profiles.active = dev in my original application.properties, the following error yields:
2021-04-10 11:29:59.097 WARN 58148 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource [org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]: Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' parameter 0; nested exception is 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 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2021-04-10 11:29:59.103 INFO 58148 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2021-04-10 11:29:59.134 INFO 58148 --- [ restartedMain] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-04-10 11:29:59.172 ERROR 58148 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
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 (the profiles dev are currently active).
I searched for solutions but all of them suggests to either disable/exclude JPA (not the case) or configure the database connection properly (which I did, and succeeded in the main profile); from the error message it is also shown that dev profile is active. How can I solve this problem?
I get this except in AWS Elastic Beanstalk, but the app works locally.
01-Aug-2018 07:44:54.815 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
Caused by: org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://d-use1-xx.xxxxxxxxxxx.us-east-1.rds.amazonaws.com:3306/xxxxxxxxxxx
The JDBC URL is specified correctly. I have this in file.war/WEB-INF/classes/application.properties
spring.datasource.url = jdbc:mysql://d-use1-xx.xxxxxxxxxx.us-east-1.rds.amazonaws.com:3306/xxxxxxxxx
spring.datasource.username = xxxx
spring.datasource.password = xxxxxxxxx
I do have \WEB-INF\lib\mysql-connector-java-5.1.46.jar in the WAR file.
I don't know if it matters but I recently added a JDBC TokenStore for Spring Security OAuth2 and added this in the main class.
#SpringBootApplication
#MapperScan("com.xxxxxx.xxxxxx.mapper")
public class XxxxxxxxxxxxApplication extends SpringBootServletInitializer {
#Bean(name = "OAuth")
#ConfigurationProperties(prefix="spring.datasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
I don't know if it matters (never did before) but there was an empty environment variable set
01-Aug-2018 07:44:38.290 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DJDBC_CONNECTION_STRING=
You need to add the spring.datasource.driver-class-name configuration property:
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
JDBC automatic driver loading only works for drivers on the initial (system) class path of the application, but drivers located in WEB-INF/lib are added to a context class path at a later time and cannot be automatically loaded.
This means you need to explicitly load them, which is what Spring Boot does if you specify spring.datasource.driver-class-name (or the environment variable SPRING_DATASOURCE_DRIVER_CLASS_NAME as you found out).
I was able to fix it by setting environment variable
SPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver
Which AWS turns into
01-Aug-2018 17:01:03.505 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DSPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver
I don't know why it suddenly required that property. I am using flyway-core-3.2.1.jar. I didn't specify a version in https://flywaydb.org/documentation/plugins/springboot so that's what it picked up.
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Cannot
create PoolableConnectionFactory (Access denied for user
'root'#'localhost' (using password: YES))
WARN :
org.springframework.web.context.support.XmlWebApplicationContext -
Exception encountered during context initialization - cancelling
refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'userDao': Unsatisfied dependency
expressed through field 'sessionFactory'; nested exception is
org.springframework.beans.factory.BeanCreationException:
Error
creating bean with name 'sessionFactory' defined in ServletContext
resource [/WEB-INF/spring/appServlet/dao-context.xml]: Invocation of
init method failed; nested exception is
org.hibernate.exception.GenericJDBCException: Unable to open JDBC
Connection for DDL execution ERROR:
org.springframework.web.context.ContextLoader - Context initialization
failed
JDBC Connection properties file
jdbc.username = root
jdbc.password= 1234
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/estore
mysql grant follow picture
My spring web project is not running by MySQL Access deny Error
Please Help me
I try to migrate an app from spring-boot1 to spring-boot 2, but I have a lot of difficulty with the spring-boot part:
I have this log:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot find changelog location: class path resource [db/changelog/db.changelog-master.yaml] (please add changelog or check your Liquibase configurati
with this yaml:
spring:
application:
name: xxx
jpa:
generate-ddl: false
liquibase:
change-log: "classpath:/liquibase/xxx-db/db.changelog.json"
datasource:
platform: oracle
liquibase and spring-boot 2 seem ignore the liquibase part of my application.yml
any idea?
my pom:
<spring-boot.version>2.0.3.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<liquibase.version>3.4.2</liquibase.version>
So, I have found:
I was using this:
spring.config.location=other.yml
whiche was working find in springboot1, but not anymore in springboot2
during the migration from spring-boot 1 to spring-boot 2, the behavior of the parameter spring.config.location ( where the application.yml- like file are) have change. (source:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide)
In springboot1, spring.config.location=other.yml add to the default file application.yml other.yml file
In springboot2, spring.config.location replace the default application.yml file by the other.yml file.
To correct, I will need to do this:
spring.config.location=application.yml,other.yml