Singleton or SingleThreadModel

M

Matthias Kranz

Hi,
I'm writing an webapp in which users can create something like a diary. They
can use it alone or together with some other users. When the start creating
the diary my webapp creates an XML-file for every diary. It is only allowed
to add data, not to change or delete.
To add the users data to the XML-files I use some servlets, corresponding to
the context/JSP from where the user adds his data. All these servlets use
the same class to write the data into the XML-file.

I'm not sure how to implement the synchronisation. Should I use a Singleton
pattern or the SingleThreadModel? And if I have implemented this my webapp
is safe?
Can someone please give me some hints?

Thanks in advance!

Greets Matze
 
J

John C. Bollinger

Matthias said:
Hi,
I'm writing an webapp in which users can create something like a diary. They
can use it alone or together with some other users. When the start creating
the diary my webapp creates an XML-file for every diary. It is only allowed
to add data, not to change or delete.
To add the users data to the XML-files I use some servlets, corresponding to
the context/JSP from where the user adds his data. All these servlets use
the same class to write the data into the XML-file.

I'm not sure how to implement the synchronisation. Should I use a Singleton
pattern or the SingleThreadModel? And if I have implemented this my webapp
is safe?
Can someone please give me some hints?

Since you have multiple servlets potentially adding data to the same
file at (about) the same time, SingleThreadModel doesn't help you. (It
is scoped to the servlet, not the whole webapp.) SingleThreadModel is
not relevant to ancillary objects that your servlets may use.

A Singleton is probably not the best answer either, presuming that you
want to support multiple different diaries. My recommendation is that
you have (at most) one instance of your file updater class per diary,
which object will manage synchronization of concurrent writes to the
same file. Then implement a factory method or even a whole factory
class by which your servlets obtain the correct file updater instance
for the diary they are updating. You would need to store the active
updater instances in some kind of data structure where they could be
retrieved upon request; a HashMap based on the file's full pathname
would probably work.

John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top