Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8202625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:15:06+00:00 2026-06-07T07:15:06+00:00

When I create a query in squeryl, it returns a Query[T] object. The query

  • 0

When I create a query in squeryl, it returns a Query[T] object. The query was not yet executed and will be, when I iterate over the Query object (Query[T] extends Iterable[T]).

Around the execution of a query there has to be either a transaction{} or a inTransaction{} block.

I’m just speaking of SELECT queries and transactions wouldn’t be necessary, but the squeryl framework needs them.

I’d like to create a query in the model of my application and pass it directly to the view where a view helper in the template iterates over it and presents the data.
This is only possible when putting the transaction{} block in the controller (the controller includes the call of the template, so the template which does the iteration is also inside). It’s not possible to put the transaction{} block in the model, because the model doesn’t really execute the query.

But in my understanding the transaction has nothing to do with the controller. It’s a decision of the model which database framework to use, how to use it and where to use transactions. So I want the transaction{} block to be in the model.

I know that I can – instead of returning the Query[T] instance – call Iterable[T].toList on this Query[T] object and then return the created list. Then the whole query is executed in the model and everything is fine. But I don’t like this approach, because all the data requested from the database has to be cached in this list. I’d prefer a way where this data is directly passed to the view. I like the MySql feature of streaming the result set when it’s large.

Is there any possibility? Maybe something like a function Query[T].executeNow() which sends the request to the database, is able to close the transaction, but still uses the MySQL streaming feature and receives the rest of the (selected and therefore fixed) result set when it’s accessed? Because the result set is fixed in the moment of the query, closing the transaction shouldn’t be a problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:15:08+00:00Added an answer on June 7, 2026 at 7:15 am

    The general problem that I see here is that you try to combine the following two ideas:

    • lazy computation of data; here: database results

    • hiding the need for a post-processing action that must be triggered when the computation is done; here: hiding from your controller or view that the database session must be closed

    Since your computation is lazy and since you are not obliged to perform it to the very end (here: to iterate over the whole result set), there is no obvious hook that could trigger the post-processing step.

    Your suggestion of invoking Query[T].toList does not exhibit this problem, since the computation is performed to the very end, and requesting the last element of the result set can be used as a trigger for closing the session.

    That said, the best I could come up with is the following, which is an adaptation of the code inside org.squeryl.dsl.QueryDsl._using:

    class IterableQuery[T](val q: Query[T]) extends Iterable[T] {
      private var lifeCycleState: Int = 0
      private var session: Session = null
      private var prevSession: Option[Session] = None
    
      def start() {
        assert(lifeCycleState == 0, "Queries may not be restarted.")
        lifeCycleState = 1
    
        /* Create a new session for this query. */
        session = SessionFactory.newSession
    
        /* Store and unbind a possibly existing session. */
        val prevSession = Session.currentSessionOption
        if(prevSession != None) prevSession.get.unbindFromCurrentThread
    
        /* Bind newly created session. */
        session.bindToCurrentThread
      }
    
      def iterator = {
        assert(lifeCycleState == 1, "Query is not active.")
        q.toStream.iterator
      }
    
      def stop() {
        assert(lifeCycleState == 1, "Query is not active.")
        lifeCycleState = 2
    
        /* Unbind session and close it. */
        session.unbindFromCurrentThread
        session.close
    
        /* Re-bind previous session, if it existed. */
        if(prevSession != None) prevSession.get.bindToCurrentThread
      }
    }
    

    Clients can use the query wrapper as follows:

    var manualIt = new IterableQuery(booksQuery)
    manualIt.start()
    manualIt.foreach(println)
    manualIt.stop()
    //      manualIt.foreach(println) /* Fails, as expected */
    
    manualIt = new IterableQuery(booksQuery) /* Queries can be reused */
    manualIt.start()
    manualIt.foreach(b => println("Book: " + b))
    manualIt.stop()
    

    The invocation of manualIt.start() could already be done when the object is created, i.e., inside the constructor of IterableQuery, or before the object is passed to the controller.

    However, working with resources (files, database connections, etc.) in such a way is very fragile, because the post-processing is not triggered in case of exceptions. If you look at the implementation of org.squeryl.dsl.QueryDsl._using you will see a couple of try ... finally blocks that are missing from IterableQuery.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a query that returns rows in the following format:
Error SQL query: CREATE TABLE Contacts( Contact_ID INT NOT NULL , User VARCHAR NOT
We are trying to create/query information from a CF based on the following structure
I'm used to create query with createQuery() instead of queryBuilder, but I need to
i need to change my query from native-query to ( named-query or create-query )
I want to create a query that selects the columns PlayerName , PlayerNumber, Location,
I want to create a query in TFS that allows me to view only
How can I create one query using both ICriteria and Linq? Example: var q
i am trying to create a query on my simpleDB. here is the query:
I'm trying to create wmi query to sccm to get PC, where was user's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.