Subject Re: [Firebird-Java] Re: Should (Could) FBStatements be held in WeakHashMap
Author Mark O'Donohue
Hi Roman

Roman Rokytskyy wrote:

> I'm afraid, this will not help. Mark had following problem:
> for (int i = 0; i < 40000; i++)
> Statement stmt = con.createStatement();
> On i = 20000 it will break because server runs out of statement
> handles. Mark suggested to use weak reference, so that when stmt
> becomes weakly reachable, garbage collector will invoke stmt.finalize
> () that in turn will close that statement (and release server
> resources). This happened within a method body.

This is a way of reproducing the problem, but where I'd like to point
out that this could happen in normal programs (say layering a collection
over the ResultSet, or incorrectly missing the close when exceptions are

I've fixed the one place I'd missed it in my code, but something (even
an internal jayBird call ) to print out used resources would I think
help diagnose when someone has this basic problem in their code.

Hmm maybe something could be printed to System.setLogStream() if the
number of connections went over some trigger level, say 5,000, 10,000
20,000 40,000 etc?

I agree, making it weak, doesn't guarantee it will be collected, but an
explicit call to System.gc() should pick it up. So does the standard
means imply we can't use a weakhashmap, here?

Otherwise having a weak reference might be good defensive programming
tactic to apply here. Since that "may" be enough at some point to stop
some poor sod getting more of a problem, such as program aborting under
heavy load or after running for 5 days or so. Currently a random crash,
at some point in time doesn't look so good - even if it is the users
fault :-).



BTW: My experiece with weak referneces is about zero, but they seem the
right thing.