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)