You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Sreyan Chakravarty <sr...@gmail.com> on 2015/08/28 21:11:59 UTC

Tomcat can't find JDBC Driver listed under Maven

Okay I am trying to use connection pooling for my project which uses Maven
as a build tool. Now the JDBC Driver is listed as a maven dependency.
Tomcat however cant find it when I try to do

ds.getConnection (ie. get a JDBC connection from a datasource).

The error that I am getting is -:

java.sql.SQLException: Cannot create JDBC driver of class '' for connect
URL 'null'
    at
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2065)
    at
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1939)
    at
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412)
    at test.Test.doGet(Test.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    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(ThreadPoolExecutor.java:1142)
    at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:315)
    at
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2049)
    ... 27 more


As you can see in the end it says that caused by "No suitable driver"


My copy of context.xml is as follows-:

<Context>

  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxTotal="100" maxIdle="30" maxWaitMillis="10000"
               removeAbandonedOnBorrow="true"
               timeBetweenEvictionRunsMillis="10000"
               minEvictableIdleTimeMillis="6000"
               removeAbandonedOnMaintenance="true"
               logAbandoned="true"
               username="sreyan" password="sreyan"
driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/practice"/>

   <Realm className="org.apache.catalina.realm.DataSourceRealm"
   dataSourceName="jdbc/TestDB"
   userTable="users" userNameCol="user_id" userCredCol="user_pass"
   userRoleTable="user_roles" roleNameCol="role_name">

   <CredentialHandler className =
"org.apache.catalina.realm.SecretKeyCredentialHandler"
   algorithm = "PBEWITHHMACSHA384ANDAES_256"
   iterations = "111111"
   saltLength = "20" />

   </Realm>

</Context>


And my copy of web.xml is -:


<web-app>

  <display-name>Archetype Created Web Application</display-name>
  <servlet>
      <servlet-name>HibTest</servlet-name>
      <display-name>HibTest</display-name>
      <description></description>
      <servlet-class>test.HibTest</servlet-class>
  </servlet>
  <servlet>
      <servlet-name>Test</servlet-name>
      <display-name>Test</display-name>
      <description></description>
      <servlet-class>test.Test</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>HibTest</servlet-name>
      <url-pattern>/hib</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
      <servlet-name>Test</servlet-name>
      <url-pattern>/Test</url-pattern>
  </servlet-mapping>

  <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/TestDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
      </resource-ref>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>TECHERS</web-resource-name>
            <url-pattern>/protected/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>

        <auth-constraint>
            <role-name>TEACHER</role-name>
        </auth-constraint>
    </security-constraint>

    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/index.jsp</form-login-page>
            <form-error-page>/index.jsp?error=true</form-error-page>
        </form-login-config>
    </login-config>

    <security-role>
        <role-name>TEACHER</role-name>
    </security-role>
</web-app>


I have used the Maven webapp archetype for creating the project.

How do I get Tomcat to load the driver at start up. Also I have put the
JDBC Driver at $Catalina_Home/lib. But even this has no help. All my other
projects that don't use Maven can use connection pooling perfectly with the
above configurations.

Where am I going wrong ? Where do you keep the <resource-ref> declaration ?
After the servlet mappings ?

Any help would be greatly appreciated.

Regards
Sreyan Chakravarty

Re: Tomcat can't find JDBC Driver listed under Maven

Posted by Sreyan Chakravarty <sr...@gmail.com>.
Thanks Christopher but I have solved the problem. It seems when you are
using the Maven archetype for webapp you have to manually create a META-INF
in the /src/main/webapp directory and place the context.xml there.

And I have partly solved the issue. Will get back to you.

Regards
Sreyan Chakravarty

