Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to call Class.forName in Tomcat Web Application even if I use a JDBC Driver 4.0?

I'm studing Java Web, and during my studies I learned that from version 4 of the JDBC no need to use the method Class.forName to load the driver. I confirmed this information in this link.

Based on this information, I developed my getConnection method without that call, but when I started my Tomcat and I went to test my method, the following exception was thrown.

jan 23, 2015 7:55:48 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [br.com.caelum.servlet.AdicionaContatoServlet] in context with path [/fj21-agenda] threw exception
java.lang.RuntimeException: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/fj21
    at br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:14)
    at br.com.caelum.jdbc.dao.ContatoDAO.<init>(ContatoDAO.java:19)
    at br.com.caelum.servlet.AdicionaContatoServlet.service(AdicionaContatoServlet.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/fj21
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:12)
    ... 25 more

I'm using on my tests:

  • Tomcat 8
  • Java 8
  • Eclipse Luna Service Release 1 (4.4.1)
  • mysql-connector-java-5.1.34-bin.jar

If I include the line Class.forName("com.mysql.jdbc.Driver"); all works fine. Someone have any idea about this?

like image 395
Humberto Corrêa Avatar asked Dec 11 '22 01:12

Humberto Corrêa


1 Answers

During the Tomcat startup, the Service Provider mechanism scans for database drivers in the $CATALINA_BASE/lib folder to register them. But this process is not executed for each web application.

Thus, the web applications that have database drivers in their WEB-INF/lib directory cannot rely on the service provider mechanism and should register the drivers explicitly.

See more in Apache Tomcat 8 - JNDI Datasource HOW-TO.

like image 75
Paul Vargas Avatar answered Dec 28 '22 06:12

Paul Vargas