• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connection problem

 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to save all connection objects in hashmap in jdbc.but when ever i try to put connection object in map it does not store actual object.
my code is like this

Connection c=DriverManager.getConnection(url,"sa", "sql");
java.util.Map conPool=new HashMap();
conPool.put(c,"01");

but when i print HashMap it gives op as follows
sun.jdbc.odbc.JdbcOdbcConnection@c2ea3f 01

when according me it should print c 01
so how i will store c in hash map.plz help
thanks
 
Sarath Mohan
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's printing correctly!...

Connection object doesn't have a proper toString() method overridden. So it will print the default implementaion of toString method of Object class.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gowher,
I agree with Sarath. Just out of curiousity, why are you writing your own connection pool?

Also you probably want conPool.put("01" ,c); instead of conPool.put(c,"01");

That way you can look up the connection by the key ("01").
 
Arun Prasath
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can keep either of them as the Key in Map. As every connection instance is unique, that can be kept as key in the Map.
I use Connection as key and the status of the connection as the value in my Connection Pool.
Originally posted by Jeanne Boyarsky:
Gowher,
I agree with Sarath. Just out of curiousity, why are you writing your own connection pool?

Also you probably want conPool.put("01" ,c); instead of conPool.put(c,"01");

That way you can look up the connection by the key ("01").
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not implementing your connection pool as a queue.

The real connection pool should have some properties like,
- shrink
- extend


Now, how are you getting the connection from Map? By passing the connection object as a key? It means you already have the connection, you are not getting it from Map, right? So, what are you getting from the Map? Are you getting the status of that particular connection? Where are your connections reside?
 
Arun Prasath
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, how are you getting the connection from Map?
We call by DBConnectionPool.getConnection().
All the implementation is hided inside this method.

By passing the connection object as a key?
No
It means you already have the connection, you are not getting it from Map, right? So, what are you getting from the Map? Are you getting the status of that particular connection? Where are your connections reside?

We store the predefined amount of connection objects in a Map. We store the availability of the connection object as the Key(true/false). When the user requests for a new connection, We iterate over the map to get the first available connection and return the connection after changing its status(false).
When connection is released from pool, we change its status to be available. (true).

hope I am clear now..
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry mate, I couldn't understand you. You said in your previous post that you are using connection object as key, and now you are saying that you are using availablity of connection as a key (true/false). These are two different things. As true/false is not unique, how can you use it as a key.


Iterate over a map until a free connection is found. Its not at all a good idea. Why not implement data structure like queue or stack. Queue would be better.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Prasath:
We call by DBConnectionPool.getConnection().
All the implementation is hided inside this method.


This one is clear to me, and now i got you.
 
Arun Prasath
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confusion. The key is Connection. Value is Status of availability of Connection.
I have added the code here..



[ March 29, 2005: Message edited by: Arun Prasath ]
 
Arun Prasath
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adeel Ansari:
Why not implement data structure like queue or stack. Queue would be better.


Now I think that implementing connection pool as a Queue would be much more easier than what I have done. Thanks for pointing it though..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic