aspose file tools*
The moose likes JDBC and the fly likes A connection taken from Statement != used for creation it at pooling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "A connection taken from Statement != used for creation it at pooling" Watch "A connection taken from Statement != used for creation it at pooling" New topic
Author

A connection taken from Statement != used for creation it at pooling

D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

I use Apache connection pool. Generally I close a connection after usage. Sometimes I can't reach an original connection, so I get it from a statement and close. I noticed that an original connection and one gotten from statement are not equal. Why it's so? Is any risk of connections leak when when I close connection from statement ?


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2516
    
  10

I noticed that an original connection and one gotten from statement are not equal.

If you're using Commons pooling,
the original one is a wrapped connection. The close() method is overridden to return the connection to the pool, in stead of closing the database connection.

When you're using the connection from the statement, it depends on the jdbc implementation. You might get the non-wrapped connection.

Closing it would not return the connection to the pool, and close the actual database connection.

You could test the scenario by creating a pool with one connection,
- Get connection from the pool,
- create a statement,
- get the connection from the statement
- close that connection.
- Get a connection from the pool.

If the last step fails, you have a leak.

Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Yes, I use Commons. I remember when I implemented a pool, I had a background process watching for closed wrapped connections and removed them from pool. I'm expecting a similar mechanism from Commons. Do you know if there any?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2516
    
  10

No.
You can provide a validation query.
Commons uses it to test the state of your connection before handing it over.
But there is (as far as I know) no mechanism to revoke wrapper connections that have not been returned to the pool.

Regards, Jan
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Ok, so it shouldn't create a problem. If a pool forgets about given connection, then they will be picked up by GC.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2516
    
  10

Originally posted by D Rog:
Ok, so it shouldn't create a problem. If a pool forgets about given connection, then they will be picked up by GC.


You might run out of pool connections if you don't return them as expected, by closing the wrapping connection.

Regards, Jan
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

It will happen if a pool keeps a track of given connections, if not, then it should not be a problem. Anyway, thank you for tips.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A connection taken from Statement != used for creation it at pooling