Spring Boot DatasourceBean error, Macbook, MySQL - mysql

I'm getting the following error when I deploy my application to Tomcat 8. I have MySQL installed on my macbook :
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate com.catalina.productcarried.dao.impl.ProductCarriedDAOImpl.jdbcTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 26 common frames omitted
Here is my application.properties file :
#Database properties local
spring.datasource.url=jdbc:mysql://localhost:3306/simulator?autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
debug=true

Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
Your application.properties is not taken into account for some reason. Note that the driver-classis unnecessary, Spring Boot detects that from the url parameter.
Make sure to review the following:
The mysql driver is defined in your dependencies
The content of the application.properties file you provided in description is in one of the supported locations

Related

Configuring a XA datasource to mysql 8 DB with spring-boot and bitronix JTA manager fails with SqlException XAERR_RMERR

I have created a Spring Boot 2 (2.1.6.RELEASE) project with dependencies on spring-boot-starter-data-jpa and spring spring-boot-starter-jta-bitronix with a XA datasource configured for Mysql DB (8.0.16).
The application property file (trimmed for relevance with placeholder values between <>) contains the following configuration:
spring:
datasource:
url: jdbc:mysql://<host>:<port>/<dbName>
username: <username>
password: <password>
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
database-platform: org.hibernate.dialect.MySQL8Dialect
hibernate:
ddl-auto: none
jta:
bitronix:
properties:
server-id: <serverid>
On starting the spring boot application, I received the error stacktrace below:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is bitronix.tm.resource.ResourceConfigurationException: cannot create JDBC datasource named dataSource
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is bitronix.tm.resource.ResourceConfigurationException: cannot create JDBC datasource named dataSource
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is bitronix.tm.resource.ResourceConfigurationException: cannot create JDBC datasource named dataSource
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is bitronix.tm.resource.ResourceConfigurationException: cannot create JDBC datasource named dataSource
Caused by: bitronix.tm.resource.ResourceConfigurationException: cannot create JDBC datasource named dataSource
Caused by: bitronix.tm.recovery.RecoveryException: failed recovering resource dataSource
Caused by: com.mysql.cj.jdbc.MysqlXAException: XAER_RMERR: Fatal error occurred in the transaction branch - check your data for consistency
Caused by: java.sql.SQLException: XAER_RMERR: Fatal error occurred in the transaction branch - check your data for consistency
Disclaimer: I am self documenting my own question in hope of helping others as surprisingly this question / issue was quite obscure to resolve.
From verifying the bitronix issue tracker on github https://github.com/bitronix/btm/issues/100, it was not very obvious on what the resolution was even though the issue was already closed without a direct answer.
Reading the bitronix FAQ at https://github.com/bitronix/btm/wiki/FAQ gave a hint on the issue though pertaining to Oracle which refers to missing user privileges.
Further investigation led to the MySQL 8 documentation page on privileges https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html with a special highlight on the following section.
Prior to MySQL 8.0, any user could execute the XA RECOVER statement to
discover the XID values for outstanding prepared XA transactions,
possibly leading to commit or rollback of an XA transaction by a user
other than the one who started it. In MySQL 8.0, XA RECOVER is
permitted only to users who have the XA_RECOVER_ADMIN privilege, which
is expected to be granted only to administrative users who have need
for it. This might be the case, for example, for administrators of an
XA application if it has crashed and it is necessary to find
outstanding transactions started by the application so they can be
rolled back. This privilege requirement prevents users from
discovering the XID values for outstanding prepared XA transactions
other than their own. It does not affect normal commit or rollback of
an XA transaction because the user who started it knows its XID.
Therefore, I added the required privilege to the datasource user through the following commands in MySQL (Replace username and host section as appropriate).
GRANT XA_RECOVER_ADMIN ON *.* TO 'username'#'%';
FLUSH PRIVILEGES;
With this change, the spring boot application starts without any issue.

Error on Running Spring Boot Application on centos tomcat

