Org. Springframework. Dao. Dataaccessresourcefailureexception
I'm Getting the Following Errors Exception in Thread "Main" Org. Springframework. Dao. Dataaccessresourcefailureexception: Error Retrieving Database Metadata...
I'm getting the following errors
Exception in thread "main" org.springframework.dao.DataAccessResourceFailureException: Error retrieving database metadata; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Client does not support authentication protocol requested by server; consider upgrading MySQL client)
at org.springframework.jdbc.core.metadata.TableMetaDataProviderFactory.createMetaDataProvider(TableMetaDataProviderFactory.java:103)
at org.springframework.jdbc.core.metadata.TableMetaDataContext.processMetaData(TableMetaDataContext.java:205)
at org.springframework.jdbc.core.simple.AbstractJdbcInsert.compileInternal(AbstractJdbcInsert.java:276)
at org.springframework.jdbc.core.simple.AbstractJdbcInsert.compile(AbstractJdbcInsert.java:263)
at org.springframework.jdbc.core.simple.AbstractJdbcInsert.checkCompiled(AbstractJdbcInsert.java:309)
at org.springframework.jdbc.core.simple.AbstractJdbcInsert.doExecuteAndReturnKey(AbstractJdbcInsert.java:369)
at org.springframework.jdbc.core.simple.SimpleJdbcInsert.executeAndReturnKey(SimpleJdbcInsert.java:133)
at sample.spring.chapter07.bankapp.dao.BankAccountDaoImpl.createBankAccount(BankAccountDaoImpl.java:35)
at sample.spring.chapter07.bankapp.service.BankAccountServiceImpl.createBankAccount(BankAccountServiceImpl.java:17)
at sample.spring.chapter07.bankapp.BankApp.main(BankApp.java:28)
Caused by: org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Client does not support authentication protocol requested by server; consider upgrading MySQL client)
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:293)
at org.springframework.jdbc.core.metadata.TableMetaDataProviderFactory.createMetaDataProvider(TableMetaDataProviderFactory.java:63)
... 9 more
when I try to execute the following main file
package sample.spring.chapter07.bankapp;
import java.util.Date;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sample.spring.chapter07.bankapp.domain.BankAccountDetails;
import sample.spring.chapter07.bankapp.domain.FixedDepositDetails;
import sample.spring.chapter07.bankapp.service.BankAccountService;
import sample.spring.chapter07.bankapp.service.FixedDepositService;
public class BankApp {
private static Logger logger = Logger.getLogger(BankApp.class);
public static void main(String args[]) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:META-INF/spring/applicationContext.xml");
BankAccountService bankAccountService = context
.getBean(BankAccountService.class);
BankAccountDetails bankAccountDetails = new BankAccountDetails();
bankAccountDetails.setBalanceAmount(1000);
bankAccountDetails.setLastTransactionTimestamp(new Date());
int bankAccountId = bankAccountService
.createBankAccount(bankAccountDetails);
logger.info("Created bank account with id - " + bankAccountId);
FixedDepositService fixedDepositService = context
.getBean(FixedDepositService.class);
FixedDepositDetails fdd = new FixedDepositDetails();
fdd.setActive("Y");
fdd.setBankAccountId(bankAccountId);
fdd.setFdCreationDate(new Date());
fdd.setFdAmount(500);
fdd.setTenure(12);
int fixedDepositId = fixedDepositService.createFixedDeposit(fdd);
logger.info("Created fixed deposit with id - " + fixedDepositId);
logger.info(fixedDepositService.getFixedDeposit(fixedDepositId));
}
}
This is the application context file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns=""
xmlns:context="" xmlns:tx=""
xmlns:jee=""
xmlns:xsi=""
xsi:schemaLocation="
">
<context:component-scan base-package="sample.spring.chapter07.bankapp" />
<context:property-placeholder
location="classpath*:META-INF/spring/database.properties" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="namedJdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg index="0" ref="dataSource" />
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
and this is the database.properties file
database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc\:mysql\://localhost\:3306/spring_bank_app_db
database.username=root
database.password=root
What's wrong with this code?
1 Answer
You are using older version of MySQL - JDBC jar version which is not compatible to the MySQL server installed. Upgrade the jar to the latest version (8.x.x).
If you are using Maven, then add dependency to pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.x.x</version>
</dependency>
or download the jar from here.