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._
Related
anyone tried to use this:
X-Ray
with Slick ?
import slick.dbio.DBIO
import slick.jdbc.JdbcBackend.Database
import slick.jdbc.PostgresProfile.api._
private[database] class PostgresConnector extends DatabaseConnector {
protected final val configurationPath = "mycompany.backend.database.postgres"
protected lazy val database = Database.forConfig(configurationPath)
Probably no way because its based on tomcat:
These interceptors are in the aws-xray-recorder-sql-postgres and
aws-xray-recorder-sql-mysql submodules, respectively. They implement
org.apache.tomcat.jdbc.pool.JdbcInterceptor and are compatible with
Tomcat connection pools.
If you are trying to instrument a SQL connection using a provider other than MySQL or Postgres, you can try to use the generic JDBC-based SQL Library, documented here: https://github.com/aws/aws-xray-sdk-java#intercept-jdbc-based-sql-queries
Alternatively, you can use the X-Ray auto-instrumentation agent for Java, which automatically captures all JDBC-based SQL queries.
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.
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.
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.
I have a Play Application based on the play-scala Typesafe template (Play Scala Seed), and tried to add Slick 3.0.0 to the project and connect to a PostgreSQL database.
First I added the dependencies to build.sbt:
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.0.0-RC1",
"postgresql" % "postgresql" % "9.1-901.jdbc4"
)
Then added the database configuration on application.conf:
brDb = {
dataSourceClass = org.postgresql.ds.PGSimpleDataSource
url = "jdbc:postgresql://localhost:5432/test"
user = "postgres"
password = "postgres"
numThreads = 10
}
Note that I haven't disabled explicitly the pooling, so it is enabled by default, and will try to use HikariCP, because as of Slick 3.0.0 RC1, HikariCP support exists and pooling using it is enabled by default.
And in my DAO object, tried to get the database connection like this:
Database.forConfig("brDb")
When I run the app using activator run, I get this error:
RuntimeException: java.lang.NoClassDefFoundError:
com/zaxxer/hikari/HikariConfig
Then I tried adding HikariCP as a dependency in build.sbt:
libraryDependencies ++= Seq(
// ...
"com.zaxxer" % "HikariCP" % "2.3.3",
// ...
)
Cleaned and recompiled the app using activator clean compile, and runned it again, but I get another error:
RuntimeException: java.lang.UnsupportedClassVersionError: com/zaxxer/hikari/HikariConfig
I think I am missing some configuration, but I am not sure and have not found more info about it. How should I set up the configuration to get the connection pool working?
That error means that the HikariCP package is compiled for a JRE newer than the one you#re running on. And in fact, if you look at the homepage, you'll see that the version you are using is this:
Java 8 maven artifact:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.3.5</version>
<scope>compile</scope>
</dependency>
I suppose you're running on Java 7. For that, add the following to your build.sbt:
libraryDependencies ++= Seq(
"com.zaxxer" % "HikariCP-java6" % "2.3.3",
)
Btw, I have found out that the configuration structure that you specified above does not actually work as of Slick 3.0.0. What worked for me was to specify the database configuration as described in the documentation http://slick.typesafe.com/doc/3.0.0/database.html#using-typesafe-config, otherwise Slick will assume some default values for the configuration. I'm mainly talking about the "properties" field.
So something like this should work:
mydb = {
dataSourceClass = "org.postgresql.ds.PGSimpleDataSource"
properties = {
serverName = "postgresdb"
portNumber = "5432"
databaseName = "mydb"
user = "myuser"
password = "secret"
}
numThreads = 10
}