From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA19047; Fri, 30 Jan 2004 13:56:48 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id NAA19292 for ; Fri, 30 Jan 2004 13:56:47 +0100 (MET) Received: from rabelais.socialtools.net (rabelais.socialtools.net [81.2.94.243]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id i0UCukP19134 for ; Fri, 30 Jan 2004 13:56:46 +0100 (MET) Received: by rabelais.socialtools.net (Postfix, from userid 108) id 56D0223341; Fri, 30 Jan 2004 12:56:46 +0000 (GMT) Received: from socialtools.net (chaucer.socialtools.net [81.2.94.242]) by rabelais.socialtools.net (Postfix) with ESMTP id 0A9242332A; Fri, 30 Jan 2004 12:56:44 +0000 (GMT) Message-ID: <401A548B.2090603@socialtools.net> Date: Fri, 30 Jan 2004 12:56:43 +0000 From: Benjamin Geer User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031007 X-Accept-Language: en-gb, en, fr, it MIME-Version: 1.0 To: Richard Jones Cc: caml-list@inria.fr Subject: Re: [Caml-list] PostgreSQL-OCaml 1.0.1 References: <20040130120206.GA28458@redhat.com> <401A4AD9.2060509@socialtools.net> <20040130122311.GA29288@redhat.com> In-Reply-To: <20040130122311.GA29288@redhat.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on rabelais.socialtools.net X-Spam-Status: No, hits=-4.9 required=5.0 tests=BAYES_00 autolearn=ham version=2.63 X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 1.0.1:01 committing:99 verbose:01 error-prone:01 conn:01 caml:01 exception:02 exception:02 suited:02 classes:03 wrote:03 annoying:03 let:04 exceptions:04 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Richard Jones wrote: > It throws Sql_error when it can determine an error. Another thing I forgot to bring up about error handling. If an error occurs, you generally want to roll back the transaction. If you're using a connection pool, you want to make sure you close the connection when you're done with it, regardless of whether the transaction was committed or rolled back. Committing a transaction, rolling back a transaction, and closing a connection are all operations that could also throw exceptions. Writing out all those try-with expressions every time becomes very verbose and error-prone. I think this is the sort of problem that functional programming is really well suited to. For example, you could have a standard function like this: let do_f_with_error_handling f conn = ... It would begin a transaction on the specified connection, run f, handle any exceptions by rolling back the transaction (or else commit it if there were no errors), then close the connection (also handling any errors). It would return the value returned by f, or if it caught an exception, it would throw that exception (after cleaning up). (This is one of the annoying things about JDBC; I tend to write pseudo-functional classes to do this sort of thing in Java, but in Caml it would be much more straightforward.) Ben ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners