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

Ok, I (finally) got time to implement a weak hash map of stmts in
Connection and it seems to works fine.

I used Branch_1_0 and have attached a code patch, the change turned out
to be quite simple. and it runs though the test suite, (and I've even
added an extra test for this case as well - not in the patch).


Recapping, briefly, the problem was if one forgot the close() on a
statement (and had long lasting Connections) then db resources were
consumed over time, compounding the problem was that it was difficult to
debug the cause, the program seems to work, but after a period of
operating it gets slower and eventually fails - also code written for
(some) other jdbc drivers do this cleanup, so using code from other
systems can give unexpected errors.


The weak references allows our driver work even if the user forgets the
close, in the following simulation of the forgotten close().

for (int i = 0; i < 40000; i++) {
Statement stmt = con.createStatement();
}

With weak references, even in this tight loop, the internal stmt count
was kept down to 4000 max.


But to still give the user an idea that he's perhaps consuming
resources, Im writing the following message to DriverManager.getLogStream()


This Connection usage of Statement resources has just exceeded [ 500 ]
active connections
If this is unexpected then it is probably an indication of a missing
close() call in ResultSet or Statement objects

This msg is written as the size of activeStatements set in Connection
exceeds, 500, 1000, 2000, 4000, 8000, 16000, 32000, 64000 ..etc.


Roman, when (and how) is a good time to add this to the main branch, I
assume you want to fix the upgrade of the transaction stuff first.


Cheers

Mark