Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure/postgresql: How do I see the exception when an error is in the db?

When using clojure with postgresql, whenever a statement I issue is malformed in some way or otherwise rejected by the db itself I get something like the following error:

java.sql.BatchUpdateException: 
  Batch entry 0 drop schema public cascade was aborted.  
  Call getNextException to see the cause.

How can I call getNextException so that I can see what I did wrong? Where do I call it from?

like image 756
prismofeverything Avatar asked Aug 16 '11 06:08

prismofeverything


2 Answers

See this link on clojure/jdbc showing how to drop a table with Clojure/JDBC.

It also shows you how to handle errors with a try catch block.

From within that try catch block, you can write something similar to:

(.printStackTrace (.getCause e))
like image 64
Nicolas Modrzyk Avatar answered Oct 13 '22 00:10

Nicolas Modrzyk


I have used the following to drop/create tables successfully and to get accurate error information when postgresql gets upset:

(defn drop-table [name]
    (sql/with-connection db
      (with-open [s (.createStatement (sql/connection))]
        (try
          (.addBatch s (str "drop table " name ))
          (seq (.executeBatch s))
          (catch Exception _)))))

(defn setup []
  (comment "TODO: create tables here"))

(defn -main []
  (try
  (print "Dropping table...") (flush)
  (drop-table "table_name")
  (print "Creating database structure...") (flush)
  (setup)
  (println " done")
  (catch Exception e (.getNextException e))))
like image 20
Paul Drummond Avatar answered Oct 13 '22 01:10

Paul Drummond