JdbcConnectionPool

33 views
Skip to first unread message

Ryan How

unread,
Jun 5, 2012, 4:03:44 AM6/5/12
to h2-da...@googlegroups.com
Hi,

I was having a quick look through the source of JdbcConnectionPool, but
I was unable to determine if connections are automatically returned to
the pool if close is not called.

So if I don't call close on the connection, and the thread with the
connection ends, will the garbage collector automatically return it to
the pool when it feels like it?

If it doesn't I might want to put a safety on the connection pool so
accidentally not calling close doesn't take down the whole application
eventually...

Thanks, Ryan


Nate Sammons

unread,
Jun 7, 2012, 5:30:38 PM6/7/12
to h2-da...@googlegroups.com
It's extremely unlikely that the GC will ever even see that connection
-- because the pool is still aware of it (it knows you checked it
out), so it probably will never be collected. It's way better for you
to make sure you close your result sets, statements and connections in
a finally block. Calling close() is how pools know you're done with
the connection.

DBCP (and other pools) have support for finding connections that have
been checked out of the pool but never returned, but you should never
rely on this for "normal" use.

If you don't/can't make sure to close those connections, consider
using a framework like iBatis or Spring JDBC Template, which take care
of all that stuff for you. They are not as heavy as Hibernate, and
take care of just the annoying details like making sure you close
connections.

-nate



"The sky calls to us. If we do not destroy ourselves, we will one day
venture to the stars." -- Carl Sagan
> --
> You received this message because you are subscribed to the Google Groups
> "H2 Database" group.
> To post to this group, send email to h2-da...@googlegroups.com.
> To unsubscribe from this group, send email to
> h2-database...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/h2-database?hl=en.
>

Ryan How

unread,
Jun 8, 2012, 2:57:26 AM6/8/12
to h2-da...@googlegroups.com
Thanks, I'll just have to be careful that all connections are closed.
I've got a web app that has a connection per request and closes them at
the end, but for some background tasks it doesn't use that framework.
Maybe I should implement some kind of thread factory which makes sure
the connection is cleaned up when the thread ends, and deny a connection
being given unless it is for a thread that is part of that thread group.
Reply all
Reply to author
Forward
0 new messages