Re: A new JDBC driver... - Mailing list pgsql-jdbc

From Kevin Wooten
Subject Re: A new JDBC driver...
Date
Msg-id BCD42993-CB7B-453F-95B4-09E84A956AB0@me.com
Whole thread Raw
In response to Re: A new JDBC driver...  (Craig Ringer <craig@2ndquadrant.com>)
List pgsql-jdbc
Yes I am familiar with these issues.  Although, as I said previously, this is only for the case where connections, statements and result-sets are not being closed.  Finalizers, with all their issues, are a lot easier and serve the same purpose as tracking things with some combination of weak, soft or phantom references and trying to basically do a finalize.

In it's final state I imagine the driver will be able to withstand leaked ResultSets and Statements but not Connections; you'll have to close your Connections properly.  To accomplish this I am using weak refs between the statements and connections and employing a finalize to catch the case where the statements and/or result sets were leaked.  If you, or anybody, has a better idea.  I am all ears.

On Mar 15, 2013, at 12:16 AM, Craig Ringer <craig@2ndquadrant.com> wrote:

On 03/15/2013 01:15 PM, Kevin Wooten wrote:
After a bit of messing around I finally settled on the much maligned "finalizer" to kill the connection if it's abandoned. 
Oh, and re finalizers:

https://www.securecoding.cert.org/confluence/display/java/MET12-J.+Do+not+use+finalizers

In particular, these points:

  • There is no fixed time at which finalizers must be executed because this depends on the JVM. The only guarantee is that any finalizer method that executes will do so sometime after the associated object has become unreachable (detected during the first cycle of garbage collection) and sometime before the garbage collector reclaims the associated object's storage (during the garbage collector's second cycle). Execution of an object's finalizer may be delayed for an arbitrarily long time after the object becomes unreachable. Consequently, invoking time-critical functionality such as closing file handles in an object's finalize() method is problematic.
  • The JVM may terminate without invoking the finalizer on some or all unreachable objects. Consequently, attempts to update critical persistent state from finalizer methods can fail without warning. Similarly, Java lacks any guarantee that finalizers will execute on process termination. Methods such as System.gc(), System.runFinalization(), System.runFinalizersOnExit(), and Runtime.runFinalizersOnExit() either lack such guarantees or have been deprecated because of lack of safety and potential for deadlock.

  • According to the Java Language Specification, §12.6.2, "Finalizer Invocations are Not Ordered" [JLS 2005]:

    The Java programming language imposes no ordering on finalize() method calls. Finalizers [of different objects] may be called in any order, or even concurrently.

    One consequence is that slow-running finalizers can delay execution of other finalizers in the queue. Further, the lack of guaranteed ordering can lead to substantial difficulty in maintaining desired program invariants.
    • Use of finalizers can introduce synchronization issues even when the remainder of the program is single-threaded. The finalize() methods are invoked by the garbage collector from one or more threads of its choice; these threads are typically distinct from the main() thread, although this property is not guaranteed. When a finalizer is necessary, any required cleanup data structures must be protected from concurrent access. See the JavaOne presentation by Hans J. Boehm [Boehm 2005] for additional information.
      In particular I would be concerned that a thread refcounting system would be subject to ordering problems or deadlocks introduced by attempts to introduce fixes for ordering problems.

      I really need to look into how other JDBC drivers handle these issues; it's not like this is a new problem.
      -- Craig Ringer                   http://www.2ndQuadrant.com/PostgreSQL Development, 24x7 Support, Training & Services

      pgsql-jdbc by date:

      Previous
      From: Kevin Wooten
      Date:
      Subject: Re: A new JDBC driver...
      Next
      From: Kevin Grittner
      Date:
      Subject: Re: Improper type conversion from smallint to short