It looks like you are trying to create a DLL that can be called from other languages. If so, then all of your functions will live in the IO monad. This way you can use IORef to store the database connection object.
Updated (see comments below)
GHC has a similar problem with the state of the global random number generator. Here is the corresponding source code from System.Random:
-- |Gets the global random number generator. getStdGen :: IO StdGen getStdGen = readIORef theStdGen theStdGen :: IORef StdGen theStdGen = unsafePerformIO $ do rng <- mkStdRNG 0 newIORef rng
Thus, presumably something like this will work to connect to the database. Yes, this is the use of evil unsafePerformIO, but sometimes you just have to be evil.
source share