On Sat, Aug 29, 2015 at 1:02 AM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> Sreyan,
>
> On 8/28/15 3:11 PM, Sreyan Chakravarty wrote:
> > Okay I am trying to use connection pooling for my project which
> > uses Maven as a build tool. Now the JDBC Driver is listed as a
> > maven dependency. Tomcat however cant find it when I try to do
> >
> > ds.getConnection (ie. get a JDBC connection from a datasource).
> >
> > The error that I am getting is -:
> >
> > java.sql.SQLException: Cannot create JDBC driver of class '' for
> > connect URL 'null'
> >
> > [snip]
> >
> > Caused by: java.sql.SQLException: No suitable driver at
> > java.sql.DriverManager.getDriver(DriverManager.java:315) at
> > org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(B
> asicDataSource.java:2049)
> >
> >
> ... 27 more
> >
> > As you can see in the end it says that caused by "No suitable
> > driver"
>
> Where is your mysql-*.jar file?
>
> > <Context>
> >
> > <Resource name="jdbc/TestDB" auth="Container"
> > type="javax.sql.DataSource" maxTotal="100" maxIdle="30"
> > maxWaitMillis="10000" removeAbandonedOnBorrow="true"
> > timeBetweenEvictionRunsMillis="10000"
> > minEvictableIdleTimeMillis="6000"
> > removeAbandonedOnMaintenance="true" logAbandoned="true"
> > username="sreyan" password="sreyan"
> > driverClassName="com.mysql.jdbc.Driver"
> > url="jdbc:mysql://localhost:3306/practice"/> <Realm
> > className="org.apache.catalina.realm.DataSourceRealm"
> > dataSourceName="jdbc/TestDB" userTable="users"
> > userNameCol="user_id" userCredCol="user_pass"
> > userRoleTable="user_roles" roleNameCol="role_name">
> >
> > <CredentialHandler className =
> > "org.apache.catalina.realm.SecretKeyCredentialHandler" algorithm =
> > "PBEWITHHMACSHA384ANDAES_256" iterations = "111111" saltLength =
> > "20" />
> >
> > </Realm> </Context>
>
> I'm glad you got the CredentialHandler stuff working (or did you?).
>
> NOTE: your <Realm> is going to need to have this attribute added to it
> in order to work:
>
>    localDataSource="true"
>
> > I have used the Maven webapp archetype for creating the project.
> >
> > How do I get Tomcat to load the driver at start up. Also I have put
> > the JDBC Driver at $Catalina_Home/lib. But even this has no help.
> > All my other projects that don't use Maven can use connection
> > pooling perfectly with the above configurations.
>
> Do you have the JDBC driver in both places (lib/ and
> webapps/[app]/WEB-INF/lib) or just in Tomcat's lib/ directory?
>
> > Where am I going wrong ? Where do you keep the <resource-ref>
> > declaration ? After the servlet mappings ?
>
> I've found that <resource-ref> is entirely unnecessary, but that may
> be because you have declared the <Resource> in your app's
> META-INF/context.xml file, so your application already knows it's local.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Comment: GPGTools - http://gpgtools.org
>
> iQIcBAEBCAAGBQJV4LdEAAoJEBzwKT+lPKRYHr4P/3Ewp4bFEOSU7SG17nzmMK9Q
> YpT8g+Zd8AFuF5uKH/IjogFMaZD5wgYalImjMxp1/KzgT3XULK0YWfd1NZcK276i
> kxXNEMB7ybjmbETASVGh/BDrCNn/v6GPv1TCI/mrjsrcP4iRHeUrLiMjfk9KxdaO
> n2kmn770bvBmYn/VvILu9KTLF1rWh/aY75mdUc2q5mtyqdC7dg+Ky0aeZCdQj5yM
> yxJCInN8ntk/UFIO2ycDoosgdQIvA9c0+4VluKJ7tGLSc0QRrEaLjNnAHn3J/evP
> jpUQTSnhgD9G9dzjVNEtNcwTFbPNemdItpSVFPHB3p6An5sE6uKY4hkTiCDAwTk5
> 4cp8fkpabVvSz0+FEYZ9FSOqeun2Bn61bJTqPM4Yk9Z9dfC92CcKkC8tnjnVyPaK
> DGgMxEFFZgBza9BZAtcGWn3CzOWBQvRJCiK/xm02dQKHYyszA33W/h9pGB1hIWJT
> hePUusot0w9p8IgDhFh4+0cTkTNAVUnIQWnPuHsdUn5Itc0qql1LDWRdhWlQMc6W
> tp3Oqt7noSnoZpIa2mLimXW/36QOkm1nxZl3urnbTlsxPuAmfrEt/ndfIoWPlhcN
> 1jbAF7Kh4k4wdBbu4SNptl1xqb2GVD3f+BcuSQr3s2OkYd8fXQqFzIDjS9JzWvD5
> iX2CYicMQroQhgS/06MQ
> =oogU
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Tomcat can't find JDBC Driver listed under Maven

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Sreyan,

On 8/28/15 3:11 PM, Sreyan Chakravarty wrote:
> Okay I am trying to use connection pooling for my project which
> uses Maven as a build tool. Now the JDBC Driver is listed as a
> maven dependency. Tomcat however cant find it when I try to do
> 
> ds.getConnection (ie. get a JDBC connection from a datasource).
> 
> The error that I am getting is -:
> 
> java.sql.SQLException: Cannot create JDBC driver of class '' for
> connect URL 'null'
> 
> [snip]
> 
> Caused by: java.sql.SQLException: No suitable driver at
> java.sql.DriverManager.getDriver(DriverManager.java:315) at 
> org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(B
asicDataSource.java:2049)
>
> 
... 27 more
> 
> As you can see in the end it says that caused by "No suitable
> driver"

Where is your mysql-*.jar file?

> <Context>
> 
> <Resource name="jdbc/TestDB" auth="Container"
> type="javax.sql.DataSource" maxTotal="100" maxIdle="30"
> maxWaitMillis="10000" removeAbandonedOnBorrow="true" 
> timeBetweenEvictionRunsMillis="10000" 
> minEvictableIdleTimeMillis="6000" 
> removeAbandonedOnMaintenance="true" logAbandoned="true" 
> username="sreyan" password="sreyan" 
> driverClassName="com.mysql.jdbc.Driver" 
> url="jdbc:mysql://localhost:3306/practice"/> <Realm
> className="org.apache.catalina.realm.DataSourceRealm" 
> dataSourceName="jdbc/TestDB" userTable="users"
> userNameCol="user_id" userCredCol="user_pass" 
> userRoleTable="user_roles" roleNameCol="role_name">
> 
> <CredentialHandler className = 
> "org.apache.catalina.realm.SecretKeyCredentialHandler" algorithm =
> "PBEWITHHMACSHA384ANDAES_256" iterations = "111111" saltLength =
> "20" />
> 
> </Realm> </Context>

I'm glad you got the CredentialHandler stuff working (or did you?).

NOTE: your <Realm> is going to need to have this attribute added to it
in order to work:

   localDataSource="true"

> I have used the Maven webapp archetype for creating the project.
> 
> How do I get Tomcat to load the driver at start up. Also I have put
> the JDBC Driver at $Catalina_Home/lib. But even this has no help.
> All my other projects that don't use Maven can use connection
> pooling perfectly with the above configurations.

Do you have the JDBC driver in both places (lib/ and
webapps/[app]/WEB-INF/lib) or just in Tomcat's lib/ directory?

> Where am I going wrong ? Where do you keep the <resource-ref>
> declaration ? After the servlet mappings ?

I've found that <resource-ref> is entirely unnecessary, but that may
be because you have declared the <Resource> in your app's
META-INF/context.xml file, so your application already knows it's local.

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJV4LdEAAoJEBzwKT+lPKRYHr4P/3Ewp4bFEOSU7SG17nzmMK9Q
YpT8g+Zd8AFuF5uKH/IjogFMaZD5wgYalImjMxp1/KzgT3XULK0YWfd1NZcK276i
kxXNEMB7ybjmbETASVGh/BDrCNn/v6GPv1TCI/mrjsrcP4iRHeUrLiMjfk9KxdaO
n2kmn770bvBmYn/VvILu9KTLF1rWh/aY75mdUc2q5mtyqdC7dg+Ky0aeZCdQj5yM
yxJCInN8ntk/UFIO2ycDoosgdQIvA9c0+4VluKJ7tGLSc0QRrEaLjNnAHn3J/evP
jpUQTSnhgD9G9dzjVNEtNcwTFbPNemdItpSVFPHB3p6An5sE6uKY4hkTiCDAwTk5
4cp8fkpabVvSz0+FEYZ9FSOqeun2Bn61bJTqPM4Yk9Z9dfC92CcKkC8tnjnVyPaK
DGgMxEFFZgBza9BZAtcGWn3CzOWBQvRJCiK/xm02dQKHYyszA33W/h9pGB1hIWJT
hePUusot0w9p8IgDhFh4+0cTkTNAVUnIQWnPuHsdUn5Itc0qql1LDWRdhWlQMc6W
tp3Oqt7noSnoZpIa2mLimXW/36QOkm1nxZl3urnbTlsxPuAmfrEt/ndfIoWPlhcN
1jbAF7Kh4k4wdBbu4SNptl1xqb2GVD3f+BcuSQr3s2OkYd8fXQqFzIDjS9JzWvD5
iX2CYicMQroQhgS/06MQ
=oogU
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org