JAVA Initializing database environment
Having globals is considered bad idea...
What is the best practice to have it created one time only ?
Should i use Singleton pattern (enum implementation) or static , or another solution?
Neither the Singleton anti-pattern nor static final is a really good idea. Your environment will be created once (I guess during startup of your system). This environment should be available to your system and your database manager can be passed to your DAOs from there.
Ideally, you'll want to use dependency injection to achieve this in the neatest way, such as that provided by the Spring framework.
Singleton pattern is best.
Because even due to some reason if the environment or any object becomes null we can check in the getInstance() for null.
And if it is null we can create one. So every time there will be only one object.
精彩评论