Hi all, I'm working the Urlybird developer project and I've been reading alot of posts about how people implemented a singleton for the Data class (the class that actually implemented readRecord, updateRecord, etc).
I did not do that in my design and I'm wondering if that's a big mistake. Each client that I spin up calls on a factory to return either a local database connection or a remote one. Each client effectively get's its own connection to the database file, hence each client gets its own copy of the Data class. Is there a problem with that?
any help would be deeply appreciated. thanks in advance.
I didn't apply Singleton pattern to my Data class too. I think it is acceptable if each network client has its own Data object as long as these Data objects use a common object for tracking the data that another client is locking. For me, I have another class called "DatabaseFileManager" which is Singleton and returns the HashMap object that is used to track all locked records. Of course this HashMap object is commonly used by my network clients. We don't lock the Data object but instead we lock this HashMap object, then it's supposed to have only one single HashMap object. Hope this helps.
Regards, Ken Kirin SCJP SWCD SCJD(B&S In progress)
I chose to make the Data class singleton, but I know people who have chosen not to. As long as you make sure you only have one instance of the actual object that holds the records (unless you aren't bothering with cacheing and always reading from the datafile), then you should be fine.
If I make the Data class as Singleton, Do I still need the sychronization at all in any method. Since only client can hold the Data object which will access the one file operation - read/write/update/delete, why do we need synchornization anyway?
Originally posted by Tommy Wan: If I make the Data class as Singleton, Do I still need the sychronization at all in any method. Since only client can hold the Data object which will access the one file operation - read/write/update/delete, why do we need synchornization anyway?
Implementing the Data class as a singleton makes sure there is only ONE Data class. It does not make sure that only one client can access the Data class at a time.
That is what you need the synchronization for, to avoid two clients entering the Data class lock operation (for instance) concurrently.
subject: does the Data class have to be a singleton?