I am creating an application that will use a connection pool for database connections and I want to ensure that only one connection pool is created for the whole application to use. I was going to create a singleton to ensure there could be one connection pool ever opened, but many people suggest avoiding singletons. What are my alternatives, and what would that look like to the clients calling my connection manager?
Asked
Active
Viewed 62 times
0
-
1what abt datasource - which environment you are working on – Mani May 01 '14 at 18:06
-
This is a standard jar w/ main not running in any containers. – yellavon May 01 '14 at 18:09
-
check here http://stackoverflow.com/questions/2835090/jdbc-connection-pooling – Mani May 01 '14 at 18:18
1 Answers
1
If you are running standalone application , I dont see an problem with Singleton. It is purely depends upon how you are ensuring it is single instance.
IMHO Enum is the best of creating SingleTon . And Off course , I assume you are handling proper synchronization on your methods to avoid same connection shared multiple clients and unwanted usecase
You dont need to reinvent the wheel , you can use Apache DBCP Connection Pool and the sample
One answer from SO
If you are running in Application servers , then singleton is overkill . In App servers Singleton is not guarantee due to clustering. It is guarantee only per JVM . It is advise to use the datasources for the connection pool.