Error :"Cannot create JDBC driver of class '' for connect URL 'null' java.sql.Sqlexception: No suitable driver"

Asked

Viewed 1,888 times

1

Hello, I am trying to configure the database connection in my JPA application using JNDI. I am following this tutorial (https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#Postgresql), but even so when I try to make any query the system gives error saying that it did not find the bank driver. I’m using Tomcat 8 container, Eclipse, and Postgres SQL 9.1.

I made the following settings:

1- Tomcat lib directory where I put the driver: inserir a descrição da imagem aqui

2-Application-specific context.xml file: inserir a descrição da imagem aqui

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Deixa a conexão com o banco a cargo do container -->
<Resource name="jdbc/gymclub" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="admin" password="123456" driverclassname="org.postgresql.Driver"
           url="jdbc:postgresql://localhost:5432/gymclub"/>
</Context>

3- web.xml:

<resource-ref>
 <description>postgreSQL Datasource example</description>
 <res-ref-name>jdbc/gymclub</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>

4- persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="gymclub">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!-- Deixa o controle de conexão com o banco a cargo do container. -->
    <non-jta-data-source>java:/comp/env/jdbc/gymclub</non-jta-data-source>
    <!-- Habilita cache de segundo nivel <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> -->
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" />
        <!-- False no ambiente de produção -->
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <!-- None no ambiente de produção -->
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <!-- Essa propriedade permite que as sequences sejam criadas com os valores 
            iniciais que são especificados no domínio. -->
        <property name="hibernate.id.new_generator_mappings" value="true" />
        <!-- Cache de segundo nivel do hibernate <property name="hibernate.cache.region.factory_class" 
            value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" /> -->
    </properties>
</persistence-unit>

When I run the application, Tomcat fires the following exception:

Cannot create JDBC driver of class '' for connect URL 'null'
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:315)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2144)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2032)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1532)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:279)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:124)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)

Does anyone know how to solve it? I have searched several places and found no solution =/

2 answers

2

Hello @Giuliana Bezerra,

Add the following Property:

<property name="hibernate.connection.datasource" value="java:comp/env/jdbc/gymclub"/>

Say the transaction type is local Source, ex:

<persistence-unit name="gymclub" transaction-type="RESOURCE_LOCAL">

For Tomcat8 it says in doc to put in context a:

maxWaitMillis="-1"
  • Hello @Dilnei Cuha, still gives the same error =/

  • 1

    One thing I forgot to mention but I think you already know this is that when we use the server by the IDE it caches the configuration files within Workspace ex: C: Users dilneicunha workspace_neon Servers Tomcat V8.0 Server at localhost-config

  • Yes, I am even changing them only so that the changes affect only the application I am developing and not all that use Tomcat.

  • The driver is only in the Tomcat lib folder or is also declared in Maven ?

  • It is in Maven too: <dependency> <groupid>postgresql</groupid> <artifactId>postgresql</artifactId> <version>9.1-901.jdbc4</version> </dependency>

  • Even, it works when I leave everything in persistence.xml. The driver is found well, but when I needed to change to use JNDI there is giving this driver error =/

  • 1

    Hmm I don’t know if this isn’t conflict, take a look at this post if you don’t happen to be able to tell me that I test here.

  • I did it! I’m going to post the answer of what I had to do, I had to go through more stuff. VLW @Dilnei Cunha!

  • Congratulations Giuliana! although I no longer use Container Servlet I will also like to know what was missing :)

Show 4 more comments

1


I managed with the help of @Dilnei Cunha. I made the following modifications:

1- The context I was modifying was not the right one, I had to modify what is in the Servers folder of the eclipse. Also the driver class property name was incorrect (was putting driverclassname when it should be driverClassName):

inserir a descrição da imagem aqui

Contents:

<?xml version="1.0" encoding="UTF-8"?>
<Context>

<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

<!-- Deixa a conexão com o banco a cargo do container -->
<Resource name="jdbc/gymclub" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000" maxWaitMillis="-1"
           username="admin" password="123456" driverClassName="org.postgresql.Driver"
           url="jdbc:postgresql://localhost:5432/gymclub"/>

2- After that he found the driver but gave incompatibility problem. I had to download a newer version of it:

inserir a descrição da imagem aqui

After those changes worked! @Dilnei Cunha, thanks again for the help! : D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.