Clojure JDBC - This ResultSet is closed

Viewed 737

I am currently learning using Clojure and connection with postgresql and I am stuck. In my code, I can successfully query using SELECT using java.jdbc dependency but got an exception when I UPDATE or INSERT

I am using these dependencies

[org.clojure/java.jdbc "0.7.8"]
[postgresql "9.3-1102.jdbc41"]

This is my working code for SELECT-ing from a table called public."user"

(ns knp-api.model.user
  (:require [clojure.java.jdbc :as sql]))


(def db "jdbc:postgresql://ampersanda@localhost:5432/idjoesoft_klinik")

(defn get-count [q]
  "Get count key from query
   It takes jdbc.query as parameter"
  (:count (first (into [] q))))

(defn is-user-email-available? [e]
  "Returns boolean when email is available.
   Take one parameter, it's email"
  (let [q "SELECT COUNT(*) FROM public.\"user\" t WHERE user_email = ?"]
    (sql/query db [q e] {:result-set-fn #(= 1 (get-count %))})))

and this is not my code which makes an exception appear

(defn set-user-timestamp-and-token [email token timestamp]
  "update user current token and last login time inside database.
   Takes email, token, and current timestamp as parameter"
  (sql/update! db 
               "public.\"user\"" 
               {:user_last_login (str "to_timestamp(" timestamp ")") 
                :user_token token}  
               ["user_email = ?" email]))

and this is the exception

enter image description here

UPDATE

I also try using REPL to create table and I also get the same results.

knp-api.handler=> (require '[clojure.java.jdbc :as sql])
nil
knp-api.handler=> (sql/db-do-commands "jdbc:postgresql://ampersanda@localhost:5432/idjoesoft_klinik" (sql/create-table-ddl :testing [[:data :text]]))

PSQLException This ResultSet is closed.  org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkClosed (AbstractJdbc2ResultSet.java:2852)
4 Answers

I suspect the update is happening fine, and then it's trying to return the rows that changed, and it's trying to do so as a lazy sequence. the thing that tries to print/process that lazy sequence of results may be doing so after the connection is closed.

try wrapping the select statement in a call to doall to ensure that it's read immediately and not allowed to be a lazy database.

Having run into the same issue today, the root cause is the postgresql java driver. postgresql "9.3-1102.jdbc41" is five years old, after upgrading to 42.2.6 the issue went away.

I remember having this problem, but it's many years ago and I can't remember the solution. However, wrapping raw JDBC in Clojure is not a good idea. There are a number of excellent Clojure libraries which make working with databases much easier. I'd particularly recomment SQL Korma and Hug SQL.

Having spent a while trying to get this solved I can confirm in my case updating the postgresql java driver solved this issue, if anybody is having the same issue try to use the newest driver from clojars/maven, Clojars even has a message telling you that you should use the version from maven central.

Related