Quoting Janet ([EMAIL PROTECTED]):
> Now I'm writing a servlet, say MyServlet, whose instances need to write some
> information to a log file of my own. And also they need to access (get and
> modify) an object containing information for all of them. So I think I
> should do some synchronization operations of this shared object and of
> writing to the same log file.
...
> And when it comes to writing to the same file, how can I assure there is
> just one instance accessing the file at one time?
Janet,
First, the obvious point that I'm sure others will raise: you can just
put a static member in your servlet, and ensure that it's initialized
when your servlet is. You could reference count it: increment every
time a new servlet is created, and decrement every time one is
destroyed. When the last one is destroyed close the file.
Second, the less obvious point. If you synchronize access to your
log file then you have to consider contention. You say you want to
ensure that only one instance accesses the log file at a time: well,
if you hardly ever write to the log, that's OK. But if you are
writing to it on every request then this will possibly hinder
your scalability.
If you think scalability is an issue consider creating some kind of
log queue or buffer that you append to, and then flush only every
once in awhile. That would help your scalability and also IO performance.
But of course you need to consider whether you can accept that the
log message might not be written out in the event of a server crash.
Anyway, I hope this was helpful.
Justin
--
WebMacro Servlet Framework: http://webmacro.org
[EMAIL PROTECTED]
AltaVista Company
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html