1 Reply Latest reply on Jun 14, 2012 5:36 PM by madchedar0

    JBoss 7.1.1 and Hibernate 4.0.1 with mySQL - java.sql.SQLException: No suitable driver found for jdbc:mysql://

    madchedar0

      I'm trying to create an ejb that programmatically configures hibernate to connect to a database.

       

      I'm able to connect using just jdbc calls, but when I use hibernate, I'm getting a "No suitable driver found" error.

       

      I have a HibernateFun bean that tells hibernate to load a User.hbm.xml file, then I set the hibernate connection properties.  dbhost is just mapped to 127.0.0.1 and my dummy user/password is user/password.  Again, I'm able to connect a-ok and retrieve data using a JDBC connection (see the jdbcConnect method).

       

      Here's my code:

       

       

      package test.ejb;
      
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.ResultSet;
      import java.sql.Statement;
      import javax.annotation.PostConstruct;
      import javax.ejb.Singleton;
      import javax.ejb.Startup;
      import org.apache.log4j.Logger;
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      
      import org.hibernate.cfg.Configuration;
      
      @Singleton
      @Startup
      public class HibernateFun {
          protected Logger logger;
      
          private static SessionFactory hibernateSessionFactory;
      
          public HibernateFun() {
              logger = Logger.getLogger(this.getClass());
          }
      
          @PostConstruct
          public void postConstruct() {
              //logger.info("This is a print from the post construct method!");
      
              /*logger.info("Going to try a regular jdbc connection");
              jdbcConnect();
              logger.info("Finished a regular jdbc connection test");*/
      
              logger.info("Trying a hibernate connect");
              hibernateConnect();
              logger.info("Finished hibernate connect");
          }
      
          private void hibernateConnect() {
              Configuration cfg = new Configuration();
      
              cfg.addClass(User.class);
              cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); 
              cfg.setProperty("hibernate.connection.url", "jdbc:mysql://dbhost:3306/");
              cfg.setProperty("hibernate.connection.username", "user");
              cfg.setProperty("hibernate.connection.password", "password");
              cfg.setProperty("hibernate.connection.pool_size", "1"); //?
      
              cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
      
              hibernateSessionFactory = cfg.buildSessionFactory();
      
      
              logger.info("Going to try and do a session");
              Session session = hibernateSessionFactory.openSession();
      
              int ID = 1;
              User user = (User) session.createQuery("SELECT u FROM User u "
                          + "WHERE u.ID = ?").setInteger(0, ID).uniqueResult();
      
              session.close();
      
              logger.info("Here's the user I got: " + user.getName());
          }
      
          // This does connect/work correctly...
          private synchronized void jdbcConnect() {
              Connection conn = null;
              String url = "jdbc:mysql://dbhost:3306/";
              String driver = "com.mysql.jdbc.Driver";
              String userName = "user";
              String password = "password";
              try {
                  Class.forName(driver).newInstance();
                  conn = DriverManager.getConnection(url, userName, password);
                  logger.info("\t\t*** Connected to the database! ***");
                  conn.close();
                  logger.info("\t\tDisconnected from database");
              } catch (Exception e) {
                  logger.error("\t\t *** Trouble connecting to database: " + e);
              }
          }
      
      }
      

       

       

      And here's the log.  Not sure why there are double prints but that's another story:

       

      14:03:40,718 INFO  [test.ejb.HibernateFun] (MSC service thread 1-3) Trying a hibernate connect
      14:03:40,718 INFO  [com.emergentspace.mbs.data.DataTransformerBean] (MSC service thread 1-12) Starting MBS data transformation service
      14:03:40,728 INFO  [org.hibernate.cfg.Configuration] (MSC service thread 1-1) HHH000221: Reading mappings from resource: test/ejb/User.hbm.xml
      14:03:40,731 INFO  [org.hibernate.cfg.Configuration] (MSC service thread 1-3) HHH000221: Reading mappings from resource: test/ejb/User.hbm.xml
      14:03:40,733 INFO  [org.hibernate.cfg.Configuration] (MSC service thread 1-1) HHH000221: Reading mappings from resource: test/ejb/User.hbm.xml
      14:03:40,734 INFO  [org.hibernate.cfg.Configuration] (MSC service thread 1-3) HHH000221: Reading mappings from resource: test/ejb/User.hbm.xml
      14:03:40,776 WARN  [org.hibernate.internal.util.xml.DTDEntityResolver] (MSC service thread 1-1) HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http
      ://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
      14:03:40,776 WARN  [org.hibernate.internal.util.xml.DTDEntityResolver] (MSC service thread 1-3) HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http
      ://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
      14:03:40,856 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-4) Initializing Mojarra 2.1.7-jbossorg-1 (20120227-1401) for context '/mbs'
      14:03:40,878 INFO  [org.jboss.web] (MSC service thread 1-9) JBAS018210: Registering web context: /db
      14:03:40,902 INFO  [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (MSC service thread 1-3) HHH000402: Using Hibernate built-in connection pool (not for productio
      n use!)
      14:03:40,902 INFO  [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (MSC service thread 1-1) HHH000402: Using Hibernate built-in connection pool (not for productio
      n use!)
      14:03:40,907 INFO  [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (MSC service thread 1-1) HHH000115: Hibernate connection pool size: 1
      14:03:40,907 INFO  [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (MSC service thread 1-3) HHH000115: Hibernate connection pool size: 1
      14:03:40,909 INFO  [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (MSC service thread 1-1) HHH000006: Autocommit mode: false
      14:03:40,910 INFO  [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (MSC service thread 1-3) HHH000006: Autocommit mode: false
      14:03:40,912 INFO  [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (MSC service thread 1-1) HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://n
      exusdb:3306/]
      14:03:40,913 INFO  [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (MSC service thread 1-3) HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://n
      exusdb:3306/]
      14:03:40,914 INFO  [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (MSC service thread 1-1) HHH000046: Connection properties: {user=user, password=****}
      14:03:40,916 INFO  [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (MSC service thread 1-3) HHH000046: Connection properties: {user=user, password=****}
      14:03:40,918 WARN  [org.hibernate.engine.jdbc.internal.JdbcServicesImpl] (MSC service thread 1-1) HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:mysql://n
      exusdb:3306/
      14:03:40,920 WARN  [org.hibernate.engine.jdbc.internal.JdbcServicesImpl] (MSC service thread 1-3) HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:mysql://n
      exusdb:3306/
      14:03:40,923 INFO  [org.hibernate.dialect.Dialect] (MSC service thread 1-1) HHH000400: Using dialect: org.hibernate.dialect.MySQLInnoDBDialect
      14:03:40,924 INFO  [org.hibernate.dialect.Dialect] (MSC service thread 1-3) HHH000400: Using dialect: org.hibernate.dialect.MySQLInnoDBDialect
      14:03:40,927 INFO  [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] (MSC service thread 1-1) HHH000422: Disabling contextual LOB creation as connection was null
      14:03:40,928 INFO  [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] (MSC service thread 1-3) HHH000422: Disabling contextual LOB creation as connection was null
      14:03:40,930 INFO  [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] (MSC service thread 1-1) HHH000399: Using default transaction strategy (direct JDBC transactions)
      14:03:40,932 INFO  [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] (MSC service thread 1-3) HHH000399: Using default transaction strategy (direct JDBC transactions)
      14:03:40,935 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (MSC service thread 1-1) HHH000397: Using ASTQueryTranslatorFactory
      14:03:40,938 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (MSC service thread 1-3) HHH000397: Using ASTQueryTranslatorFactory
      14:03:40,971 INFO  [test.ejb.HibernateFun] (MSC service thread 1-1) Going to try and do a session
      14:03:40,974 INFO  [test.ejb.HibernateFun] (MSC service thread 1-3) Going to try and do a session
      14:03:41,046 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (MSC service thread 1-1) SQL Error: 0, SQLState: 08001
      14:03:41,046 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (MSC service thread 1-3) SQL Error: 0, SQLState: 08001
      14:03:41,047 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (MSC service thread 1-1) No suitable driver found for jdbc:mysql://dbhost:3306/
      14:03:41,049 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (MSC service thread 1-3) No suitable driver found for jdbc:mysql://dbhost:3306/
      14:03:41,061 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC00001: Failed to start service jboss.deployment.subunit."mbs.ear"."mbs-ejb.jar".component.HibernateFun.START: org.jboss.msc.
      service.StartException in service jboss.deployment.subunit."mbs.ear"."mbs-ejb.jar".component.HibernateFun.START: Failed to start service
              at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
              at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_30]
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_30]
              at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_30]
      Caused by: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
              at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:163)
              at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:85)
              at org.jboss.as.ejb3.component.singleton.SingletonComponent.getComponentInstance(SingletonComponent.java:116)
              at org.jboss.as.ejb3.component.singleton.SingletonComponent.start(SingletonComponent.java:130)
              at org.jboss.as.ee.component.ComponentStartService.start(ComponentStartService.java:44)
              at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
              at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
              ... 3 more
      Caused by: javax.ejb.EJBException: org.hibernate.exception.JDBCConnectionException: Could not open connection
              at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:166)
              at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:230)
              at org.jboss.as.ejb3.tx.CMTTxInterceptor.requiresNew(CMTTxInterceptor.java:333)
              at org.jboss.as.ejb3.tx.SingletonLifecycleCMTTxInterceptor.processInvocation(SingletonLifecycleCMTTxInterceptor.java:56)
              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
              at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
              at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
              at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
              at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:161)
              ... 9 more
      Caused by: org.hibernate.exception.JDBCConnectionException: Could not open connection
              at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:131)
              at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
              at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
              at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
              at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)
              at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
              at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.extractPhysicalConnection(ConnectionProxyHandler.java:82)
              at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:138)
              at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
              at $Proxy42.prepareStatement(Unknown Source)    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:147)
              at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:166)
              at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:145)
              at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1739)
              at org.hibernate.loader.Loader.doQuery(Loader.java:828)
              at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
              at org.hibernate.loader.Loader.doList(Loader.java:2463)
              at org.hibernate.loader.Loader.doList(Loader.java:2449)
              at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279)
              at org.hibernate.loader.Loader.list(Loader.java:2274)
              at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
              at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
              at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196)
              at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1115)
              at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
              at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:890)
              at test.ejb.HibernateFun.hibernateConnect(HibernateFun.java:71)
              at test.ejb.HibernateFun.postConstruct(HibernateFun.java:37)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_30]
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_30]
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_30]
              at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_30]
              at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptorFactory$ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptorFactory.ja
      va:130)
              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
              at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
              at org.jboss.as.ee.component.ManagedReferenceInterceptorFactory$ManagedReferenceInterceptor.processInvocation(ManagedReferenceInterceptorFactory.java:95)
              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
              at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
              at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
              at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:228)
              ... 18 more
      Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://dbhost:3306/
              at java.sql.DriverManager.getConnection(DriverManager.java:602) [rt.jar:1.6.0_30]
              at java.sql.DriverManager.getConnection(DriverManager.java:154) [rt.jar:1.6.0_30]
              at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173)
              at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:276)
              at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
              ... 56 more
      

       

      This ejb is contained within an .ear.... and in the jboss-deployment-structure.xml this is how the ejb is defined:

       

      <sub-deployment name="my-test-ejb.jar">
              <dependencies>
                  <module name="org.apache.log4j"/>
                  <module name="org.hibernate"/>
                  <module name="com.mysql"/>
              </dependencies>
          </sub-deployment>
      

       

      I installed mysql as a module... so it's referenced in jboss-deployment-structure.xml above...

       

      Any ideas?

       

      I've checked out:

      http://stackoverflow.com/questions/6969970/hibernate-no-suitable-driver-found-for-jdbcmysql

      http://stackoverflow.com/questions/10065923/error-deploying-hibernate-app-on-jboss-no-suitable-driver-found-for-jdbc

      http://stackoverflow.com/questions/10761910/jboss-7-1-no-suitable-driver-found-javamysql-could-not-open-connection/10770576#10770576

      http://stackoverflow.com/questions/9097699/jboss-cant-find-jdbc-driver-for-postgresql-in-development-environment

      https://forum.hibernate.org/viewtopic.php?f=1&t=1015157

       

      Thanks

        • 1. Re: JBoss 7.1.1 and Hibernate 4.0.1 with mySQL - java.sql.SQLException: No suitable driver found for jdbc:mysql://
          madchedar0

          These two threads ended up helping me out:

          https://community.jboss.org/thread/177875

          https://forum.hibernate.org/viewtopic.php?f=1&t=1015136

           

          What I have is a working solution but it definitely is more of a hack.  I ended up including the driver connector I was using (com.mysql) as a dependency within the org.hibernate module used by my ejb.  There must be a better way?  I'll need to read more on this but hopefully the jboss and hibernate communities know of a better solution.

           

          Working solution:


          I already had a mysql driver that I installed as a module in my jboss7.1.1:

          https://community.jboss.org/wiki/DataSourceConfigurationInAS7#Installing_a_JDBC_driver_as_a_module

           

          In my $JBOSS_HOME/modules/org/hibernate/main module I altered the module.xml:

           

           

          <dependencies>
                  <module name="asm.asm"/>
                  <module name="javax.api"/>
                  <module name="javax.persistence.api"/>
                  <module name="javax.transaction.api"/>
                  <module name="javax.validation.api"/>
                  <module name="org.antlr"/>
                  <module name="org.apache.commons.collections"/>
                  <module name="org.dom4j"/>
                  <module name="org.infinispan" optional="true"/>
                  <module name="org.javassist"/>
                  <module name="org.jboss.as.jpa.hibernate" slot="4" optional="true"/>
                  <module name="org.jboss.logging"/>
                  <module name="org.hibernate.envers" services="import" optional="true"/>
                  <!-- my hack!!! -->
                  <module name="com.mysql"/>
          

           

          I added my database driver module (in this case com.mysql) as a dependency to hibernate itself.  This is a very dirty solution, but it seems to work with the code I originally posted. 

           

          Hopefully there is a better solution?  I'll leave this "unanswered" until a cleaner solution is known.  Really the goal is to have everything contained in one .ear and deployed to the JBoss server vice hacking individual files on JBoss itself.  Right?

          1 of 1 people found this helpful