should a db connection be a singleton?

前端 未结 4 1886
青春惊慌失措
青春惊慌失措 2020-12-01 03:25

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

4条回答
  •  青春惊慌失措
    2020-12-01 03:32

    Singletons are a pattern - there's no explicit way to create one, you just follow the design practice.

    So, if you're using a database that can handle concurrent reads/writes (i.e. MySQL), you don't need to worry so much about thread safety. If you're using a DB that's doesn't do concurrent writes well (SQLite), then a singleton should theoretically work.

提交回复
热议问题