2
votes

I am trying application in tomcat 8.0, but I get the error

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver found for jdbc:mysql://ip:3306/DURGA_DEV?characterEncoding=UTF-8

at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:630) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:727) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:737) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:787) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]

I am using the

  1. Tomcat 8.0.21
  2. Spring 4.1.6
  3. Mysql Conector : mysql-connector-java-5.1.27.jar

My context.xml file is given below.

context.xml

<?xml version="1.0" encoding="UTF-8"?>



<beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:context="http://www.springframework.org/schema/context"
                xmlns:p="http://www.springframework.org/schema/p"
                xmlns:util="http://www.springframework.org/schema/util"
                xmlns:tx="http://www.springframework.org/schema/tx" 
                xsi:schemaLocation="
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

    <bean id="datasourceProperties" 
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:META-INF/config/datasource.properties</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
  </bean>
  <bean id="batchUpdateDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName">
            <value>com.mysql.jdbc.jdbc2.optional.MysqlDataSource</value>
        </property>

        <property name="url">
            <value>${jdbc.components.url.DURGA}</value>
        </property>

        <property name="username">
            <value>${jdbc.components.userName.DURGA}</value>
        </property>

        <property name="password">
            <value>${jdbc.components.password.DURGA}</value>
        </property>

 </bean>

  <bean id="batchJDBCTemplate" class="com.nri.durga.maf.batch.MafBatchJdbcTemplate">
    <constructor-arg
        type="org.springframework.jdbc.datasource.DriverManagerDataSource"
        ref="batchUpdateDataSource" />
 </bean>
  <bean id="GLOBAL" 
        class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
        p:URL="${jdbc.components.url.GLOBAL}"
        p:user="${jdbc.components.userName.GLOBAL}"
        p:password="${jdbc.components.password.GLOBAL}">        
  </bean>
  <bean id="DURGA" 
        class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
        p:URL="${jdbc.components.url.DURGA}"
        p:user="${jdbc.components.userName.DURGA}"
        p:password="${jdbc.components.password.DURGA}">        
  </bean> 

  <bean id="global-tm" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="global-em" />
    <property name="dataSource" ref="GLOBAL" />
  </bean>
  <bean id="durga-tm" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="durga-em" />
    <property name="dataSource" ref="DURGA" />
  </bean>  
  <tx:annotation-driven transaction-manager="transactionManager"/>

  <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="false" />
    <property name="generateDdl" value="false" />
    <property name="databasePlatform" value="${jpa.hibernate.dialectClass}" />
  </bean>

  <bean id="global-em" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:packagesToScan="com.nrift.finch.*.model, com.nrift.finch.*.domain" 
        p:dataSource-ref="GLOBAL"
        p:jpaVendorAdapter-ref="jpaVendorAdapter" />
  <bean id="durga-em" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:packagesToScan="com.nri.durga.*.model, com.nri.durga.*.domain" 
        p:dataSource-ref="DURGA"
        p:jpaVendorAdapter-ref="jpaVendorAdapter" /> 
</beans>

datasource.properties

jdbc.components.all=GLOBAL
# Setting for GLOBAL component
jdbc.components.url.GLOBAL=jdbc:mysql://ip:3306/DURGA_DEV
jdbc.components.userName.GLOBAL=usr
jdbc.components.password.GLOBAL=pswd
jdbc.components.minLimit.GLOBAL=1
jdbc.components.maxLimit.GLOBAL=200
jdbc.components.initialLimit.GLOBAL=1
jdbc.components.queryTimeout.GLOBAL=0

# Setting for DURGA component
jdbc.components.url.DURGA=jdbc:mysql://ip:3306/DURGA_DEV
jdbc.components.userName.DURGA=usr
jdbc.components.password.DURGA=pswd
jdbc.components.minLimit.DURGA=1
jdbc.components.maxLimit.DURGA=200
jdbc.components.initialLimit.DURGA=1
jdbc.components.queryTimeout.DURGA=0

# Datasource properties Mysql
jdbc.database.driverClass=com.mysql.jdbc.Driver
jpa.hibernate.dialectClass=org.hibernate.dialect.MySQL5Dialect
2
can you include your context.xml snippet for this datasource, and the code where you try to open your DB connectionJay
@jay I have attached the context.xml and datasource properties. If I downgarde the tomcat 7.0.6 and Spring 3.2, my application works fineAsish Kumar Samanta
I would start eliminating possibilities to see where the error is. 1) Can you connect from the server running tomcat? Try to manual connect outside of the java application. 2)Is the property file being read correctly? Try hardcoding the connection values in the context.xml. 3)Is tomcat trying to connect? Check the DB logs. etc...Jay

2 Answers

0
votes

This means that your mysql driver jar is not in the classpath. Make sure that it is in "$CATALINA_HOME/lib"

0
votes

I head a similar problem when deploying my application and starting Tomcat from Eclipse. After building the war and copying it manually to the webapps folder it was working.

My setup is very much as described in this question:

java.sql.SQLException: No suitable driver

The Driver class was in Tomcat's lib folder, but it seems, my config.xml was not loaded, when deployed from Eclipse into wtpwebapps.