I'm trying to provoke a deadlock with Pg, but instead of erroring out, it just hangs indefinitely, nothing in the pg log [thread]
(def config2
{:dbtype "postgresql"
:host "localhost"
:dbname "postgres"
:user "postgres"
:password "postgres"})
(comment
;; example from Designing data-intensive applications, pg 235 + 236
(let [ds (jdbc/get-datasource config2)
conn1 (jdbc/get-connection ds)
conn2 (jdbc/get-connection ds)
_ (jdbc/execute! conn1 ["SET statement_timeout = '5s'"])
_ (jdbc/execute! conn1 ["DROP TABLE IF EXISTS listings"])
_ (jdbc/execute! conn1 ["DROP TABLE IF EXISTS invoices"])
_ (jdbc/execute! conn1 ["CREATE TABLE listings (id serial, buyer text)"])
_ (jdbc/execute! conn1 ["CREATE TABLE invoices (id serial, listing_id bigint, recipient text)"])
[{:keys [id]}] (jdbc/execute! conn1 ["INSERT INTO listings DEFAULT VALUES RETURNING id"])
_ (jdbc/execute! conn1 ["INSERT INTO invoices (listing_id) VALUES (?)" id])
;; go!
_ (jdbc/execute! conn1 ["BEGIN"])
_ (jdbc/execute! conn1 ["UPDATE listings SET buyer = ? WHERE id = ?" "alice" 1])
_ (jdbc/execute! conn2 ["BEGIN"])
_ (jdbc/execute! conn2 ["UPDATE listings SET buyer = ? WHERE id = ?" "bob" 1])
_ (jdbc/execute! conn2 ["UPDATE invoices SET recipient = ? WHERE listing_id = ?" "bob" 1])
_ (jdbc/execute! conn2 ["COMMIT"])
_ (jdbc/execute! conn1 ["UPDATE invoices SET recipient = ? WHERE listing_id = ?" "alice" 1])
_ (jdbc/execute! conn1 ["COMMIT"])
_ 42]
"ok"))This is pg_stat_activity:
SELECT wait_event_type,wait_event,state,query FROM pg_stat_activity where application_name='PostgreSQL JDBC Driver';
+-----------------+---------------+---------------------+----------------------------------------------+
| wait_event_type | wait_event | state | query |
|-----------------+---------------+---------------------+----------------------------------------------|
| Client | ClientRead | idle in transaction | UPDATE listings SET buyer = $1 WHERE id = $2 |
| Lock | transactionid | active | UPDATE listings SET buyer = $1 WHERE id = $2 |
+-----------------+---------------+---------------------+----------------------------------------------+deadlock timeout is 1s (default), isolation level is default (also tried a couple of others, but I guess it shouldn't really change the fact that it'll deadlock
There's something I'm not really getting 😅
D'uh .. I need threads
Was just about to mention. :)
I must have been programming too much with async in typescript these last days ... 😄 It's getting to me
TS strikes again!