PlaySlick Database Connection to PostgreSQL - postgresql

I just started working with Play Framework in Scala for a school project so I don't know much about the the Framework yet. I'm trying to connect my project to PostgreSql using slick but it is not working. I did exactly the same I have found in the Play website and in many tutorials but it doesn't work.
This is my DB config in the application.conf file
slick.dbs.default.driver="slick.driver.PostgresDriver$"
slick.dbs.default.db.driver="org.postgresql.Driver"
slick.dbs.default.db.url="jdbc:postgresql://localhost:5432/IRProject"
slick.dbs.default.db.user="<my_username>"
slick.dbs.default.db.password="<my_password>"
1.sql file in conf/evolutions/default
# --- !Ups
CREATE TABLE Document(
id SERIAL PRIMARY KEY NOT NULL,
title VARCHAR(500),
date TIMESTAMP WITH TIME ZONE,
url VARCHAR(1000) NOT NULL ,
path VARCHAR(1000) NOT NULL ,
summary VARCHAR(5000) NOT NULL
);
# --- !Downs
DROP TABLE IF EXISTS Document;
My dependencies:
"org.webjars" % "jquery" % "2.1.3",
"com.typesafe.play" %% "play-slick" % "1.1.1",
"com.typesafe.play" %% "play-slick-evolutions" % "1.1.1",
"postgresql" % "postgresql" % "9.1-901.jdbc4"
When I run using activator, I don't see any logs about DB connection or any error at all. The application just starts up but without any tables in the database.
Does any one know a reason for this?

Should anyone come across this problem, these are the solutions I found.
I placed the slick db config in the db{} block inside the config file but that is wrong, it should be outside directly inside the config file.
I needed to add a test query that can be used to test the connection e.g
slick.dbs.default.db.connectionTestQuery="SELECT TRUE"
After this it worked.

Related

Slick can't connect to SQL Server Database

I'm running a Scala-Play back end and i'm trying to get slick 2.1.0 to connect to my microsoft sql server database. However i'm constantly getting "Can't Connect To Database" every-time i load up.
These are my settings i'm running slick 2.1.0
slick-extensions 2.1.0 gives me a "unknown artifact" in my IDE
libraryDependencies ++= Seq(
cache, ws, filters, jdbc, evolutions,
"com.typesafe.slick" %% "slick-extensions" % "2.1.0",
"com.typesafe.slick" %% "slick" % "2.1.0"
)
My Application.conf.
db.default {
driver = "com.typesafe.slick.driver.ms.SQLServerDriver"
url = """jdbc:sqlserver://SKYNET\DEV:40000"""
username = "XXX"
password = "XXXXXXXX"
}
Any help or push in the right direction would be greatly appreciated. The JDBC connection url is 100% not the problem because iv'e tested that JDBC string in my IDE to connect.
I think its because i can't get the driver from slick-extensions.
EDIT: Have the resolver in my built.sbt aswell.
EDIT UPDATE: I think after some Github slick reading iv'e progressed the new problem is "Cannot load Driver[Driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] cannot be instantiated.]"
Updated Code:
db.default {
slickdriver = "com.typesafe.slick.driver.ms.SQLServerDriver"
driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
url = "jdbc:sqlserver://SKYNET\\DEV:40000"
username = "XXXXX"
password = "XXXXXXXXXXXXXXXXXXXX"
}
"com.microsoft.sqlserver" % "mssql-jdbc" % "6.2.1.jre8",
Added into my libraryDependencies in SBT fixed it.

Table "play_evolutions" not found

I have a basic development database configured like this:
# Default database configuration using H2 database engine in an in-memory mode
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play;DB_CLOSE_DELAY=-1;MODE=MYSQL;DATABASE_TO_UPPER=FALSE"
I added a script to create the tables from the database in conf/evolutions/default/1.sql
When starting the application with sbt run and going to any page, Play asks me to apply the script 1.sql. After pressing the button, I get the following error:
JdbcSQLException: Table "play_evolutions" not found; SQL statement: update play_evolutions set last_problem = ? where id = ? [42102-192]`en`
Shouldn't the play framework already create this?
Versions:
//project/plugins.sbt
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.10")
//build.sbt
lazy val root =
(project in file("."))
.enablePlugins(PlayScala)
scalaVersion := "2.11.8"
libraryDependencies += jdbc
libraryDependencies += evolutions
I have checked the 1.sql in evolutions.default package, and there might be something that causes the table not found thing.
USE mydb;
With this statement, your "working schema" is now jdbc:h2:mem:play/mydb.
However the play_evolutions table is created just under the root jdbc:h2:mem:play, meaning nothing under jdbc:h2:mem:play/mydb called play_evolutions, which causes the ERROR.
The solution is easy:
remove USE mydb;
Hope it helps.

