Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Jan Cumps
Bartender
Posts: 2598
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2598
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2598
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic