-->
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.  [ 5 posts ] 
Author Message
 Post subject: No suitable driver found for jdbc:mysql://localhost/events
PostPosted: Sun Apr 15, 2012 3:41 pm 
Newbie

Joined: Sun Apr 15, 2012 3:31 pm
Posts: 2
Hi,
I am unable to connect to mysql database through hibernate. Details are below:
Server: Tomcat 6.0
Error:3383 [main] WARN org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:mysql://localhost/events
3413 [main] INFO org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
3453 [main] INFO org.hibernate.engine.jdbc.internal.LobCreatorBuilder - HHH000422: Disabling contextual LOB creation as connection was null
3495 [main] INFO org.hibernate.engine.transaction.internal.TransactionFactoryInitiator - HHH000399: Using default transaction strategy (direct JDBC transactions)
3516 [main] INFO org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory - HHH000397: Using ASTQueryTranslatorFactory
4436 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000228: Running hbm2ddl schema update
4436 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000102: Fetching database metadata
4438 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000319: Could not get database metadata
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/events
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)

hibernate.cfg.xml entries:
Code:
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc&#58;mysql&#58;//localhost/events</property>
      <property name="hibernate.connection.username">events</property>
      <property name="hibernate.connection.password">events</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>


The mysql driver mysql-connector-java-5.1.19-bin is already placed in WEB-INF/lib directory. Also tried in tomcat lib directory.

Hibernate version used is 4.1.2

Please help me to resolve this error.


Top
 Profile  
 
 Post subject: Re: No suitable driver found for jdbc:mysql://localhost/events
PostPosted: Mon Apr 16, 2012 3:07 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I guess it has to do with https://hibernate.onjira.com/browse/HHH-7169
bolsover in https://forum.hibernate.org/viewtopic.php?f=1&t=1015136 has a similiar problem with hibernat4.1.2


Top
 Profile  
 
 Post subject: Version 4.1.2 SessionFactory guidance needed
PostPosted: Mon Apr 16, 2012 8:00 am 
Regular
Regular

Joined: Sat Apr 23, 2005 7:28 am
Posts: 52
From my perspective, the problem has been introduced with ClassLoader changes introduced to DriverManagerConnectionProviderImpl in version 4.1.2. Sadly, I have not found any good solution yet other than to explicitly load the database driver prior to making any calls to hibernate e.g.:

Code:
try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }


I think the community need some guidance from the hibernate gurus on the 'correct' method of initialising a SessionFactory in circumstances where the database driver is on the classpath.


Top
 Profile  
 
 Post subject: Re: No suitable driver found for jdbc:mysql://localhost/events
PostPosted: Tue Apr 17, 2012 8:38 am 
Regular
Regular

Joined: Sat Apr 23, 2005 7:28 am
Posts: 52
I have now done quite a bit of testing in an effort to identify why this problem should occur in version 4.1.2.

Firstly, I tested against MySql on the basis that tis is main stream and should work

HilbernateUtil.java
Code:
package hibernatetest1;

//~--- non-JDK imports --------------------------------------------------------
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;


public class HibernateUtil {

    public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
    private static final Configuration configuration;
    private static final ServiceRegistry serviceRegistry;
    private static final SessionFactory sessionFactory;

    static {
        try {

            // Create the SessionFactory
            configuration = new Configuration();
            configuration.addClass(hibernatetest1.Countries.class);


            configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
            configuration.setProperty("hibernate.connection.url",
                    "jdbc:mysql://localhost:3306/mybusinessrecords");
            configuration.setProperty("hibernate.connection.username", "root");
            configuration.setProperty("hibernate.connection.password", "secret");
            configuration.setProperty("show_sql", "false");
            configuration.configure("hibernate.cfg.xml");
            serviceRegistry =
                    new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        } catch (Throwable ex) {

            ex.printStackTrace();

            throw new ExceptionInInitializerError(ex);
        }
    }


    @SuppressWarnings("unchecked")
    public static void closeSession() {
        Session s = session.get();

        if (s != null) {
            s.close();
        }

        session.set(null);
    }


    @SuppressWarnings("unchecked")
    public static Session openSession() {
        Session s = session.get();

        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }

        return s;
    }
}



hibernate.cfg.xml
Code:
<?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>
    </session-factory>
</hibernate-configuration>


countries.java
Code:
package hibernatetest1;

/**
* Class description
*
*
* @version Enter version here..., 10/03/09
* @author Enter your name here...
*/
public class Countries implements java.io.Serializable {

    private Integer country_id;
    private String country;

    /**
     * Constructs ...
     *
     */
    public Countries() {
    }

    /**
     * @return the country_id
     */
    public Integer getCountry_id() {
        return country_id;
    }

    /**
     * @param country_id the country_id to set
     */
    public void setCountry_id(Integer country_id) {
        this.country_id = country_id;
    }

    /**
     * @return the country
     */
    public String getCountry() {
        return country;
    }

    /**
     * @param country the country to set
     */
    public void setCountry(String country) {
        this.country = country;
    }
   
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("country_id = ");
        sb.append(country_id);
        sb.append("\n");
        sb.append("country = ");
        sb.append(country);
        sb.append("\n");
        return sb.toString();
    }
}


countries.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class batch-size="10" name="hibernatetest1.Countries" table="COUNTRIES">
        <id name="country_id" type="integer">
            <column name="COUNTRY_ID" not-null="false"/>
            <generator class="assigned"/>
        </id>
        <property name="country" type="string">
            <column length="22"  name="COUNTRY" not-null="false" />
        </property>
    </class>
</hibernate-mapping>



HibernateTest1.java
Code:
package hibernatetest1;

//~--- non-JDK imports --------------------------------------------------------

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

//~--- JDK imports ------------------------------------------------------------


/**
*
* @author DBolsover
*/
public class HibernateTest1 {

    /**
     * Method description
     *
     */
    private void init() {
        System.out.println(System.getProperty("java.class.path"));

        Session   session = HibernateUtil.openSession();
        Countries country = (Countries) session.createCriteria(Countries.class).add(Restrictions.eq("country_id",
                                1)).uniqueResult();

        HibernateUtil.closeSession();
        System.out.println(country.toString());
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        HibernateTest1 ht1 = new HibernateTest1();

        ht1.init();
    }
}


Result - no problems - Hibernate performs as expected and should - :-)

Next, I tested against a PervasiveSQL database:

HibernateUtil.java
Code:
package hibernatetest3;

//~--- non-JDK imports --------------------------------------------------------
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

    public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
    private static final Configuration configuration;
    private static final ServiceRegistry serviceRegistry;
    private static final SessionFactory sessionFactory;

    static {
        try {

            // Create the SessionFactory
            configuration = new Configuration();
            configuration.addClass(hibernatetest3.Currency.class);

            configuration.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
            configuration.setProperty("hibernate.connection.driver_class", "com.pervasive.jdbc.v2.Driver");
            configuration.setProperty("hibernate.connection.url",
                    "jdbc:pervasive://127.0.0.1:1583/JS2010R2;encoding=cp1252");
            configuration.setProperty("hibernate.connection.username", "");
            configuration.setProperty("hibernate.connection.password", "");
            configuration.setProperty("show_sql", "false");
            configuration.configure("hibernate.cfg.xml");
            serviceRegistry =
                    new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        } catch (Throwable ex) {

            ex.printStackTrace();

            throw new ExceptionInInitializerError(ex);
        }
    }

    @SuppressWarnings("unchecked")
    public static void closeSession() {
        Session s = session.get();

        if (s != null) {
            s.close();
        }

        session.set(null);
    }

   
    @SuppressWarnings("unchecked")
    public static Session openSession() {
        Session s = session.get();

        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }

        return s;
    }
}


hibernate.cfg.xml
Code:
<?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>
<property name="hibernate.dialect">hibernatetest3.PervasiveDialect</property>
    </session-factory>
</hibernate-configuration>


HibernateTest3.java
Code:
package hibernatetest3;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class HibernateTest3 {

    private void loadDriver() {
        try {
            Class.forName("com.pervasive.jdbc.v2.Driver");
           
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    private void init() {
        System.out.println(System.getProperty("java.class.path"));

        Session session = HibernateUtil.openSession();
        Currency currency = (Currency) session.createCriteria(Currency.class).add(Restrictions.eq("currency",
                "£")).uniqueResult();

        HibernateUtil.closeSession();
        System.out.println(currency.toString());
    }

    public static void main(String[] args) {
        HibernateTest3 ht3 = new HibernateTest3();
        ht3.loadDriver();
        ht3.init();
    }
}



Using the Pervasive driver, I have to force loading of the driver with a call to Class.forName("com.pervasive.jdbc.v2.Driver");
Otherwise, an exception is thrown at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192) and the test fails.

Not quite sure where to go from here - clearly there is some issue with PervasiveSQL driver that prevents it from laoding in the same way as the MySql driver - but I have no idea why or where to go from here...

If required I can provide copies of the PervasiveDialect Currency class and mappings.

db


Top
 Profile  
 
 Post subject: Re: No suitable driver found for jdbc:mysql://localhost/events
PostPosted: Tue Apr 17, 2012 8:22 pm 
Newbie

Joined: Sun Apr 15, 2012 3:31 pm
Posts: 2
Thanks all,
Seems issue is with version 4.1.2. I downgraded to 3.6.0 and all works fine. This might work with 4.1.0 as well. I went with 3.6.0 as Spring 3 supports 3.6.0 only.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.