0

I would like to know your thoughts on whether this type of methodology could be practically implemented. I am very knew to database pooling, so forgive me for any misunderstandings. I have multiple classes calling a dbData class (which connects to a database and has helper functions, like frequently uses gets and updates and etc). I need to keep all the get and update functions in dbData class, but keep a pool of active connections I can re-use. Which means I will instantiate the dbData class multiple times, each time checking whether an appropriate connection exists, if not, create a new one and put it into the pool.

My question is this how and where you would store this pool. I could perhaps accomplish this if dbData would not be instantiated more than once and keeps one persistent pool object. But in my case, I have multiple dbData instances which should all connect to a single pool. I thought about serializing the pool object, but this seems ridiculous. Is this possible (what is shown in the pic)? It seems I am having trouble with the object-oriented part of this design.

The applications uses multithreading with Class1 and Class2.

I would not like to use any external libraries if possible.

db pool img

5
  • Are you running this in a container environment (e.g. on Tomcat, JBoss, ..) or as a standalone Java application? Commented Nov 17, 2011 at 20:36
  • Why are you trying to avoid existing libraries/methodologies? This is an essentially solved problem, through libraries or containers. If you're dead-set on reinventing it, take a look at commons-dbcp, built on commons-pool, for ideas on how to re-implement it :/ Commented Nov 17, 2011 at 20:47
  • Standalone Java app. I would not like to use existing libraries to keep it simple. I read some IBM documentation on Java DBCP and it essentially looks like just a HashSet with connections. I don't think I need to go all out and get a huge library. Commented Nov 17, 2011 at 20:52
  • It seems I forgot about static variables. I'll just make this pool static and keep querying it for connections even though there are multiple instances of dbData. Commented Nov 17, 2011 at 20:57
  • Keeping it simple IS using those other libraries, not writing your own. Commented Nov 18, 2011 at 0:39

2 Answers 2

1

If it is a standalone app, then I would create a seperate service with a static collection that keeps the connection and does all the handling of those. Then the dbData class can call the static method to get a connection. The service then itself takes care of creating new connections if required. If your dbData instances are running in parallel you have to think about synchronized access (if required).

Sign up to request clarification or add additional context in comments.

4 Comments

Realized it the same time you did - a static synchronized pool. I just find it a waste to use a 7MB library for a thing you can implement yourself with some thinking. Thanks.
It is probably a waste, but on the other side reinventing the wheel? :-) Of course it makes fun, but could lead to errors which others already fought.
You're right. There is an also an issue with licensing due to the fact this will used for commericial purposes.
You would have no issues with the commons-dbcp license.
0

Have you considered using the singleton design pattern?
http://en.wikipedia.org/wiki/Singleton_pattern
http://java.sun.com/developer/technicalArticles/Programming/singletons/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.