0

My goal is to enhance a working set of simple REST services by a service, that returns the result from a SQL query issued by plain JDBC to a MySQL database.

The REST service works fine on Tomcat without accessing the database.
A class works fine as a stand alone app, which accesses the MySQL data base on a remote server. The class provides a get function returning a string.
When I call that function from a REST service to return the string, I get the no suitable driver found error.

The MySQL driver has been deployed by Tomcat on Tomcat/webapps/app/WEB-INF/lib from the war created by the spring-boot framework using Maven. That is why I think Tomcat should be able to find it.

What I tried so far (restarted Tomcat every time):

  • Copied the MySQL driver form Tomcat/webapps/app/WEB-INF/lib to Tomcat/lib.
  • Filled application.properties with spring.datasource.url, username and password, as directed by SpringBoot documentation.
  • Defined the MySQL resource in Tomcat/webapps/app/META-INF/context.xml and the resource-ref in Tomcat/conf/web.xml.

Still getting the "no suitable driver found" error.

Versions: Win-10, Java-11, Maven 3.8.4, Tomcat-10. For the spring-boot-starter-web and -tomcat dependencies and the spring-boot-maven-plugin, there are no version numbers in the pom.xml.

Should it work that way (sure, I searched for typos and syntax failures)?
Or did I miss something for Tomcat to reach the MySQL database?

ngong
  • 754
  • 1
  • 8
  • 23
  • Adding ''Class.forName("com.mysql.jdbc.Driver");'' in the constructor of my enhancement class solved it, as [already answered](https://stackoverflow.com/questions/71145077/enhance-springboot-rest-services-by-jdbcmysql-no-suitable-driver-found). I did not need any spring.datasource... in application.properties, nor changing of Tomcat/conf/web.xml or Tomcat/webapps/META-INF/context.xml. Thanks to the closer of this task. – ngong Feb 18 '22 at 04:28

1 Answers1

0

Do you specify the scope provided for the Tomcat library in Maven?

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
</dependency>

Maybe also Mysql Connector is missing

 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
 </dependency>

And in the application.properties:

spring.datasource.driverClassName=com.mysql.jdbc.Driver
oaPiet
  • 116
  • 4
  • yes, oaPiet - already with https://start.spring.io/ for other REST services, not specifically for JDBC to work. Do you think it should work that way? I got to ask, because it is my first trial. – ngong Feb 16 '22 at 16:50
  • @ngong Oh I see, please add your pom.xml to see what's going on exactly. Maybe a dependency is not declared in it. – oaPiet Feb 16 '22 at 17:00
  • Which dependency do you think is missing? If you think it should work the way I described, than I have to strip down and anonymize the project prior to providing details. I will, If no obvious omission can be seen. – ngong Feb 16 '22 at 17:40
  • @ngong I edited my answer with a few information that you can try it – oaPiet Feb 16 '22 at 18:07
  • driverClassName line was missing in application.properties. But "no suitable driver found" remains. With logging=INFO in app.properties I got a hint vom Tomcat: _java.util.ArrayList.forEach Name =_ _Ignoring unknown property: value of "MySQL Datasource" for "description" property_ – ngong Feb 17 '22 at 06:13