Historical context: This problem was not at all what I thought. The reason and solution is given below, but the original publication is left for reference.
I develop a simple structure for periodically polling the directory for .properties files, then execute SQL queries and send emails based on their configurations. Since each .properties file has the same range of operations, they are all interpreted by the same Task class. But since each of them represents different logical operations, each of them receives separate log files.
This is achieved by sharing a single instance of the log4j RollingFileAppender and dynamically changing its output file based on the value in the .properties file. Since this is a single-threaded application, it works great.
However, I noticed that in certain situations, this RollingFileAppender will become closed, and the application will continue to forget, except that no protocols are currently happening. I only managed to catch this in action once, thanks to the output to the console, since this service usually works as a background process on a Linux server. Here's what happened:1) StartScheduler, the main class, creates a new instance of TaskPoller every minute.
2) TaskPoller scans the directory, downloads a little information from each .properties file, and determines if it should be run. It also has its own separate RollingFileAppender, which it retrieves through Logger.getLogger (TaskPoller.class). If the task is to be started, it creates an instance of the Task object, passing it to the specific .properties file that must be started.
3) The task gets its RollingFileAppender, and then calls the fileAppender.setFile ("newtaskname.log") and fileAppender.activateOptions () to change the location of the output file. Then, during its execution, something like this happens:
[TaskPoller]
...
task = new Task(fileName);
if (!task.Execute())
logger.warn(fileName + " returned with an error code.");
[Task.Execute]
...
try {
dbDAO.Connect();
} catch (Exception e) {
logger.fatal{"Database connection error.", e};
return false;
}
[DBDAO.Connect throws SQLException, ClassNotFoundException]
...
try {
Class.forName(dbDriver);
connection = DriverManager.getConnection(urlString, userName, password);
} catch (SQLException e) {
if (connection != null)
try { connection.close(); } catch (Exception e2) { ; }
throw e;
}
DBDAO.Connect(), com.mysql.jdbc.exceptions.jdbc4.CommunicationsException( - , jdbc ). Connect(), Execute().
- Task RollingFileAppender . , , , , , Connect(). , log4j Appender.
, , , ?
- Edit--
, ; - , , TaskPoller , log4j. , [ ] [1], , . , , - , .