kclay / rethink-scala

Scala Driver for RethinkDB
Other
100 stars 24 forks source link

Wrong result returned when using multiple connection instances #43

Closed gmethvin closed 9 years ago

gmethvin commented 9 years ago

I wanted to do a few initialization tasks with a BlockingConnection and all other queries with an AsyncConnection. My instances were created as follows:

private lazy val version: Version3 = new Version3(conf.host)
implicit lazy val asyncConnection: AsyncConnection = Async(version)
implicit lazy val blockingConnection: BlockingConnection = Blocking(version)

If I run queries on both, I will sometimes get the wrong result returned on one or more of my queries, and usually get an error like java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double when trying to use the result of a query (since the other query was for something completely different).

I suspect there is some shared global state that's causing this, maybe the cache here? https://github.com/kclay/rethink-scala/blob/master/core/src/main/scala/com/rethinkscala/net/CursorFactory.scala#L16

Note that this also happens with two async connections. So I suppose maybe this library just doesn't support using multiple connection instances in the same JVM. If that's the case, I'd prefer to have an explicit check and throw an exception on initialization so I know this ahead of time.

kclay commented 9 years ago

Nice catch, I'll push an snapshot for this tomorrow.

Also in the mean time you can use an Async connection and do the following\

import com.rethinkscala.Async._
block{
implicit c:BlockingConnection=>
import c.delegate._
 r.tableAs[Foo].insert(....).run
}

or 
val result:Either[RethinkError,InsertResult] = block(r.tableAs[Foo].insert(....))

https://github.com/kclay/rethink-scala/blob/master/core/src/main/scala/com/rethinkscala/Implicits.scala#L306

I'll be sure to update the wiki for this as well.

gmethvin commented 9 years ago

@kclay Thanks. So it'll be possible to have multiple connection instances now? In my case it's not absolutely necessary, but it would be if you had connections to two different database hosts.

My workaround was just to use the async connection and call Await.result (since in my case this is in initialization code where exceptions should be fatal).

kclay commented 9 years ago

@gmethvin Correct right now its not possible to have multiple connection instances but you can alleviate the need of explicitly using Await.result by using one of the examples above in your code that needs to block.

kclay commented 9 years ago

@gmethvin did you get to check out the recent 0.4.8-SNAPSHOT

gmethvin commented 9 years ago

It seems like that works. Thanks.