开发者

JAVA Initializing database environment

开发者 https://www.devze.com 2023-03-05 03:30 出处:网络
I am using berkeley database. You should open an environment (it is just some files in order to support locking etc) 开发者_StackOverflow中文版once and then using that environment open your database

I am using berkeley database. You should open an environment (it is just some files in order to support locking etc) 开发者_StackOverflow中文版once and then using that environment open your database files...

The problem here is that Environment should be created once..

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消