-1

Can you please explain how singleton is use full for creating database connection?
How it will work in multithreaded environment while creating db connection and closing the connection? Once connection is closed will it gets disconnect from database?

Vinod Kumar Y S
  • 628
  • 3
  • 9
rama
  • 173
  • 1
  • 2
  • 7
  • We can't explain you how it's useful because in fact it's useless: it creates coupling and makes the code difficult to test. As you figured out, extra care has to be taken in order to ensure singleton creation is thread safe. You should lookup "Dependency Injection". Good luck, the journey begins here :) – Gregory Pakosz Sep 08 '13 at 09:35

1 Answers1

2

You should normally use a "connection pool" when managing database connections in your Java applications. Creating connections is expensive / heavy so you really don't want to be creating them time and time again, especially for a busy site (the performance drop will kill it).

The manner in which you acquire the pool reference depends on the type of application (managed like servlet/JSP or a standalone application) but search around for "datasource". Also have a look at this answer.

Community
  • 1
  • 1
Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71