My Spring Boot Application works perfectly on localhost but it gives an error when I run it on Centos. This is the error that's shown on the page.
Root Cause
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor 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$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
....
Root Cause
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
....
Root Cause
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
....
Root Cause
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:247)
....
application.yml file
`# ===============================
# = Hibernate datasource
# ===============================
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://67.209.127.115:3306/blog
username: shadab
password: test
# ===============================
# = JPA configurations
# ===============================
jpa:
show-sql: true
hibernate:
ddl-auto: update
database-platform: MYSQL
properties:
hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
pom.xml
I have included mysql-connector in pom.xml file but still it doesn't work.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
You are missing MySQL dependency from your classpath. Just put this into Maven/Gradle build:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

Grails 3 urlMappingsReport failed

I have created a Grails 3 Restful application, using MySQL as my database. The endpoints are working fine when I hit them with Rest Clients like Postman.
I am using this blog for reference. But it fails when I try to run the command grails url-mappings-report.
Here is the output:
:compileJava NO-SOURCE
:compileGroovy
:buildProperties UP-TO-DATE
:processResources
:classes
:findMainClass
:urlMappingsReport
Configuring Spring Security Core ...
... finished configuring Spring Security Core
Configuring Spring Security REST 2.0.0.M2...
... finished configuring Spring Security REST
... with GORM support
2017-10-01 17:45:24.867 ERROR --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
restTokenValidationFilter
↓
restAuthenticationProvider
↓
tokenStorageService
↓
userDetailsService
┌─────┐
| hibernateDatastore
↑ ↓
| userPasswordEncoderListener
└─────┘
Context failed to load: Error creating bean with name 'restTokenValidationFilter': Cannot resolve reference to bean 'restAuthenticationProvider' while setting bean property 'restAuthenticationProvider'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restAuthenticationProvider': Cannot resolve reference to bean 'tokenStorageService' while setting bean property 'tokenStorageService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tokenStorageService': Cannot resolve reference to bean 'userDetailsService' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailsService': Initialization of bean failed; 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.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userPasswordEncoderListener': Cannot resolve reference to bean 'hibernateDatastore' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'hibernateDatastore': Requested bean is currently in creation: Is there an unresolvable circular reference?
:urlMappingsReport FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':urlMappingsReport'.
> Process 'command '/usr/lib/jvm/java-8-openjdk-amd64/bin/java'' 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.
BUILD FAILED
Total time: 37.453 secs
| Error Command [url-mappings-report] error: Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-3.4.1-bin.zip'. (Use --stacktrace to see the full trace)
Grails Version: 3.2.10
Groovy Version: 2.4.10
JVM Version: 1.8.0_131
Try using GORM version >= 6.1
I get the same issue with GORM version 6.0.12.RELEASE

grails test-app MetaDataAccessException after upgrade to mysql-connector-java:5.1.34

I recently upgraded to the mysql:mysql-connector-java:5.1.34 connector. My Grails version is 2.2.4. MySQL version is 5.6.10. Upgrading went very smoothly, and the application itself seems to go fine. However, one of my test apps is failing.
The following error happens:
Error Error executing script TestApp:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'transactionManagerPostProcessor':
Initialization of bean failed;
nested exception is
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'transactionManager':
Cannot resolve reference to bean 'sessionFactory'
while setting bean property 'sessionFactory';
nested exception is
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory':
Cannot resolve reference to bean 'hibernateProperties'
while setting bean property 'hibernateProperties';
nested exception is
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'hibernateProperties':
Cannot resolve reference to bean 'dialectDetector'
while setting bean property 'properties' with key [hibernate.dialect];
nested exception is
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dialectDetector':
Invocation of init method failed;
nested exception is
org.springframework.jdbc.support.MetaDataAccessException:
Error while extracting DatabaseMetaData;
nested exception is java.sql.SQLException:
Access denied for user 'web'#'localhost' (using password: YES)
(Use --stacktrace to see the full trace)
I doublechecked the web user, and as far as I can tell the account is defined properly and has admin privs comparable to root. This has happened both on my development box with my personal database and on our jenkins build system.
I don't see anything in the logs, Running with --stacktrace does not give me any useful information, googling does not show this as a common error.

BeanCreationException in grails deployment on linux env

I am trying to deploy a war file created using grails on tomcat6. The same war files works just alright on my windows machine it does give me following exception with prod linux environment
SEVERE: Exception sending context initialized event to listener instance of class org.codehaus.groovy.grails.web.context.GrailsContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'hibernateProperties' while setting bean property 'hibernateProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateProperties': Cannot resolve reference to bean 'dialectDetector' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
and
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateProperties': Cannot resolve reference to bean 'dialectDetector' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
I have already gone through
How do I fix the org.springframework.beans.factory.BeanCreationException error in my Grails application?
and
Grails transactionManager exception on Run
but it die not help
Scroll to the end where it says Cannot load JDBC driver class 'com.mysql.jdbc.Driver'. It's usually the case with Grails exceptions that the top-level text is generic and useless, and the real cause is further down near the bottom.
I'm guessing you put the MySQL driver jar in the lib directory, but Grails didn't find it there. You shouldn't use jars unless they're not available in an online repo, and the MySQL driver certainly is. In fact it's the sample entry in BuildConfig.groovy in the dependencies block. Uncomment that, optionally updating to the latest version (5.1.28), and redeploy your war file. Make sure it's there in the file, in WEB-INF/lib