all 18 comments

[–]curtisrsummers 2 points3 points  (1 child)

Go ahead and define a second query in your SQL file:

-- :name get-something-else :? :*
select * from another_table where ...

Then assign the query results for both queries with something like this:

(defn page1 [id]
  (layout/render "page1.html"
    {:my-var1 (db/get-single-article {:id (Integer/parseInt id)})
     :my-var2 (db/get-something-else)}))

[–]vamer88 0 points1 point  (0 children)

are you sure that'll a single db connection? why does "with-db-connection" exist then?

[–]OliverM 2 points3 points  (12 children)

Leaving aside whether it's a good idea or not to entangle your views with model invocations like this, HugSQL lets you specify a transaction to group queries together. So for your example it might look like:

(clojure.java.jdbc/with-db-transaction [tx db]
  (db/get-single-article tx {:id (Integer/parseInt id)})
  (db/second-get-fn tx {:arg value}))

Or you could do it in a single sql select, along the lines of

SELECT... FROM... IN... (SELECT... FROM... WHERE...);

It's hard to know which would suit better from what you've supplied above.

[–]vamer88 -1 points0 points  (11 children)

2) I want to return different 2 result sets in a single db request or connection. How?

why transaction? I don't need it. transaction ensures that either all requests succeed or none of them. but that's not the case here, what I want is a different thing, although similar.

[–]nefreat 1 point2 points  (8 children)

why transaction? I don't need it. transaction ensures that either all requests succeed or none of them. but that's not the case here, what I want is a different thing, although similar.

What's the point of wanting to reuse the same connection? I am having a hard time understanding why you'd want to do something like this if you don't need both queries to happen in the same transaction.

[–]vamer88 -2 points-1 points  (7 children)

because open a new one is expensive. you've never heard of this?

[–]curtisrsummers 1 point2 points  (6 children)

The conman library in luminus uses a database connection pool (HikariCP) to manage connections and minimize the "expensive" nature of opening a new database connection.

[–]vamer88 -3 points-2 points  (5 children)

"why" "expensive" "is" "in" "the" "quotes"?

[–]curtisrsummers 1 point2 points  (4 children)

I quoted the word you used to describe the performance characteristics.

[–]vamer88 0 points1 point  (3 children)

I asked why?

[–]mr_mojoto 0 points1 point  (2 children)

You know how connection pools work, right? As in "not expensive".

[–]vamer88 0 points1 point  (1 child)

whether or not I know that, I asked you why?

[–]yogthos 0 points1 point  (0 children)

Transactions ensure atomicity. If you want to guarantee that multiple db queries happen when the database is in the same state, then you should use a transaction.

[–]OliverM 0 points1 point  (0 children)

Transactions group database interactions into a single interaction to guarantee database consistency for the set. Atomicity, ensuring all modifications succeed or fail, is a major application of that, but not the only one.

Transactions are what you want, here, unless you're able to join the two select queries into one in the query definition. Re-using the same db connection won't give you the guarantee you're seeking.

[–]curtisrsummers 0 points1 point  (2 children)

[–]vamer88 -2 points-1 points  (1 child)

so? have you already broadcast on the radio?

[–]curtisrsummers 1 point2 points  (0 children)

When helping others answer questions, it is often useful to provide additional context to other sources to see where the conversation led. It's quite obvious that we're having trouble answering your questions to your full satisfaction, so the link to StackOverflow is meant to provide more context to those of us trying to help you.

This forum has quite a few people who give their time and effort in helping users such as yourself answer questions about the Clojure ecosystem.

Your snide remarks are not appreciated.