Hibernate – Specified JDBC Driver com.ibm.db2.jcc.DB2Driver class not found

classnotfoundexceptiondb2hibernatejdbc

I have spent considerable time researching and trying to figure out where I have gone wrong with my configuration but am completely stuck. From the posts I have been reading the implication is that my driver is not in the classpath. I added the Class.forName("com.ibm.db2.jcc.DB2Driver") line to ensure that the driver is available and it loads in that line. I would really appreciate any suggestions as to what I may have missed.

my code is:

    public class TestHibernateConfig {

    private static SessionFactory sessionFactory;

    public static void testConfig() {
        try {
            DB2Driver drvr = (DB2Driver) Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();

            Configuration cfg = new Configuration();
            cfg.configure("hibernate.cfg.xml");
            sessionFactory = cfg.buildSessionFactory();
            Session sess = sessionFactory.getCurrentSession();
            Transaction tx = sess.beginTransaction();

            Criteria crit = sess.createCriteria(ItemData.class);
            List items = crit.list();

        } catch (Exception e) {
            Logger logger = Logger.getLogger("");
            logger.error(e.getMessage());
        }
    }
}

I get the class not found error when my code hits:
sessionFactory = cfg.buildSessionFactory();

hibernate.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory name="speed2db2">
 <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
  <property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
  <property name="hibernate.connection.url">jdbc:db2://appdb:50000/MYTEST</property>
  <property name="hibernate.connection.username">myUser</property>
  <property name="hibernate.connection.password">myPass</property>
 </session-factory>
</hibernate-configuration>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="speedPersistUnit">
        <class>ItemData</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>
            <property name="hibernate.connection.url" value="jdbc:db2://appdb:50000/MYTEST"/>
            <property name="hibernate.default_schema" value="MYSCHEMA"/>
            <property name="hibernate.connection.username" value="myUser"/>
            <property name="hibernate.connection.password" value="myPass"/>
            <property name="hibernate.connection.driver_class" value="com.ibm.db2.jcc.DB2Driver"/>
        </properties>
    </persistence-unit>
</persistence>

From within Eclipse I am able to connect to the database and if I go to the Hibernate perspective, I am able to browse the database schema. I am also able to use the HQL editor to load data in my class.

Edit:

09:35:50,338 INFO  [org.hibernate.cfg.Configuration] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000043: Configuring from resource: /com/newpig/speed2DB2/hibernate.cfg.xml
09:35:50,340 INFO  [org.hibernate.cfg.Configuration] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000040: Configuration resource: /com/newpig/speed2DB2/hibernate.cfg.xml
09:35:50,369 INFO  [org.hibernate.cfg.Configuration] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000041: Configured SessionFactory: speed2db2
09:35:52,059 INFO  [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000402: Using Hibernate built-in connection pool (not for production use!)
09:36:16,856 ERROR [] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) Specified JDBC Driver com.ibm.db2.jcc.DB2Driver class not found

The error happens on this line.

 sessionFactory = cfg.buildSessionFactory();

Deployed to my .war file:
.War
|-WEB-INF
||-lib
|||-db2jcc4.jar
|||-hibernate-core-4.1.2.Final.jar
|||-hibernate-entitymanager-4.1.2.Final.jar
|||-hibernate-jpa-2.0-api-1.0.1.Final.jar

Best Answer

I don't think I mentioned that I am using JBoss7.1. In the end I was not able to figure out why Hibernate was failing to load the driver but got around it by switching to use JNDI and configuring the datasource in the JBoss standalone.xml instead of trying to make the connection from Hibernate.

Related Topic