简体   繁体   中英

Squeryl session management with 'using'

I'm learning Squeryl and trying to understand the 'using' syntax but can't find documentation on it.

In the following example two databases are created, A contains the word Hello , and B contains Goodbye . The intention is to query the contents of A, then append the word World and write the result to B.

Expected console output is Inserted Message(2,HelloWorld)

object Test {
    def main(args: Array[String]) {
        Class.forName("org.h2.Driver");
        import Library._

        val sessionA = Session.create(DriverManager.getConnection(
                "jdbc:h2:file:data/dbA","sa","password"),new H2Adapter)
        val sessionB = Session.create(DriverManager.getConnection(
                "jdbc:h2:file:data/dbB","sa","password"),new H2Adapter)

        using(sessionA){
            drop; create
            myTable.insert(Message(0,"Hello"))
        }
        using(sessionB){
            drop; create
            myTable.insert(Message(0,"Goodbye"))
        }

        using(sessionA){
            val results = from(myTable)(s => select(s))//.toList

            using(sessionB){
                results.foreach(m => {
                    val newMsg = m.copy(msg = (m.msg+"World"))
                    myTable.insert(newMsg)
                    println("Inserted "+newMsg)
                })
            }
        }
    }

    case class Message(val id: Long, val msg: String) extends KeyedEntity[Long]
    object Library extends Schema { val myTable = table[Message] }
}

As it stands, the code prints Inserted Message(2,GoodbyeWorld) , unless the toList is added on the end of the val results line.

Is there some way to bind the results query to use sessionA even when evaluated inside the using(sessionB) ? This seems preferable to using toList to force the query to evaluate and store the contents in memory.

Update

Thanks to Dave Whittaker's answer, the following snippet fixes it without resorting to 'toList' and corrects my understanding of both 'using' and the running of queries.

val results = from(myTable)(s => select(s))

using(sessionA){            
    results.foreach(m => {
        val newMsg = m.copy(msg = (m.msg+"World"))
        using(sessionB){myTable.insert(newMsg)}
        println("Inserted "+newMsg)
    })
}

First off, I apologize for the lack of documentation. The using() construct is a new feature that is only available in SNAPSHOT builds. I actually talked to Max about some of the documentation issues for early adopters yesterday and we are working to fix them.

There isn't a way that I can think of to bind a specific Session to a Query. Looking at your example, it looks like an easy work around would be to invert your transactions. When you create a query, Squeryl doesn't actually access the DB, it just creates an AST representing the SQL to be performed, so you don't need to issue your using(sessionA) at that point. Then, when you are ready to iterate over the results you can wrap the query invocation in a using(sessionA) nested within your using(sessionB). Does that make sense?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM