Scala / Connect Database Connections

I am working on a new web application using Scala with Lift. I want to make it reusable so that others can install it on their servers for their needs. I am leaving a PHP background where it is common practice to create an installation form with a request for details of connecting to the database. This information is stored in a configuration file and is used by the rest of the PHP application to connect to the database. This is very user-friendly, because everything is contained in a directory storing PHP files. They are free to define everything else. My Java / Scala background was corporate, where the application was intended only to work in the database that we installed for it. It is not intended to be installed on other web servers or in different databases.

So my question is, how is this usually done for the Java / Scala world? If there are open source applications that implement the core solution, feel free to point to them too.

+3
source share
2 answers

I use this to configure the database:

val vendor =
  new StandardDBVendor(
    Props.get("db.driver") openOr "org.h2.Driver",
    Props.get("db.url") openOr "jdbc:h2:mem:db;AUTO_SERVER=TRUE",
    Props.get("db.user"), 
    Props.get("db.password"))
LiftRules.unloadHooks.append(vendor.closeAllConnections_! _)
DB.defineConnectionManager(DefaultConnectionIdentifier, vendor)

The "supports" referenced will be (by default) in the default.props file in the props directory in the resources.

Updated . This is what I do on servers in production. Using "Props.whereToLook" you provide a function that retrieves the configuration input stream. It can be a file, as in the example below, or you can, for example, extract it through a network socket.

You probably run the application with an error.

val localFile = () => {
  val file = new File("/opt/jb/myapp/etc/myapp.props")
  if (file.exists) Full(new FileInputStream(file)) else Empty
}
Props.whereToLook = () => (("local", localFile) :: Nil)
+2
source

, .
Lift Scala (Boot.scala) , Lift , , .properties.

Java/ Scala .properties. , .., PHP.

, Boot.scala, .properties , , , .

0

Source: https://habr.com/ru/post/1792701/


All Articles