Search code examples
javamysqllinuxupstart

Linux: Java program called from Upstart cannot access mysql database


I am working on a simple java background process that handles data from socket connections and then uses the mysql connector JDBC driver to establish a connection to a database where this info is stored. I want to use upstart to use the program as a service, but after initializing my service the process will fail out after the first upload tries to access the JDBC driver to insert data into the database (doesn't fail on jdbc register, however this is my best assumption from monitoring the output although a stacktrace is never shown in my logs). If I remove the exit call from the catch statement in the program I can watch the process continually receive the uploads but no data will actually be stored in the mysql database.

When running the java program normally from the console it executes without any issues. The section of the upstart .conf below points to the same program I can run without any problems.

script

echo $$ > /var/run/uploadclient.pid
exec java -cp /user/server UploadProgram >> /home/user/upload.log

end script

Has anyone heard of a problem like this before?


Solution

  • Think I got it figured out... When running a java program through upstart it hides the stacktrack error for some reason. This can be overcome by capturing the stacktrace with a StringWriter and PrinterWriter and then using a regular System.out.println() to print out the error.

    The problem ended up being that even though the original class was compiled with the jdbc connector upstart requires that it be in the classpath during execution.

    exec java -cp .:/user/jdbc.jar:/user/server UploadProgram >> /home/user/upload.log
    

    That solved the issue.