tomekw/hikari-cp

Timeout on datasource creation (but can connect to the database bypassing the pool)

xavi opened this issue · 5 comments

xavi commented

I'm using

[hikari-cp "1.7.6"]
[org.clojure/java.jdbc "0.7.0-alpha3"]
[org.postgresql/postgresql "9.4.1212.jre7"]

and connecting directly to my database, bypassing the pool, works

(require '[clojure.java.jdbc :as j])
(j/query {:dbtype "postgresql" :dbname "fina"} ["select * from users"])
;; => ({:email nil, ...} ...)

But when trying to create a datasource to connect through the pool...

(require '[hikari-cp.core :as hikari-cp])
(hikari-cp/make-datasource {:adapter "postgresql" :database-name "fina"})

there's a timeout

   java.net.SocketTimeoutException: connect timed out
                            org.postgresql.util.PSQLException: The connection attempt failed.
com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: The connection attempt failed.

Why is that?

xavi commented

Hi Jan, thanks for the prompt response.
I tried

(hikari-cp/make-datasource {:jdbc-url "jdbc:postgresql://localhost/fina"})
(hikari-cp/make-datasource {:jdbc-url "jdbc:postgresql://localhost:5432/fina"})
(hikari-cp/make-datasource {:adapter "postgresql" :database-name "fina" :server-name "localhost"})

and nothing worked, but... now I tried...

(hikari-cp/make-datasource {:adapter "postgresql" :database-name "fina" :server-name "127.0.0.1"})

and it worked! This also works...

(hikari-cp/make-datasource {:jdbc-url "jdbc:postgresql://127.0.0.1/fina"})

So now I can do

(def db-spec {:datasource (hikari-cp/make-datasource {:jdbc-url "jdbc:postgresql://127.0.0.1/fina"})})
(j/query db-spec ["select * from users"])

(By the way, is clojure.java.jdbc/with-db-connection, which appears in the README examples, really necessary when using hikari-cp?)

Do you know why localhost doesn't work? In /etc/hosts there is...

127.0.0.1	localhost
::1             localhost

In any case, the issue is solved. Thanks!

Interesting; thanks for digging deeper into it. I've tried all the following configurations with a locally running Postgres and I wasn't able to reproduce the java.net.SocketTimeoutException.

(hikari-cp/make-datasource {:jdbc-url "jdbc:postgresql://localhost/fina"})
(hikari-cp/make-datasource {:jdbc-url "jdbc:postgresql://localhost:5432/fina"})
(hikari-cp/make-datasource {:adapter "postgresql" :database-name "fina" :server-name "localhost"})

Have you tried to use HikariCP directly? Is the problem reproducible without the Clojure wrapper?

(By the way, is clojure.java.jdbc/with-db-connection, which appears in the README examples, really necessary when using hikari-cp?)

It should also work without it. Feel free to patch the README 🙂

xavi commented

Hey, I tried using HikariCP directly and had the same problem with localhost not working.

Anyway, I found the cause of the problem. It's because I use Vagrant for another project, and in my /etc/hosts there was an entry like...

## vagrant-hostmanager-start id: c7f036bf-c816-49de-9c00-a2f1a8ce6001
192.168.33.22	localhost
## vagrant-hostmanager-end

For both hikari-cp and HikariCP, the problem went away after deleting that entry in /etc/hosts.

Great, thanks a lot for coming back and sharing the root cause of the problem! Happy to hear that it works for you now. Closing 😄