• Post Reply Bookmark Topic Watch Topic
  • New Topic

class for storing Connections  RSS feed

 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though there are libraries like C3P0, Apache DBCP, Proxool or BoneCP (which i read from net) I decided to write my own connection pooling code. So, I have a small doubt of choosing class for storing connections i.e vector ,arraylist or any other. Since I want to create a multi threaded environment shall I go with Vector or Is there any better option?
--Thank YoU
-Rajeev
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a synchronised List (from the Collections class).
Or just go with Vector.

Writing your own pool you'll want a Connection wrapper of some sort so you can handle the closing of the connection so it returns it to the pool without actually closing it.
You also need to think about how to handle timed out connections, and what to do when the pool is empty.

That's just a couple of things existing pools handle.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope that's just for your education and not something you'll use for real-world applications. Otherwise, why?
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah it's for educating myself. I'm at learning stage.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I hope this isn't related to your other question about Singleton.
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier I thought users should not waste memory by creating new Connection Objects , so only I made a single Connection Object and made it reusable. By doing this(according to me) works fine and can be done in a single thread environment. But in case of multi threaded environment, I shouldn't make thread2 to wait until Thread1 releases the connection Object. After that I thought of multiple connections then I came to know about Connection Pooling .
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While writing code is probably the best way to learn, another way that complements "doing it yourself" is "reading what others have done". In the case of connection pools, there are many considerations to take into account to come up with a robust and reliable solution. I would suggest that before you go and try to write your own, you should at least read through the code in some of open source connection pool libraries. If it were me, I wouldn't even spend any time trying to write my own. But then again, I've never been one to be concerned too much with the plumbing details. I'd rather spend time solving other kinds of problems, of which there are already too many to handle without getting down and dirty at the levels where Connection Pools live.

Think of it this way: Do you want to learn how to fly the plane or do you want to learn how to be the engineer who designs and builds the plane's engine? If you'd rather be the pilot, then spend more time learning other things and let the engineer worry about the engine design. If you'd rather be the engineer, then go for it, but build on top of what other engineers have already done in the past.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!