From 101780d2cd6e6c87203c42eee8d9ebcc395ea4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= Date: Mon, 22 Sep 2014 20:32:26 +0200 Subject: [PATCH] db.postgresql.lib: if PQsetdbLogin fails, PQfinish must be called --- basis/db/postgresql/lib/lib.factor | 7 ++++--- basis/db/postgresql/postgresql-tests.factor | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/basis/db/postgresql/lib/lib.factor b/basis/db/postgresql/lib/lib.factor index 0818ccfd68..7f350d17ee 100644 --- a/basis/db/postgresql/lib/lib.factor +++ b/basis/db/postgresql/lib/lib.factor @@ -42,8 +42,9 @@ M: postgresql-result-null summary ( obj -- str ) PGRES_COMMAND_OK PGRES_TUPLES_OK 2array member? ; : connect-postgres ( host port pgopts pgtty db user pass -- conn ) - PQsetdbLogin - dup PQstatus zero? [ (postgresql-error-message) throw ] unless ; + PQsetdbLogin dup PQstatus zero? [ + [ (postgresql-error-message) ] [ PQfinish ] bi throw + ] unless ; : do-postgresql-statement ( statement -- res ) db-connection get handle>> swap sql>> PQexec dup postgresql-result-ok? [ @@ -147,7 +148,7 @@ M: postgresql-malloc-destructor dispose ( obj -- ) &postgresql-free ] if ] with-out-parameters memory>byte-array - ] with-destructors + ] with-destructors ] [ drop pq-get-is-null nip [ f ] [ B{ } clone ] if ] if ; diff --git a/basis/db/postgresql/postgresql-tests.factor b/basis/db/postgresql/postgresql-tests.factor index 2d27c1218c..ddfae56f87 100644 --- a/basis/db/postgresql/postgresql-tests.factor +++ b/basis/db/postgresql/postgresql-tests.factor @@ -1,8 +1,19 @@ USING: kernel db.postgresql alien continuations io classes -prettyprint sequences namespaces tools.test db db.private +prettyprint sequences math namespaces tools.test db db.private db.tuples db.types unicode.case accessors system db.tester ; IN: db.postgresql.tests +: nonexistant-db ( -- db ) + + "localhost" >>host + "fake-user" >>username + "no-pass" >>password + "dont-exist" >>database ; + +! Don't leak connections +[ ] [ + 2000 [ [ nonexistant-db [ ] with-db ] ignore-errors ] times +] unit-test ! Ensure the table exists [ ] [ postgresql-test-db [ ] with-db ] unit-test @@ -84,4 +95,4 @@ IN: db.postgresql.tests postgresql-test-db [ "select * from person" sql-query length ] with-db -] unit-test \ No newline at end of file +] unit-test