简体   繁体   English

光滑的threadLocalSession与隐式会话

[英]Slick threadLocalSession vs implicit session

I encountered this problem while posting this question: Slick Write a Simple Table Creation Function 我在发布这个问题时遇到了这个问题: Slick写一个简单的表创建函数

I am very new to Slick and concurrency, knowing only the basics. 我对Slick和并发很新,只知道基础知识。 I worked with JDBC before, but in there you have to manually open a session and then close it. 之前我使用过JDBC,但在那里你必须手动打开一个会话然后关闭它。 Nothing goes beyond that, and there is very little automation (at least I didn't have to make automated process). 没有什么比这更好了,自动化很少(至少我不需要进行自动化处理)。

However, I get confused with Slick session. 但是,我对Slick会话感到困惑。 In the tutorial, the example "Getting Started" encourages people to use threadLocalSession : 在本教程中,示例“Getting Started”鼓励人们使用threadLocalSession

// Use the implicit threadLocalSession //使用隐式threadLocalSession

import Database.threadLocalSession import Database.threadLocalSession

http://slick.typesafe.com/doc/1.0.0/gettingstarted.html http://slick.typesafe.com/doc/1.0.0/gettingstarted.html

The original recommendation is: 最初的建议是:

The only extra import we use is the threadLocalSession. 我们使用的唯一额外导入是threadLocalSession。 This simplifies the session handling by attaching a session to the current thread so you do not have to pass it around on your own (or at least assign it to an implicit variable). 这通过将会话附加到当前线程来简化会话处理,因此您不必自己传递它(或者至少将其分配给隐式变量)。

Well, I researched a bit online, and some people suggest not to use threadLocalSession and only use implicit session. 好吧,我在网上研究了一下,有些人建议不要使用threadLocalSession,只使用隐式会话。 Some suggest using threadLocalSession. 有人建议使用threadLocalSession。

One reason to support implicit session is that "makes sure at compile time that you have a session". 支持隐式会话的一个原因是“在编译时确保您有会话”。 Well, I only have two questions: 好吧,我只有两个问题:

  1. When people use "thread", are they referring to concurrency? 当人们使用“线程”时,他们是指并发吗? Slick/JDBC data storage was handled through concurrency? Slick / JDBC数据存储是通过并发处理的吗?

  2. Which way is better? 哪种方式更好? Implicit or threadLocalSession? 隐式或threadLocalSession? Or when to use which? 或何时使用哪个?

  3. If it is not too much to ask, I read the syntax of {implicit session:Session => ...} somewhere in my Scala book, and I forgot where it was. 如果问的不是太多,我在Scala书中的某处读了{implicit session:Session => ...}的语法,我忘记了它的位置。 What's this expression? 这个表达是什么?

  1. A threadLocalSession is called this way because it is stored in a "thread local variable", local to the current execution thread. threadLocalSession以这种方式调用,因为它存储在当前执行线程本地的“线程局部变量”中。

  2. As of Slick 2, we recommend not to use threadLocalSession (which is now called dynamicSession) unless you see a particular need for it and are aware of the disadvantages. 从Slick 2开始,我们建议不要使用threadLocalSession(现在称为dynamicSession),除非你看到它的特殊需要并且意识到它们的缺点。 threadLocalSession is implicit, too, by the way. 顺便说一句,threadLocalSession也是隐式的。 The problem is, that a threadLocalSession is only valid at runtime when a withSession (in Slick 2.0 withDynSession) call happened somewhere down the call stack. 问题是,当一个withSession(在Slick 2.0 withDynSession中)调用发生在调用堆栈的某个地方时,threadLocalSession仅在运行时有效。 If it didn't the code still compiles but fails at runtime 如果没有,代码仍然编译但在运行时失败

  3. {implicit session:Session => ...} is a function from (the explicitly annotated type) Session to ..., where the session is available as an implicit value in ... . {implicit session:Session => ...}是一个函数,从(显式注释的类型)Session到...,其中会话在...中作为隐式值可用。 In db.withSession{ implicit session:Session => ... }, db creates a session, passes it into the closure handed to withSession. 在db.withSession {implicit session:Session => ...}中,db创建一个会话,将其传递给传递给withSession的闭包。 In the closure body ..., the session is implicit and can implicitly used by .list calls, etc. 在闭包体......中,会话是隐式的,可以由.list调用等隐式使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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