0

I am new in Play Framework (Scala) and need some advise.

I use Scala 2.12 and Play Framework 2.6.20. I need to use several databases in my project. Right now I connected MySQL database as it says in documentation. How correctly connect project to remote Oracle 12g database?

application.conf:

db {
  mysql.driver = com.mysql.cj.jdbc.Driver
  mysql.url = "jdbc:mysql://host:port/database?characterEncoding=UTF-8"
  mysql.username = "username"
  mysql.password = "password"
}

First of all to lib folder I put ojdbc8.jar file from oracle website.

Then add libraryDependencies += "com.oracle" % "ojdbc8" % "12.1.0.1" code to sbt.build file. Finally I wrote settings to aplication.conf file.

After that step I notice error in terminal:

[error] (*:update) sbt.ResolveException: unresolved dependency: com.oracle#ojdbc8;12.1.0.1: not found
[error] Total time: 6 s, completed 10.11.2018 16:48:30
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256M; support was removed in 8.0

EDIT:

application.conf:

db {
  mysql.driver = com.mysql.cj.jdbc.Driver
  mysql.url = "jdbc:mysql://@host:@port/@database?characterEncoding=UTF-8"
  mysql.username = "@username"
  mysql.password = "@password"

  oracle.driver = oracle.jdbc.driver.OracleDriver
  oracle.url = "jdbc:oracle:thin:@host:@port/@sid"
  oracle.username = "@username"
  oracle.password = "@password"
}

ERROR:

play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:

1) No implementation for play.api.db.Database was bound.
  while locating play.api.db.Database
    for the 1st parameter of controllers.GetMarkersController.<init>(GetMarkersController.scala:14)
  while locating controllers.GetMarkersController
    for the 7th parameter of router.Routes.<init>(Routes.scala:45)
  at play.api.inject.RoutesProvider$.bindingsFromConfiguration(BuiltinModule.scala:121):
Binding(class router.Routes to self) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)

GetMarkersController.scala:

package controllers

import javax.inject._

import akka.actor.ActorSystem
import play.api.Configuration
import play.api.mvc.{AbstractController, ControllerComponents}
import play.api.libs.ws._
import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future, Promise}
import services._
import play.api.db.Database

class GetMarkersController @Inject()(db: Database, conf: Configuration, ws: WSClient, cc: ControllerComponents, actorSystem: ActorSystem)(implicit exec: ExecutionContext) extends AbstractController(cc) {
    def getMarkersValues(start_date: String, end_date: String) = Action.async {
        getValues(1.second, start_date: String, end_date: String).map {
            message => Ok(message)
        }
    }

    private def getValues(delayTime: FiniteDuration, start_date: String, end_date: String): Future[String] = {
        val promise: Promise[String] = Promise[String]()
        val service: GetMarkersService = new GetMarkersService(db)
        actorSystem.scheduler.scheduleOnce(delayTime) {
            promise.success(service.get_markers(start_date, end_date))
        }(actorSystem.dispatcher)
        promise.future
    }
}
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193

1 Answers1

0

You cannot access Oracle without credentials. You need to have an account with Oracle. Then add something like the following to your build.sbt file

resolvers += "Oracle" at "https://maven.oracle.com"

credentials += Credentials("Oracle", "maven.oracle.com", "username", "password")

More information about accessing the OTN: https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9012

If you have the hard coded jar, you don't need to include as a dependency. See unmanagedDependencies https://www.scala-sbt.org/1.x/docs/Library-Dependencies.html

Daniel Hinojosa
  • 972
  • 5
  • 9
  • Thank you for your answer. I have question. I add code which you recommended but unfortunately steal has the same error. Is it mean that I don't need `libraryDependencies += "com.oracle" % "ojdbc8" % "12.1.0.1"` in `build.sbt` file anymore? – Nurzhan Nogerbek Nov 11 '18 at 07:11
  • Also I notice in terminal this error message: `[error] Unable to find credentials for [OAM 11g @ login.oracle.com].` – Nurzhan Nogerbek Nov 11 '18 at 10:14
  • Yes. It is either or, if you have the jar you don't need to put it in the `libraryDependencies`. You don't need to do both. – Daniel Hinojosa Nov 11 '18 at 14:03
  • I think you found it. Add `credentials += Credentials("OAM 11g", "login.oracle.com", "username", "password")` – Daniel Hinojosa Nov 11 '18 at 14:07
  • I add to `build.sbt` file only that code which you recommended without `libraryDependencies`. Now when I try to take any data from database it raise error. From error I understand that there is steal no connection between Play Framework and remote Oracle database. Can you check my post again please?! – Nurzhan Nogerbek Nov 12 '18 at 03:39
  • @NurzhanNogerbek That error is inside your `GetMarkersController`, you are injecting `play.api.db.Database` and it isn't mapped in your module. What does the `GetMarkersController` `class` look like? – Daniel Hinojosa Nov 12 '18 at 03:57
  • I add `GetMarkersController` class to post. You can check it now again. As you can see it's pretty simple, nothing extraordinary. – Nurzhan Nogerbek Nov 12 '18 at 04:12
  • Check the documentation. You have two databases, `mysql` and `oracle` and none of them will work because they are not called `default` and it doesn't know which one to inject. See: https://www.playframework.com/documentation/2.6.x/ScalaDatabase where it uses `@Named("orders")` to distinguish the databases. You'll need to name yours `@Named("oracle")` and `@Named("mysql") – Daniel Hinojosa Nov 12 '18 at 19:27
  • I think this is now two different questions in one post. If you have further up questions, you may want to do a separate a post (my opinion). ;) – Daniel Hinojosa Nov 12 '18 at 19:29
  • Sorry, should be `@NamedDatabase("orders") ` – Daniel Hinojosa Nov 12 '18 at 20:06
  • How to add jdbc8 in sbt I answered here https://stackoverflow.com/a/56489036/7618482 – avgolubev Jun 13 '19 at 11:07