aspose file tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Singleton problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Reply Bookmark "Singleton problem" Watch "Singleton problem" New topic
Author

Singleton problem

Axel Leucht
Greenhorn

Joined: Jan 19, 2009
Posts: 21
I'm scratching my head with a problem I have.

My Data class is a singleton class. This seems natural to me as only one object should be instantiated for one database file and the single object is responsible for all file locking issues.

But this leads to difficulties with my junit test cases. In some of them I want to be able to connect to different database files. Definitely not a singleton. I can change the implementation to have a singleton object per database file, but I wonder if this is worth the effort as the application can only connect/operate on one database file.

I wonder how you solved the problem. I currently worked around it with a constructor which bypasses the already instamtiated object and creates a new one instead. Though this works, the implementation doesn't smell right to me, does it?

I'd appreciate if someone can share his/her ideas with me.


Regards

SCJP, SCJD
K. Tsang
Ranch Hand

Joined: Sep 13, 2007
Posts: 1222

Howdy champ,

My Data class is a singleton too. Now about your testing... why you need to connect to different db files? The server can instantiate only one file per server port hence only one server per database for SCJD. If you really want to you can use different server ports for different files but again your clients can only connect to one (assuming they can't change files/port within the app).

Ultimately your JUnit tests should test your locking and other things. From the locking perspective, you are really doing a stress test too. Many clients connect to the database (everyone gets the same file hence singleton).


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD
Johnny Barbosa
Greenhorn

Joined: Sep 29, 2009
Posts: 26
My Data class was not a singleton, but only my DataBlocking class.
I think about Data class, as DB connection point. So, each client must have your own instance of connection.


Cheers,
Johnny Barbosa
SCJA, SCJP, SCWCD, SCJD(Story | Relato), SCBCD (coming soon)
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Singleton problem
 
Similar Threads
Data class Singleton question
Urgent, please help, reading remote Data
URLyBird - Question about Singleton pattern
Question from Andrew's book
NX: Notes on a design that passed 389/400