What is the best way in Java to create a singleton? Should a DB connection be a singleton (being a singleton it\'s automatically thread-safe)? Because theoretical the DB can
A DB connection should not normally be a Singleton.
Two reasons:
Instead of doing this consider a database pool. The pool is shared (and could be a singleton if you wanted). When you need to do database work your code does this:
getConnectioFromPool();
doWork()
closeConnection() // releases back to pool
Sample Pool Libraries: