-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Cannot load JDBC driver class
PostPosted: Sat Mar 16, 2013 10:34 am 
Newbie

Joined: Sat Mar 16, 2013 10:19 am
Posts: 2
I have run into a problem during Hibernate configuration in my web-app. I am using mysql (which works fine from MySql Workbench) and trying to configure hibernate to use it in a tomcat 6.0.35 deployment. During the Spring configuration it is throwing an exception indicating that it can’t load the com.mysql.jdbc.Driver class which resides in the mysql-connector-java-5.1.23.jar. I cite that jar in my maven pom file and so the jar is deployed in the WEB-INF/lib directory. I have
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>
</dependency>
also tried putting the jar in the tomcat/lib directory. Having the jar in either directory results in the same error.
In my web.xml I have the following entry to point to my custom configuration class.
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>donsexample.spring.config.SpringContextConfigurator</param-value>
</init-param>
I am using Java-based Spring configuration.

I have spent many hours trying to figure this out and would appreciate any help with this issue..

thanks

Don C.


I have included the SpringContextConfigurator source code here:
Code:
--------------------------------------------------------------------------------------------
package donsexample.spring.config;

import java.util.Properties;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

@Configuration
@ComponentScan("donsexample") 
@EnableScheduling
@EnableTransactionManagement
@EnableWebMvc

public class SpringContextConfigurator
{
   @Autowired
   protected Environment env;   

   // The following is used to establish the database connection..
   // These beans are unique within an @Configuration class.
   @Bean
   public DataSource dataSource() throws Exception {
                
        final BasicDataSource ds = new BasicDataSource();  // Another Spring Class.
        
        ds.setDriverClassName("com.mysql.jdbc.Driver ");
        ds.setUrl("jdbc:mysql//localhost:3306/donsdb" );
        ds.setUsername("root");
        ds.setPassword("*********");
        ds.setValidationQuery("Select 1 from DUAL");
        ds.setRemoveAbandoned(true);
        ds.setTestOnBorrow(true);
        ds.setTestWhileIdle(true);
   return ds;
   }
   
   // These beans are unique within an @Configuration class. --> Added @Autowired
   @Bean
   @Autowired
   public HibernateTransactionManager transactionManager() throws Exception
   {

      HibernateTransactionManager transactionManager = new HibernateTransactionManager(); // HIbernate class..
   
      // Get our session factory from the method below.
   //   SessionFactory theFactory = getSessionFactory();
      SessionFactory theFactory = (SessionFactory)getSessionFactory();
      transactionManager.setSessionFactory(theFactory);      
   return transactionManager;
   }
      
   // These beans are unique within an @Configuration class.
   @Bean
   public SessionFactory getSessionFactory() throws Exception {
         
      // Loop here so we can attached via Eclipse to debug initialization..
       try
       {
         boolean stillLooping = true;
         while(stillLooping)
         {
            Thread.currentThread().sleep(5000);
         }
       }
       catch(InterruptedException ie)
       {
         
       }
      
      Properties properties = new Properties();
      properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
      properties.put("hibernate.show_sql", "true");      
      
      //-------------- added the following Properties ------
      
      properties.put("format_sql", "true");
      properties.put("hbm2ddl.auto", "create");
      //----------------------------------------------------
      
      AnnotationSessionFactoryBean factory = new AnnotationSessionFactoryBean();
      factory.setPackagesToScan(new String [] {"donsexample.model"});
      
      // Get the data Source defined in the method above
      
      Class mysqlDriverClass = Class.forName("com.mysql.jdbc.Driver");
      DataSource ourDataSource = dataSource();

      factory.setDataSource(ourDataSource);
      factory.setHibernateProperties(properties);

      // <<<< This is where we get the error about trying to load  >>>>
      // the com.mysql.jdbc.Driver even though we can load it
      // up above with Class.forName(...)
      
      // Note: the afterPropertiesSet method is inherited from
      // AbstractSessionFactoryBean
      factory.afterPropertiesSet();//  <<- Error happens here!

      return factory.getObject();
   }

}

I have tracked down where the error occurs in my Spring configuration java. This occurs during the following call:
factory.afterPropertiesSet();
I notice that the mysql-connector-java-5.1.23.jar is in the list of jars (looking at it in eclipse) in the factory instance before the afterPropertiesSet call.

Traceback follows:
org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver '
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371) at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:113)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2863)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at donsexample.spring.config.SpringContextConfigurator.getSessionFactory(SpringContextConfigurator.java:190)
at donsexample.spring.config.SpringContextConfigurator$$EnhancerByCGLIB$$11cad483.CGLIB$getSessionFactory$2(<generated>) at donsexample.spring.config.SpringContextConfigurator$$EnhancerByCGLIB$$11cad483$$FastClassByCGLIB$$65c17077.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:280)
at donsexample.spring.config.SpringContextConfigurator$$EnhancerByCGLIB$$11cad483.getSessionFactory(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597) …etc, etc, etc,
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)


Top
 Profile  
 
 Post subject: Re: Cannot load JDBC driver class
PostPosted: Tue Mar 19, 2013 7:57 am 
Newbie

Joined: Thu Mar 22, 2012 7:43 am
Posts: 3
Quote:
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver


check your project BuildPath. Sometimes when updating project using maven fails to download necessary jar files.


Top
 Profile  
 
 Post subject: Re: Cannot load JDBC driver class
PostPosted: Tue Mar 19, 2013 8:47 am 
Newbie

Joined: Sat Mar 16, 2013 10:19 am
Posts: 2
Thanks for looking into this. However, I have checked the expanded war after deploying to Tomcat. The mysql-connector-java-5.1.23.jar is in the WEB-INF/lib directory. Note: I have already tried putting the jar into the tomcat/lib directory also (at the same time removing it from WEB-INF/lib). Same result.

I downloaded the source code for hibernate and dbcp commons. I then copied some of the code from the offending method BasicDataSource.createConnectionFactory into my SpringContextConfigurator getFactorySession bean method . However, when placed in the my SpringContextConfigurator , it loads the driver class with no problems. However, it still throws the same NoClassFound Exception down in the BasicDataSource.createConnectionFactory. Is this due to a different context being used for BasicDataSource and therefore, some weird stuff with the WebAppsClassLoader?

Other ideas which haven't panned out:

I have recently been looking at whether I need any additional tomcat configuration (a la server.xml or context.xml). But so far no luck on that front.

In my recent readings I also found something about if the bootstrap class loader has already loaded a class then it may prevent any one else loading it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.