Thanks, Sean, here's simplified test which shows the behavior:
Project dependencies:
:dependencies [[org.clojure/clojure "1.3.0"]
[org.clojure/java.jdbc "0.2.2"]
[mysql/mysql-connector-java "5.1.6"]
Code:
(ns jdbc-test.core
(:require [clojure.java.jdbc :as sql])
(:import (java.text SimpleDateFormat)
(java.util Date)))
;; MySQL connection configuration
(def mysql-db
{:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:user "root"
:password "root"
:subname "//localhost/clojurejdbc"})
;; Table definition
(defn add-row-to-table
[label datetime]
(sql/with-connection mysql-db
(sql/insert-records :inserttest
{:id 0 :label label :created datetime})))
(defn generate-entries
[no]
(let [date-formatter (SimpleDateFormat. "yyyy-MM-dd HH:mm:ss")]
(loop [counter 1
datetime (.format date-formatter (Date.))]
(if (= 0 (mod counter 1000))
(println (str counter " rows added")))
(add-row-to-table (str "Line #" counter) datetime)
(if (< counter no)
(recur (inc counter) datetime)))))
The MySQL database and table definition:
CREATE DATABASE clojurejdbc;
USE clojurejdbc;
CREATE TABLE inserttest (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
label CHAR(255),
created DATETIME
);
I call the function generate-entries like this:
(generate-entries 1000000)
On my machine, the program stops after 34000+ rows have been added to the table:
...
32000 rows added
33000 rows added
34000 rows added
NoRouteToHostException Cannot assign requested address
java.net.PlainSocketImpl.socketConnect (PlainSocketImpl.java:-2)
The problem is probably the high number of connections kept open to
the localhost port, but that's just a guess:
http://stackoverflow.com/questions/1572215/how-to-avoid-a-noroutetohostexception
"netstat -a" shows a lot of TIME_WAIT entries
tcp 0 0 localhost:mysql *:* LISTEN
tcp 0 0 localhost:mysql localhost:46850 TIME_WAIT
tcp 0 0 localhost:mysql localhost:46737 TIME_WAIT
tcp 0 0 localhost:mysql localhost:46340 TIME_WAIT
(long list continued) ....
So this is not Clojure/JDBC specific, but a general problem with Java
and MySQL executing many queries to localhost. I'll investigate more
later.
Thanks,
Raju