Tuesday, December 13, 2005

log4j DailyRollingFileAppender

Wanted to log messages in my J2EE web application in new log file everyday.
Log4j is the best option for that. (BEST.. because I know how to use it :))
Using "DailyRollingFileAppender" is the solution.

It is not as simple as it seems to be.
I conveniently made one log4j.xml, put it in /WEB-INF/classes and started working on other things.
Every morning I saw some weird error message saying something like
log4j:ERROR Failed to rename [logfilename] to [logfilename.2005.12.11]
I also observed that, NO new log file is created.

Searching on the net suggested that error is because of “multiple processes accessing same file”.
Because of multiple processes, accessing my log file, renaming was not possible.

We need to deinitialize/initialize logging mechanism every time we restart the server to terminate processes accessing log files.
To achieve this, I wrote one small listener implementing javax.servlet.ServletContextListener interface.
Everytime when context was destroyed, I shutdown log4j’s logging mechanism using org.apache.log4j.LogManager.shutdown() method.
Shutting down the mechanism made sure that all the processes that are using log files are terminated and this way resolved the issue.

Try it and do let me know whether this solved your problem or not.
Please let me know if there is better solution for this.

Thanks.

Wednesday, October 12, 2005

Tomcat lost connection with MySQL server

While working on a project with Tomcat server and MySQL database, I got an exception saying something like "Lost connection to MySQL server".
This problem I faced every morning at the start of a day.
i.e. If I use my application before leaving for a day and if server was left RUNNING, on the next morning when I try to use application again, above exception was thrown.

Problem here was, MySQL's variable "interactive_timeout".
Definition of "interactive_timeout" from MySQL manual is as follows -
"The number of seconds the server waits for activity on an interactive connection before closing it."
This variable's default value is 28800 i.e. 8 hrs.
So when I kept my server running all the night, no interaction was there on connection and server was closing the connection.
This connection was tried to be used by my application and hence above exception was thrown.

Some of the solutions to this problem are
1. If you are using connection pooling, keep the connection interactive so that it is never idle for ${interactive_timeout} seconds
2. Change the value of variable on MySQL server as per your requirement (This solution, I think, is not the best solution)
3. Before firing any query to server, check whether connection is active or not

Drop a comment to let me know whether this helped you in solving the problem.

Thanks.

Tuesday, October 04, 2005

Tomcat /servlet problem

After coding and deploying my first servlet application on Tomcat 5.0, I entered following URL in browser to test the servlet.

http://machineName:portNumber/webapplication/servlet/servletName

This did not work and I got "HTTP 404" error.
I tried to run same servlet application on Resin and it worked.

After googling a little, I came to know that the file
"installationDirectory"\Apache Software Foundation\Tomcat 5.0\conf\web.xml
is responsible for the mapping "/servlet/servletName" to our servlet.

In newer Tomcat versions, this mapping is disabled for some reason I don’t know.

To enable this /servlet/* mapping, you have to uncomment two things:
- servlet "invoker" in <servlet> and
- servlet mapping for /servlet/* which is mapped to "invoker" servlet in <servlet-mapping>

After uncommenting these two things, restart the server and things should go fine.

Do let me know if this doesn’t work.

If anybody have any idea about why /servlet/* mapping is disabled in Tomcat's latest versions, drop me your comment.

Introduction

Hi,
If you are here to read something philosophical, some of my past experiences, some views of mine on world issues, good English then this blog is not for you.

I am a software engineer and I am creating this for my fellow mates.

In our everyday life, we face lot of problems while coding, configuring servers, logging data.. etc. Here I will share all the problems I faced, while working, along with its solution.
I am in no way responsible if anything goes wrong in your system while you are trying any of the solutions written here.

You can refer this only as a guideline to your problem and solutions, stated here, may differ with version of your APIs or platform you are working on.
I will try my best to put all the version details in my blog while stating problems and while giving solutions to them.

Thanks.