This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton problem  RSS feed

 
Axel Leucht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Johnny Barbosa
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!