Trying to connect postgresql heroku with play

I'm trying to use heroku postgresql with play framework (version 2.5.9) but it doesn't work...
build.sbt
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs,
javaJpa,
"org.webjars" % "bootstrap" % "2.3.1",
"org.hibernate" % "hibernate-entitymanager" % "5.2.3.Final",
"postgresql" % "postgresql" % "9.1-901-1.jdbc4"
)
conf/application.conf
ebean.default = ["models.*"]
db.default.driver=org.postgresql.Driver
db.default.url="postgres://ambpfzhcrpupmx:PeSnt07zQFVWKQrsqcRd1GzW6I#ec2-54-227-245-197.compute-1.amazonaws.com:5432/d4i5fck68498ce"
default.username = hereMyUserNameFromHeroku
default.password = "hereMyPassNameFromHeroku"
I obtain :
Caused by: org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "78.192.102.83", user "ambpfzhcrpupmx", database "d4i5fck68498ce", SSL off
I dont' understand why...
I'm trying to add "?ssl=true" at the end of my url but it doesn't work too... I obtain :
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Anybody can help me plz ? Thank :)
First, reset your Postgres username and password because you've posted them publicly and anyone can access your database now. Run this command:
$ heroku pg:credentials DATABASE --reset
Then change your conf/application.conf to use the JDBC_DATABASE_URL environment variable as per the Play Framework documentation.
db.default.url=${?JDBC_DATABASE_URL}
db.default.username=${?JDBC_DATABASE_USERNAME}
db.default.password=${?JDBC_DATABASE_PASSWORD}
The underlying problem is that your database URL does not enable SSL, and your Postgres JDBC dependency is too old for it to be turned on automatically. I recommend updating to this:
"org.postgresql" % "postgresql" % "9.4.1211"

Play Framework trying to access DB with windows username

I have a db.conf which looks like this in the conf folder:
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://62.210.145.112/babybets"
db.default.username=postgres
db.default.password="my_password"
It is included in the application.conf:
include "db.conf"
Infos are definitely correct, as I use this very same jdbc connection string, user/pass to connect the Intellij DB view to the database.
When I try to access any page, I get an error:
play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]]
at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:94) ~[play_2.10-2.3.8.jar:2.3.8]
at play.api.Configuration.reportError(Configuration.scala:743) ~[play_2.10-2.3.8.jar:2.3.8]
at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:247) ~[play-jdbc_2.10-2.3.2.jar:2.3.2]
at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:238) ~[play-jdbc_2.10-2.3.2.jar:2.3.2]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library-2.10.5.jar:na]
Caused by: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "John"
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:293) ~[postgresql-9.1-901-1.jdbc4.jar:na]
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:108) ~[postgresql-9.1-901-1.jdbc4.jar:na]
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66) ~[postgresql-9.1-901-1.jdbc4.jar:na]
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125) ~[postgresql-9.1-901-1.jdbc4.jar:na]
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30) ~[postgresql-9.1-901-1.jdbc4.jar:na]
In the above you can see it tries to connect to the database using my windows login "John".
I went through the project with agent Ransack and tried the following (case insensitive) regex:
[^\\]john[^\\]
It only matches with the above error log.
Question: Why is play using my windows login to Connect DB? How to prevent that ?
Just for info my build.sbt looks like this:
name := "babyBets2"
version := "1.0"
lazy val `babybets2` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.10.5"
libraryDependencies ++= Seq( cache , ws )
libraryDependencies ++= Seq("postgresql" % "postgresql" % "9.1-901-1.jdbc4",
"com.typesafe.play" %% "play-slick" % "0.8.0",
"ws.securesocial" %% "securesocial" % "2.1.4" )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
Ok this is crazy, there is a difference between Play versions:
https://www.playframework.com/documentation/2.4.x/ScalaDatabase
https://www.playframework.com/documentation/2.3.x/ScalaDatabase
db.default.username=postgres
vs
db.default.user=postgres
So apparently, if no user is provided, play displays no useful error messages and tries to connect you with windows login!

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.