[Haskell] Re: ANN: HDBC (Haskell Database Connectivity)

John Goerzen jgoerzen at complete.org
Wed Jan 4 14:13:00 EST 2006

On 2006-01-04, Krasimir Angelov <kr.angelov at gmail.com> wrote:
>> I use ForeignPtrs everywhere in HDBC to try to make sure that nothing
>> like this happens, and also that The Right Thing happens if a database
>> handle gets garbage collected without being explicitly closed first.
> I prefer not to rely on ForeignPtrs. It isn't guaranteed that they
> will be run. Another problem is that the order in which finalizers are
> executed isn't specified. In this case the connection handle can be

Well, yes and no.  It would be impossible to garbage collect (and thus
finalize) any object for which references to it still exist.  Statement
handles in HDBC maintain references to the database handle pointers,
either directly or indirectly, so I can't see how it is possible for a
database handle to be finalized before the statement handle in this

> closed before the statement handle. The usage of raw pointers has the
> advantage that it is easier for the developer to see that there is a
> space leak.

The advantage of ForeignPtrs is that it's almost impossible for a space
leak to exist in the first place ;-)

> I saw that you are using unsafe foreign imports everywhere in
> Database.HDBC.PostgreSQL. The trouble with them is that all Haskell
> threads will be suspended during the call. Maybe this explains why you
> don't see the crash which you saw with HSQL. I still don't know where
> was the problem with HSQL.

I think you have that backwards, but I'm unsure.  According to the FFI
spec, section 3.3:

  "Optionally, an import declaration can specify, after the calling
  convention, the safety level that should be used when invoking an
  external entity. A safe call is less efficient, but guarantees to
  leave the Haskell system in a state that allows callbacks from the
  external code. In contrast, an unsafe call, while carrying less
  overhead, must not trigger a callback into the Haskell system."

There is no reason for any of these calls to trigger a callback into
Haskell, so they can all be imported unsafe for greater efficiency.

But it doesn't directly address threads, so I don't know what to make of
that.  Do you have a reference?

-- John

John Goerzen <jgoerzen at complete.org>    GPG: 0x8A1D9A1F    www.complete.org
"Value your freedom, or you will lose it, teaches history.  `Don't bother us
with politics,' respond those who don't want to learn."

More information about the Haskell mailing list