Note that there are some explanatory texts on larger screens.

plurals
  1. POSession must be bound error using Squeryl
    primarykey
    data
    text
    <p><strong>Update:</strong> 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:</p> <pre><code>def tryit[T](p: =&gt; T) = { Class.forName("org.h2.Driver") //what's the point in this...? SessionFactory.concreteFactory = Some(() =&gt; Session.create(java.sql.DriverManager.getConnection("jdbc:h2:~/irc","ScalaIRC",""), new H2Adapter)) transaction { p } } </code></pre> <p>Then used it like so: </p> <pre><code> val query = from(IRCDB.channels)(c =&gt; select(c)) IRCDB.tryit { for (r &lt;- query) println("chan: " + r.name) } </code></pre> <p>And it now works. From my understanding, I believed that once the session was created using the SessionFactory, I'd be able to use <code>transaction { ... }</code> anywhere that has the proper import. Apparently, my understanding is incorrect. </p> <p>If anyone has anything to add please do.</p> <hr> <p>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:</p> <p>I have the following imports:</p> <pre><code>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._ </code></pre> <p>I have the following table defined:</p> <pre><code>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) } </code></pre> <p>And the following Schema defined:</p> <pre><code>object IRCDB extends Schema { val channels = table[ChannelTable] on(channels)(c =&gt; 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(() =&gt; Session.create(DriverManager.getConnection("jdbc:h2:~/irc","ScalaIRC",""), new H2Adapter)) } def getAllChannels = transaction { from(channels)(c =&gt; select(c))} } </code></pre> <p>I have been able to insert into the table with the following at the Console:</p> <pre><code>scala&gt; import db._;import org.squeryl.PrimitiveTypeMode._ import db._ import org.squeryl.PrimitiveTypeMode._ scala&gt; IRCDB.init scala&gt; transaction { IRCDB.channels.insert(new ChannelTable(0,"#chan_name", false, false, false, false, false, false, None)) } res3: db.ChannelTable = db.ChannelTable@4 </code></pre> <p>I can confirm with the H2 Console that this does indeed occur. However, when I call <code>IRCDB.getAllChannels</code> (in both the Scala Console and in code) I get the following error:</p> <pre><code>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.&lt;init&gt;(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$.&lt;init&gt;(&lt;console&gt;:12) at RequestResult$line7$object$.&lt;clinit&gt;(&lt;console&gt;) at RequestResult$line7$object.scala_repl_result(&lt;console&gt;) 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) </code></pre> <p>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 <code>transaction { from(IRCDB.channels)(c => select(c)) }</code> at the Console I get the same error. </p> <hr> <p><strong>Edit:</strong> I've added the full stack trace. Also, I rewrote the <code>getAllChannels</code> function like this:</p> <pre><code>def getAllChannels = transaction { val ret = from(channels)(c =&gt; select(c)) println("sql: " + ret) ret } </code></pre> <p>I still receive an error, but it does print the query out first -- I thought this may be useful:</p> <pre><code>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-&gt;ChannelTable.name:java.lang.String),$(2-&gt;ChannelTable.i_mode:java.lang.Boolean),$(3-&gt;ChannelTable.s_mode:java.lang.Boolean),$(4-&gt;ChannelTable.key:Option[java.lang.String]),$(5-&gt;ChannelTable.n_mode:java.lang.Boolean),$(6-&gt;ChannelTable.m_mode:java.lang.Boolean),$(7-&gt;ChannelTable.id:java.lang.Long),$(8-&gt;ChannelTable.t_mode:java.lang.Boolean),$(9-&gt;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.&lt;init&gt;(AbstractQuery.scala:140) at org.squeryl.dsl.AbstractQuery.iterator(AbstractQuery.scala:138) at scala.collection.IterableLike$class.foreach(IterableLike.scala:79) at org.squeryl.... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload