How to configure hello-slick-3.0 app for mysql (async) - scala

I'm trying to update the hello-slick-3.0 application downloaded using typesafe activator to use mysql rather than the h2 in-mem database. I've changed all the references to h2 to import import slick.driver.MySQLDriver.api._, but I don't know how to force the correct version of this driver at runtime. I'm getting this error at runtime:
background log: info: Running HelloSlick
background log: error: Exception in thread "main" java.sql.SQLException: No suitable driver
I wish to start using the async mysql driver with scala/slick.

You need to use standard MySQL JDBC driver, for example:
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.35"
Slick 3 implements "asynchrony" itself using Reactive Streams.

Related

Slick 3.2 with Postgresql?

The official Slick 3.2 docs
(http://slick.lightbend.com/doc/3.2.0/database.html)
says that Slick can be configured with a normal javax.sql.DataSource such as PGSimpleDataSource or PGPoolingDataSource with this:
val db = Database.forDataSource(dataSource: javax.sql.DataSource, Some(size: Int))
I can't find a Database object to import.
That Database singleton object doesn't even exist in the official ScalaDoc:
http://slick.lightbend.com/doc/3.2.0/api/index.html
I include the following dependencies in my build.sbt. Am I missing a slick-postgresql binding or some other dependency that has the missing Database object specified in the documentation?
"com.typesafe.slick" %% "slick" % "3.2.0"
"org.postgresql" % "postgresql" % "42.0.0"
The Quick Intro section says
// Use H2Profile to connect to an H2 database
import slick.jdbc.H2Profile.api._
Since we are using H2 as our database system, we need to import features from Slick’s H2Profile. A profile’s api object contains all commonly needed imports from the profile and other parts of Slick such as database handling.
So I believe you want to import the PostgresProfile api:
import slick.jdbc.PostgresProfile.api._

Play-ReactiveMongo Authentication Failed with MongoDB 3

I have problem using play-reactivemongo to connect to MongoDB 3 and encountered the "'not authorized for query on XXX' (code = 13)" problem. The program ran perfectly ok when MongoDB was started without --auth.
The database version should be 3.0.4, as shown in the log.
2015-10-24T15:58:09.868+0800 I CONTROL [initandlisten] db version v3.0.4
I have used version 0.11.7 play2-reactivemongo plugin.
libraryDependencies ++= Seq(
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.7.play24",
I have specified the authMode in the mongodb.uri like this.
mongodb.uri = "mongodb://postsUser:12345678#localhost:27017/posts?authMode=?authMode=scram-sha1&authSource=posts&rm.tcpNoDelay=true"
I found the following error in mongodb.log.
2015-10-24T16:41:24.977+0800 I ACCESS [conn105] Failed to authenticate postsUser#posts with mechanism MONGODB-CR: AuthenticationFailed MONGODB-CR credentials missing in the user document
Why MONGODB-CR is used instead of SCRAM-SHA-1? Suppose SCRAM-SHA-1 should be default, right?
Appending following to the connection string works for me
authMode=scram-sha1
so the final connection string is
mongodb://user:pass#ipaddress:27017/litmus?3treadPreference=primary&authMode=scram-sha1
I also have nodejs script connecting to same db, interestingly it connects perfectly without appending authMode=scram-sha1 flag.
Seems to be unhandled case in reactive mongo
Sorry, I made a typo in the mongodb.uri
authMode=?authMode=scram-sha1
and causing the DB to authenticate with mongodb-cr. However, I still got the "not authorized to query" error on first access to DB and subsequent db access were fine. In mongodb.log, I could see the following.
2015-10-25T01:28:45.242+0800 I QUERY [conn33] assertion 13 not authorized for query on posts.posts ns:posts.posts query:{}
2015-10-25T01:28:45.247+0800 I ACCESS [conn33] Successfully authenticated as principal postsUser on posts
I better start another question tomorrow.

What version of Salat/Casbah should I use to connect to Mongo 3.x server?

I am using https://github.com/salat/salat. But it seems that salat is using Casbah 2.7. Our mongo server is 3.0.6. But we are not able to connect and always got this error.
Configuration error: Configuration error[Access denied to MongoDB database: [mg_prod] with user: [mydbuser]]
at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:94)
at play.api.Configuration.reportError(Configuration.scala:743)
at se.radley.plugin.salat.SalatPlugin$MongoSource.connection(SalatPlugin.scala:36)
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:136)
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:131)
Can you please give me some idea how should I fix this issue?
As a workaround you can add
"org.mongodb" %% "casbah" % "2.8.2"
to your build.sbt file and the error should be gone.

