使用 Squeryl 时必须绑定会话错误
更新: 好的,我解决了遇到的问题,但我仍然不太确定我做错了什么。无论如何,我在 IRCDB 中编写了以下方法:
def tryit[T](p: => T) = {
Class.forName("org.h2.Driver") //what's the point in this...?
SessionFactory.concreteFactory = Some(() =>
Session.create(java.sql.DriverManager.getConnection("jdbc:h2:~/irc","ScalaIRC",""), new H2Adapter))
transaction {
p
}
}
然后像这样使用它:
val query = from(IRCDB.channels)(c => select(c))
IRCDB.tryit {
for (r <- query) println("chan: " + r.name)
}
现在它可以工作了。根据我的理解,我相信一旦使用 SessionFactory 创建会话,我就可以在具有正确导入的任何地方使用 transaction { ... }
。显然,我的理解是不正确的。
如果有人有什么要补充的,请补充。
所以,我刚刚开始在我正在进行的一个项目中使用 Squeryl,所以这可能只是我的一个疏忽。所以,首先是代码:
我有以下导入:
import org.squeryl.PrimitiveTypeMode._
import java.sql.Timestamp
import java.sql.DriverManager
import java.util.Date
import org.squeryl.adapters.H2Adapter
import org.squeryl.dsl.{OneToMany, ManyToOne, CompositeKey2}
import org.squeryl._
我定义了下表:
class ChannelTable(val id: Long, val name : String,
val p_mode : Boolean, val s_mode : Boolean,
val i_mode : Boolean, val t_mode : Boolean,
val n_mode : Boolean, val m_mode : Boolean,
val key : Option[String]) extends KeyedEntity[Long] {
def this() = this(0,"", false, false, false, false, false, false, Some(""))
lazy val bans:OneToMany[ChannelBanTable] = IRCDB.channelToChanBans.left(this)
lazy val users = IRCDB.channelUsers.left(this)
lazy val invites = IRCDB.channelInvites.left(this)
}
并定义了以下架构:
object IRCDB extends Schema {
val channels = table[ChannelTable]
on(channels)(c => declare(
c.p_mode defaultsTo(false),
c.s_mode defaultsTo(false),
c.i_mode defaultsTo(false),
c.t_mode defaultsTo(false),
c.n_mode defaultsTo(false),
c.m_mode defaultsTo(false),
c.name is(unique, indexed)
))
def init {
Class.forName("org.h2.Driver") //what's the point in this...?
SessionFactory.concreteFactory = Some(() =>
Session.create(DriverManager.getConnection("jdbc:h2:~/irc","ScalaIRC",""), new H2Adapter))
}
def getAllChannels = transaction { from(channels)(c => select(c))}
}
我已经能够在控制台上将以下内容插入到表中:
scala> import db._;import org.squeryl.PrimitiveTypeMode._
import db._
import org.squeryl.PrimitiveTypeMode._
scala> IRCDB.init
scala> transaction { IRCDB.channels.insert(new ChannelTable(0,"#chan_name", false, false, false, false, false, false, None)) }
res3: db.ChannelTable = db.ChannelTable@4
我可以通过 H2 控制台确认这确实发生了。但是,当我调用 IRCDB.getAllChannels 时(在 Scala 控制台和代码中),我收到以下错误:
java.lang.RuntimeException: no session is bound to current thread, a session must be created via Session.create
and bound to the thread via 'work' or 'bindToCurrentThread'
at scala.Predef$.error(Predef.scala:58)
at org.squeryl.Session$$anonfun$currentSession$1.apply(Session.scala:117)
at org.squeryl.Session$$anonfun$currentSession$1.apply(Session.scala:117)
at scala.Option.getOrElse(Option.scala:59)
at org.squeryl.Session$.currentSession(Session.scala:116)
at org.squeryl.dsl.AbstractQuery.org$squeryl$dsl$AbstractQuery$$_dbAdapter(AbstractQuery.scala:136)
at org.squeryl.dsl.AbstractQuery$$anon$1.<init>(AbstractQuery.scala:140)
at org.squeryl.dsl.AbstractQuery.iterator(AbstractQuery.scala:138)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:79)
at org.squeryl.dsl.AbstractQuery.foreach(AbstractQuery.scala:27)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:206)
at org.squeryl.dsl.AbstractQuery.map(AbstractQuery.scala:27)
at scala.runtime.ScalaRunTime$.inner$1(ScalaRunTime.scala:255)
at scala.runtime.ScalaRunTime$.stringOf(ScalaRunTime.scala:258)
at RequestResult$line7$object$.<init>(<console>:12)
at RequestResult$line7$object$.<clinit>(<console>)
at RequestResult$line7$object.scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$18.apply(Interpreter.scala:981)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$18.apply(Interpreter.scala:981)
at scala.util.control.Exception$Catch.apply(Exception.scala:79)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1.apply(Interpreter.scala:980)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1.apply(Interpreter.scala:980)
at scala.util.control.Exception$Catch.apply(Exception.scala:79)
at scala.tools.nsc.Interpreter$Request.loadAndRun(Interpreter.scala:979)
at scala.tools.nsc.Interpreter.loadAndRunReq$1(Interpreter.scala:578)
at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:597)
at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:575)
at scala.tools.nsc.InterpreterLoop.reallyInterpret$1(InterpreterLoop.scala:471)
at scala.tools.nsc.InterpreterLoop.interpretStartingWith(InterpreterLoop.scala:514)
at scala.tools.nsc.InterpreterLoop.command(InterpreterLoop.scala:361)
at scala.tools.nsc.InterpreterLoop.processLine$1(InterpreterLoop.scala:242)
at scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:248)
at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:558)
at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:609)
at org.jetbrains.plugins.scala.compiler.rt.ConsoleRunner.main(ConsoleRunner.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
我不明白为什么如果我能够插入行,我会收到此错误以几乎相同的方式进入表中。如果我尝试在控制台执行 transaction { from(IRCDB.channels)(c => select(c)) }
我会收到相同的错误。
编辑:我添加了完整的堆栈跟踪。另外,我重写了 getAllChannels 函数,如下所示:
def getAllChannels = transaction {
val ret = from(channels)(c => select(c))
println("sql: " + ret)
ret
}
我仍然收到错误,但它确实首先打印出查询 - 我认为这可能有用:
sql: 'QueryExpressionNode[root:1a0d111]:rsm='ResultSetMapper:fdf48d()--
'FieldSelectElement:ChannelTable1_name
org.squeryl.dsl.ast.FieldSelectElement$$anon$3
'FieldSelectElement:ChannelTable1_i_mode
org.squeryl.dsl.ast.FieldSelectElement$$anon$3
'FieldSelectElement:ChannelTable1_s_mode
org.squeryl.dsl.ast.FieldSelectElement$$anon$3
'FieldSelectElement:ChannelTable1_key
org.squeryl.dsl.ast.FieldSelectElement$$anon$3
'FieldSelectElement:ChannelTable1_n_mode
org.squeryl.dsl.ast.FieldSelectElement$$anon$3
'FieldSelectElement:ChannelTable1_m_mode
org.squeryl.dsl.ast.FieldSelectElement$$anon$3
'FieldSelectElement:ChannelTable1_id
org.squeryl.dsl.ast.FieldSelectElement$$anon$3
'FieldSelectElement:ChannelTable1_t_mode
org.squeryl.dsl.ast.FieldSelectElement$$anon$3
'FieldSelectElement:ChannelTable1_p_mode
org.squeryl.dsl.ast.FieldSelectElement$$anon$3
'ViewExpressionNode[sample:ChannelTable[16e3f87]]:rsm='ResultSetMapper:442b95($(1->ChannelTable.name:java.lang.String),$(2->ChannelTable.i_mode:java.lang.Boolean),$(3->ChannelTable.s_mode:java.lang.Boolean),$(4->ChannelTable.key:Option[java.lang.String]),$(5->ChannelTable.n_mode:java.lang.Boolean),$(6->ChannelTable.m_mode:java.lang.Boolean),$(7->ChannelTable.id:java.lang.Long),$(8->ChannelTable.t_mode:java.lang.Boolean),$(9->ChannelTable.p_mode:java.lang.Boolean))--*
Select
ChannelTable1.name as ChannelTable1_name,
ChannelTable1.i_mode as ChannelTable1_i_mode,
ChannelTable1.s_mode as ChannelTable1_s_mode,
ChannelTable1.key as ChannelTable1_key,
ChannelTable1.n_mode as ChannelTable1_n_mode,
ChannelTable1.m_mode as ChannelTable1_m_mode,
ChannelTable1.id as ChannelTable1_id,
ChannelTable1.t_mode as ChannelTable1_t_mode,
ChannelTable1.p_mode as ChannelTable1_p_mode
From
ChannelTable ChannelTable1
java.lang.RuntimeException: no session is bound to current thread, a session must be created via Session.create
and bound to the thread via 'work' or 'bindToCurrentThread'
at scala.Predef$.error(Predef.scala:58)
at org.squeryl.Session$$anonfun$currentSession$1.apply(Session.scala:117)
at org.squeryl.Session$$anonfun$currentSession$1.apply(Session.scala:117)
at scala.Option.getOrElse(Option.scala:59)
at org.squeryl.Session$.currentSession(Session.scala:116)
at org.squeryl.dsl.AbstractQuery.org$squeryl$dsl$AbstractQuery$$_dbAdapter(AbstractQuery.scala:136)
at org.squeryl.dsl.AbstractQuery$$anon$1.<init>(AbstractQuery.scala:140)
at org.squeryl.dsl.AbstractQuery.iterator(AbstractQuery.scala:138)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:79)
at org.squeryl....
Update:
Okay, I fixed the problem I was having, but I'm still not quite sure what I was doing wrong. In any case, I wrote the following method in IRCDB:
def tryit[T](p: => T) = {
Class.forName("org.h2.Driver") //what's the point in this...?
SessionFactory.concreteFactory = Some(() =>
Session.create(java.sql.DriverManager.getConnection("jdbc:h2:~/irc","ScalaIRC",""), new H2Adapter))
transaction {
p
}
}
Then used it like so:
val query = from(IRCDB.channels)(c => select(c))
IRCDB.tryit {
for (r <- query) println("chan: " + r.name)
}
And it now works. From my understanding, I believed that once the session was created using the SessionFactory, I'd be able to use transaction { ... }
anywhere that has the proper import. Apparently, my understanding is incorrect.
If anyone has anything to add please do.
So, I just started using Squeryl for a project I'm working on so this might just be an oversight on my part. So, first the code:
I have the following imports:
import org.squeryl.PrimitiveTypeMode._
import java.sql.Timestamp
import java.sql.DriverManager
import java.util.Date
import org.squeryl.adapters.H2Adapter
import org.squeryl.dsl.{OneToMany, ManyToOne, CompositeKey2}
import org.squeryl._
I have the following table defined:
class ChannelTable(val id: Long, val name : String,
val p_mode : Boolean, val s_mode : Boolean,
val i_mode : Boolean, val t_mode : Boolean,
val n_mode : Boolean, val m_mode : Boolean,
val key : Option[String]) extends KeyedEntity[Long] {
def this() = this(0,"", false, false, false, false, false, false, Some(""))
lazy val bans:OneToMany[ChannelBanTable] = IRCDB.channelToChanBans.left(this)
lazy val users = IRCDB.channelUsers.left(this)
lazy val invites = IRCDB.channelInvites.left(this)
}
And the following Schema defined:
object IRCDB extends Schema {
val channels = table[ChannelTable]
on(channels)(c => declare(
c.p_mode defaultsTo(false),
c.s_mode defaultsTo(false),
c.i_mode defaultsTo(false),
c.t_mode defaultsTo(false),
c.n_mode defaultsTo(false),
c.m_mode defaultsTo(false),
c.name is(unique, indexed)
))
def init {
Class.forName("org.h2.Driver") //what's the point in this...?
SessionFactory.concreteFactory = Some(() =>
Session.create(DriverManager.getConnection("jdbc:h2:~/irc","ScalaIRC",""), new H2Adapter))
}
def getAllChannels = transaction { from(channels)(c => select(c))}
}
I have been able to insert into the table with the following at the Console:
scala> import db._;import org.squeryl.PrimitiveTypeMode._
import db._
import org.squeryl.PrimitiveTypeMode._
scala> IRCDB.init
scala> transaction { IRCDB.channels.insert(new ChannelTable(0,"#chan_name", false, false, false, false, false, false, None)) }
res3: db.ChannelTable = db.ChannelTable@4
I can confirm with the H2 Console that this does indeed occur. However, when I call IRCDB.getAllChannels
(in both the Scala Console and in code) I get the following error:
java.lang.RuntimeException: no session is bound to current thread, a session must be created via Session.create
and bound to the thread via 'work' or 'bindToCurrentThread'
at scala.Predef$.error(Predef.scala:58)
at org.squeryl.Session$anonfun$currentSession$1.apply(Session.scala:117)
at org.squeryl.Session$anonfun$currentSession$1.apply(Session.scala:117)
at scala.Option.getOrElse(Option.scala:59)
at org.squeryl.Session$.currentSession(Session.scala:116)
at org.squeryl.dsl.AbstractQuery.org$squeryl$dsl$AbstractQuery$_dbAdapter(AbstractQuery.scala:136)
at org.squeryl.dsl.AbstractQuery$anon$1.<init>(AbstractQuery.scala:140)
at org.squeryl.dsl.AbstractQuery.iterator(AbstractQuery.scala:138)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:79)
at org.squeryl.dsl.AbstractQuery.foreach(AbstractQuery.scala:27)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:206)
at org.squeryl.dsl.AbstractQuery.map(AbstractQuery.scala:27)
at scala.runtime.ScalaRunTime$.inner$1(ScalaRunTime.scala:255)
at scala.runtime.ScalaRunTime$.stringOf(ScalaRunTime.scala:258)
at RequestResult$line7$object$.<init>(<console>:12)
at RequestResult$line7$object$.<clinit>(<console>)
at RequestResult$line7$object.scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.Interpreter$Request$anonfun$loadAndRun$1$anonfun$apply$18.apply(Interpreter.scala:981)
at scala.tools.nsc.Interpreter$Request$anonfun$loadAndRun$1$anonfun$apply$18.apply(Interpreter.scala:981)
at scala.util.control.Exception$Catch.apply(Exception.scala:79)
at scala.tools.nsc.Interpreter$Request$anonfun$loadAndRun$1.apply(Interpreter.scala:980)
at scala.tools.nsc.Interpreter$Request$anonfun$loadAndRun$1.apply(Interpreter.scala:980)
at scala.util.control.Exception$Catch.apply(Exception.scala:79)
at scala.tools.nsc.Interpreter$Request.loadAndRun(Interpreter.scala:979)
at scala.tools.nsc.Interpreter.loadAndRunReq$1(Interpreter.scala:578)
at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:597)
at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:575)
at scala.tools.nsc.InterpreterLoop.reallyInterpret$1(InterpreterLoop.scala:471)
at scala.tools.nsc.InterpreterLoop.interpretStartingWith(InterpreterLoop.scala:514)
at scala.tools.nsc.InterpreterLoop.command(InterpreterLoop.scala:361)
at scala.tools.nsc.InterpreterLoop.processLine$1(InterpreterLoop.scala:242)
at scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:248)
at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:558)
at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:609)
at org.jetbrains.plugins.scala.compiler.rt.ConsoleRunner.main(ConsoleRunner.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
I don't understand why I would be getting this error if I was able to insert rows into the table doing nearly the same way. If I try to execute transaction { from(IRCDB.channels)(c => select(c)) }
at the Console I get the same error.
Edit: I've added the full stack trace. Also, I rewrote the getAllChannels
function like this:
def getAllChannels = transaction {
val ret = from(channels)(c => select(c))
println("sql: " + ret)
ret
}
I still receive an error, but it does print the query out first -- I thought this may be useful:
sql: 'QueryExpressionNode[root:1a0d111]:rsm='ResultSetMapper:fdf48d()--
'FieldSelectElement:ChannelTable1_name
org.squeryl.dsl.ast.FieldSelectElement$anon$3
'FieldSelectElement:ChannelTable1_i_mode
org.squeryl.dsl.ast.FieldSelectElement$anon$3
'FieldSelectElement:ChannelTable1_s_mode
org.squeryl.dsl.ast.FieldSelectElement$anon$3
'FieldSelectElement:ChannelTable1_key
org.squeryl.dsl.ast.FieldSelectElement$anon$3
'FieldSelectElement:ChannelTable1_n_mode
org.squeryl.dsl.ast.FieldSelectElement$anon$3
'FieldSelectElement:ChannelTable1_m_mode
org.squeryl.dsl.ast.FieldSelectElement$anon$3
'FieldSelectElement:ChannelTable1_id
org.squeryl.dsl.ast.FieldSelectElement$anon$3
'FieldSelectElement:ChannelTable1_t_mode
org.squeryl.dsl.ast.FieldSelectElement$anon$3
'FieldSelectElement:ChannelTable1_p_mode
org.squeryl.dsl.ast.FieldSelectElement$anon$3
'ViewExpressionNode[sample:ChannelTable[16e3f87]]:rsm='ResultSetMapper:442b95($(1->ChannelTable.name:java.lang.String),$(2->ChannelTable.i_mode:java.lang.Boolean),$(3->ChannelTable.s_mode:java.lang.Boolean),$(4->ChannelTable.key:Option[java.lang.String]),$(5->ChannelTable.n_mode:java.lang.Boolean),$(6->ChannelTable.m_mode:java.lang.Boolean),$(7->ChannelTable.id:java.lang.Long),$(8->ChannelTable.t_mode:java.lang.Boolean),$(9->ChannelTable.p_mode:java.lang.Boolean))--*
Select
ChannelTable1.name as ChannelTable1_name,
ChannelTable1.i_mode as ChannelTable1_i_mode,
ChannelTable1.s_mode as ChannelTable1_s_mode,
ChannelTable1.key as ChannelTable1_key,
ChannelTable1.n_mode as ChannelTable1_n_mode,
ChannelTable1.m_mode as ChannelTable1_m_mode,
ChannelTable1.id as ChannelTable1_id,
ChannelTable1.t_mode as ChannelTable1_t_mode,
ChannelTable1.p_mode as ChannelTable1_p_mode
From
ChannelTable ChannelTable1
java.lang.RuntimeException: no session is bound to current thread, a session must be created via Session.create
and bound to the thread via 'work' or 'bindToCurrentThread'
at scala.Predef$.error(Predef.scala:58)
at org.squeryl.Session$anonfun$currentSession$1.apply(Session.scala:117)
at org.squeryl.Session$anonfun$currentSession$1.apply(Session.scala:117)
at scala.Option.getOrElse(Option.scala:59)
at org.squeryl.Session$.currentSession(Session.scala:116)
at org.squeryl.dsl.AbstractQuery.org$squeryl$dsl$AbstractQuery$_dbAdapter(AbstractQuery.scala:136)
at org.squeryl.dsl.AbstractQuery$anon$1.<init>(AbstractQuery.scala:140)
at org.squeryl.dsl.AbstractQuery.iterator(AbstractQuery.scala:138)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:79)
at org.squeryl....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我怀疑的,你的方法:
def getAllChannels = transaction { from(channels)(c => select(c))}
返回一个未评估查询,即
的返回类型为
from(channels)(c => select(c))
Query[ChannelTable]
所以该方法返回,交易结束,控制台尝试打印它,
这会触发评估:
由于交易已结束,您会收到您收到的消息。
试试这个:
Here's what I suspect, your method :
def getAllChannels = transaction { from(channels)(c => select(c))}
is returning an unevaluated query, i.e.
the return type of
from(channels)(c => select(c))
is
Query[ChannelTable]
so the method returns, the transaction is ended, the console tries to print it,
which triggers an evaluation :
since the transaction is ended, you get the message you are getting.
Try this :
也许,在将结果打印到控制台时出现错误,并且查询正常。请提供完整的堆栈跟踪。
Maybe, error appears while printing the result to console and the query was ok. Please, provide full stacktrace.