I have a Java application running on Tomcat 9. One of the dependecies of this project is mysql-connector-java-5.1.18-bin
(declared on pom.xml
), as the program accesses a MySQL database on certain conditions.
There's no problem when the database is accessed on doGet
method (that is, on a request).
Upon development, however, I had to create a job that runs every 30 minutes and is started on server start. This method accesses the database.
The method is called and fixed to run every 30 minutes the following way (as showed on this question):
public void contextInitialized(ServletContextEvent arg0) {
// Tasks done on server start
Timer t = new Timer();
MyTask mTask = new MyTask();
// Runs every 30 minutes
t.scheduleAtFixedRate(mTask, 0, 1800000);
}
And declared on web.xml as follows:
<listener>
<listener-class>
timed.MyServletContextListener
</listener-class>
</listener>
However, upon start and on following loops, I get this exception:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/iot
HOWEVER, after the first request is made to the servlet (via a doGet()
), this error disappears, and the job manages to run properly.
It seems that the servlet can't find the dependencies until a request is made. Is there a way do go around this? Or do I have to make a request first?
EDIT
Here's the web.xml
file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>IOTServer</display-name>
<context-param>
<param-name>AppID</param-name>
<param-value>123</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>default.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
timed.MyServletContextListener
</listener-class>
</listener>
</web-app>
And here's the servlet class:
@WebServlet(value="/iotIN", loadOnStartup=1)
public class IoTIN extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// make the necessary operations
}