Play 2.3.5 and ReactiveMongo: MongoError['No primary node is available!']

I recently upgraded Play to version 2.3.5 and try to use it with ReactiveMongo. However everytime I try to read data from mongoDB an exception occurred. This is my build.sbt:
name := """ReactiveMongoRestExample"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.1"
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
ws,
"org.reactivemongo" %% "play2-reactivemongo" % "0.10.5.0.akka23"
)
This is the stacktrace:
play.api.Application$$anon$1: Execution
exception[[PrimaryUnavailableException$: MongoError['No primary node
is available!']]] at
play.api.Application$class.handleError(Application.scala:296)
~[play_2.11-2.3.5.jar:2.3.5] at
play.api.DefaultApplication.handleError(Application.scala:402)
[play_2.11-2.3.5.jar:2.3.5] at
play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:205)
[play_2.11-2.3.5.jar:2.3.5] at
play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:202)
[play_2.11-2.3.5.jar:2.3.5] at
scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
[scala-library-2.11.2.jar:na] Caused by:
reactivemongo.core.actors.Exceptions$PrimaryUnavailableException$:
MongoError['No primary node is available!'] at
reactivemongo.core.actors.Exceptions$PrimaryUnavailableException$.(actors.scala)
~[reactivemongo_2.11-0.10.5.0.akka23.jar:0.10.5.0.akka23] at
reactivemongo.core.actors.MongoDBSystem$$anonfun$pickChannel$4.apply(actors.scala:508)
~[reactivemongo_2.11-0.10.5.0.akka23.jar:0.10.5.0.akka23] at
reactivemongo.core.actors.MongoDBSystem$$anonfun$pickChannel$4.apply(actors.scala:508)
~[reactivemongo_2.11-0.10.5.0.akka23.jar:0.10.5.0.akka23] at
scala.Option.getOrElse(Option.scala:120)
~[scala-library-2.11.2.jar:na] at
reactivemongo.core.actors.MongoDBSystem.pickChannel(actors.scala:508)
~[reactivemongo_2.11-0.10.5.0.akka23.jar:0.10.5.0.akka23]
MongoDB works fine. I'm able to retrieve data with the commandline tool and IntelliJ.
I pushed the code to github
Maybe someone knows the issue and can help me? That would be awesome
Same issue though updated to Play 2.3.8 with same rx mongo plugin. Mongodb backed works fine under mongo shell as does mongo explorer in intellij idea 14.
Using mongodb.uri in application.conf:
mongodb.uri = "mongodb://pxxxxxxxx:pxxxxxxxx#berne:27017/playdb"
Digging deeper on assumption this is a configuration issue.
Henry
It's probably unable to start the Mongo server.
Go to your /bin folder and run mongo.
You might have too little room available on your temporary folder.
try running : mongo --smallfiles
I had this error because I tried to connect to the db with an unknown user. Creating the user resolved the error.

How to use inmemory data base for Junits in Play 2.2

I am working on play 2.2 for my application. In my application.conf, I have specified the connection properties as below.
db.testdb.driver=org.h2.Driver
db.testdb.url="jdbc:h2:mem:play"
db.testdb.user=xxxxx
db.testdb.password=xxxx
And When I try to access the h2 db in my Jnuit , I am getting connection "testdb" undefined error.
Junit:
FakeApplication app = Helpers.fakeApplication(Helpers.inMemoryDatabase("testdb"));
Helpers.start(app);
Please help me to connect h2 db from my Junit.
Thanks in adv.
Try to use default setting for h2 db:
db.default.driver=org.h2.Driver#
db.default.url="jdbc:h2:mem:play#"
db.default.user=sa
db.default.password=""