I am getting Following Exception while configuring the Connection Pool in Tomcat

This is Context.xml

<Context path="/DBTest" docBase="DBTest"
        debug="5" reloadable="true" crossContext="true">

    <!-- maxActive: Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to -1 for no limit.
         -->

    <!-- maxIdle: Maximum number of idle dB connections to retain in pool.
         Set to -1 for no limit.  See also the DBCP documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         -->

    <!-- maxWait: Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->

    <!-- username and password: MySQL dB username and password for dB connections  -->

    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
         -->

    <!-- url: The JDBC connection url for connecting to your MySQL dB.
         -->

  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="password" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql:///BUSINESS"/>

</Context>

This is Bean Entry

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/TestDB"></property>
    <property name="resourceRef" value="true"></property>
</bean>



org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Co
nnection; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Ca
nnot load JDBC driver class 'com.mysql.jdbc.Driver'
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(Dat
aSourceUtils.java:82)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:
382)
        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:45
8)
        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:46
6)
        at com.businesscaliber.dao.Dao.getQueryForListMap(Dao.java:66)
        at com.businesscaliber.dao.MiscellaneousDao.getDefaultSucessStory(Miscel
laneousDao.java:109)
        at com.businesscaliber.listeners.BusinessContextLoader.contextInitialize
d(BusinessContextLoader.java:40)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContex
t.java:3795)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4
252)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
.java:760)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:74
0)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)

        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:831)

        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:720
)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490
)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1150)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
:311)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
eSupport.java:120)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)

        at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)

        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443
)
        at org.apache.catalina.core.StandardService.start(StandardService.java:4
48)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:700
)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driv
er class 'com.mysql.jdbc.Driver'
        at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDat
aSource.java:1136)
        at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSo
urce.java:880)
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(D
ataSourceUtils.java:113)
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(Dat
aSourceUtils.java:79)
        ... 30 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:164)
        at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDat
aSource.java:1130)
share|improve this question

3 Answers

up vote 4 down vote accepted

Nothing complex here - you have mysql configured, but the jdbc driver for mysql either you never installed or never placed in the classpath.

Ignore/disable spring, hibernate, connection pools and just try this java code in a jsp or whatever.

Class.forName("com.mysql.jdbc.Driver");

Until that works (eg file is in class path), you will continue to have sadness

share|improve this answer
1  
Sorry guys i forgot to add "mysql-connector-java-5.1.10-bin.jar" under "C:\apache-tomcat-5.5.28\common\lib" assums that it will work if it is in application \WEB-INF\lib\mysql-connector-java-5.1.10-bin.jar But code loads data at load on startup Thanks – Vicky Mar 12 '10 at 6:02

You must do the following steps:

1) Configuration of Spring beans --------------------------------

    <?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:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 <bean id="entityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="jpaVendorAdapter">
   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true" />
    <property name="generateDdl" value="true" />
    <property name="database" value="MYSQL" />
    <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
   </bean>
  </property>
 </bean>

 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
  scope="singleton">
  <property name="jndiName" value="jdbc/jpaExamples" />
  <property name="resourceRef" value="true" />
 </bean>

 <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
 </bean>

 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
 </bean>

 <tx:annotation-driven transaction-manager="transactionManager" />

 <!-- Definition of your beans -->

</beans>

2) Definition of DataSource resource in web-xml: ------------------------------------------------

 <resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/jpaExamples</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>

3) Definition of Resource in Context of server.xml --------------------------------------------------

<Context docBase="WebServiceRequestPersistence_Web3" path="/WebServiceRequestPersistence_Web3" debug="5" reloadable="true" crossContext="true" source="org.eclipse.jst.jee.server:WebServiceRequestPersistence_Web3">
 <Resource 
  name="jdbc/jpaExamples" 
  auth="Container"
  type="javax.sql.DataSource"
  maxActive="100" 
  maxIdle="30" 
  maxWait="10000"
  username="myUsername"
  password="myPassword" 
  driverClassName="com.mysql.jdbc.Driver" 
  url="jdbc:mysql://localhost:3306/jpaExamples" 
 />
</Context>

4) Copy of JDBC driver jar into lib of Apache Tomcat 6 ------------------------------------------------------ Copy your MySQL Java connector (for example: mysql-connector-java-5.1.6.jar) in the lib sub-directory of Apache Tomcat 6 installation.

share|improve this answer

If you are using MyEclipse and you are trying to run it from MyEclipse Tomcat; Go to preferences (Window > Preferences), then Servers > Integrated Sandbox > MyEclipse Tomcat > Paths Under prepend to calsspath, add the jar.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.