Using a database with the Play framework - database

I've been following this tutorial to get accustomed to the Play framework:
http://www.playframework.org/documentation/2.0.2/ScalaTodoList
When reaching the "Persist the tasks in a database" segment, it is unclear to me whether I have to do anything to start up the database or if this is done automatically for me. I am currently receiving this error:
[Exception: DB plugin is not registered.]

It depends on the type of database you use.
H2 database (the one used in Play! tutorial) can be used as an in-memory database or as a disk based database. Play will handle all for you if you go for this database. You don't have to run a specific service to have the persistent database.
But you will have to describe in your application.conf which driver to use and which url to access :
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
As you probably don't have the right driver in your classpath, you would have to include this driver lib. As explained in Play2 documentation, you can manage this in project/Build.scala. For h2 driver lib the dependency will be :
val appDependencies = Seq(
"com.h2database" % "h2" % "1.3.167"
)
After having changed your dependencies, you will need to reload the application build file and update the dependencies :
//in sbt
reload
update
How to link your play application with a database is explained here
If you want to use an other database (Mysql, Postgresql...) you will have to run the database service yourself and repeat the steps described here.

Related

How to create and get started with Embedded Apache Derby database in Dropwizard project (Angular 7 front-end)

I'm reading through Derby documentation and following all the instructions. I've successfully installed it (extracted it to my Linux machine and set the DERBY_HOME path). I have a complete REST API project with Angular 7 front-end and Dropwizard backend. I hard coded some data in the backend, and created all the HTTP API methods I need (GET, POST, PATCH, DELETE).
The application is fully functional, but now I need to implement the Embedded version of Derby into it. I have 0 experience with such databases, and because Dropwizard gave me enough trouble already, I cannot figure out how to get started.
Do I create a new class and get started there, how to create those SQL files and how to store data? I can't find a concrete answer to similar questions, if there are detailed explanations and examples already out there, please feel free to provide me with the resources. I know this is a noob question, but I just barely learned how HTTP works (the basics) and managed to completely create a functional REST using Angular and Dropwizard.
Consider the embedded database like a full-fledged database, that instead of being in a different environment, and maybe requiring a network connection, is packed along with your application and run in the same JVM. The same mechanisms applies between the two.
The embedded Derby Driver is located inside the derby.jar file, so it is required to have it in the classpath of your application. It should be located under %DERBY_INSTALL%\lib\, where %DERBY_INSTALL% is the installation directory. You can see by the image where it is contained.
From Oracle
Any JDBC 4.0 drivers that are found in your class path are
automatically loaded. (However, you must manually load any drivers
prior to JDBC 4.0 with the method Class.forName.)
What that means is that if the Derby driver is a JDBC 4.0 driver, you don't have to do anything else besided getting a connection via DriverManager.
If it is not a JDBC 4.0 driver, you'll have to instantiate the Driver with
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Appartently you'll need that piece of code above.
Now simply get a hold on a Connection object.
DriverManager.getConnection("jdbc:derby:dbName;create=true");
From there on, you can create Statement(s) as you like. Which means you can create tables, insert rows, update rows, delete rows, etc.
To gracefully shutdown the embedded Derby database, you need to use
DriverManager.getConnection("jdbc:derby:dbName;shutdown=true"); // see the same database name "dbName"
prior to quitting the main application. It is not mandatory, but recommended.
You can create a utility class to hold an EmbeddedDataSource (docs), which will provide connections around your application.
public final class EmbeddedDerby {
private static final DataSource DATA_SOURCE;
static {
// Initialize DATA_SOURCE with EmbeddedDataSource
}
...
public static Connection getConnection() {
return DATA_SOURCE.getConnection();
}
}

TinyVirtuoso: where is the documentation?

I found this NuGet package interesting: a embedded rdf database in C# based on OpenLink Virtuoso, with MIT licensing...
But, when I tried to find any instruction about usage, or the API reference, I couldn't find it...
I installed TinyVirtuoso.win v 7.2.1 from NuGet ...
Why doesn't the sample code in "First Steps" work?
It does not recognize VirtuosoConnection() nor IStore
using Semiodesk.TinyVirtuoso;
using Semiodesk.VirtuosoInstrumentation;
// Create a new Virtuoso store in a directory named 'Data', located in your app folder.
var virtuoso = new TinyVirtuoso("Data");
// You can have multiple separate database instances which contain different data.
// This will create a directory "Data/ExampleProject" in your app directory.
var instance = virtuoso.GetOrCreateInstance("ExampleProject");
// Start the embedded database server instance.
instance.Start();
// a) You can access the store using the Semiodesk Trinity graph database API.
// Please refer to our Trinity documentation and examples for more information.
IStore store = StoreFactory.CreateStore(instance.GetTrinityConnectionString());
// b) Alternatively, you can use it with plain ADO.Net to use the relational
// database features and SQL (make sure to install OpenLink.Data.Virtuoso).
var connection = new VirtuosoConnection();
connection.ConnectionString = instance.GetAdoNetConnectionString();
connection.Connect();
// When you are done, stop the embedded database server instance.
instance.Stop();
Looking it over quickly, I think TinyVirtuoso is not an "embedded RDF database in C#" (which is not surprising, as that would require portation of the entire C-based VOS (Virtuoso Open Source) project). Note that their wiki says —
TinyVirtuoso does not link against OpenLink Virtuoso in any way. It just provides a way to start, stop and configure the software.
Elsewhere in that wiki, they say they depend on the Open Source Virtuoso project, but it's not clear whether they fully install Virtuoso, just install the ADO.NET client library, or somewhere in between.
I did notice this advice --
The software is supported by Semiodesk. If you have any questions, suggestions or just want to tell us in which projects you are using the library, don't hesitate to hello#semiodesk.com.
-- which led me to a somewhat more informative (and I think more recently updated) page on Semiodesk's website, which suggests that they do indeed intend bundling the main Virtuoso binary, among other things, which I don't think you've installed yet.
Documentation of Virtuoso itself, and its ADO.NET Provider (both Installation/Configuration and Programming/API), are found on the OpenLink website.
I hope this is helpful...

Create h2 database with Spring Boot if it not exists, then don't delete. Desktop application

I am writing a desktop applicaiton, which requires language dictationary.
I want my application to create h2 database only when user first time run my application, and then load translations into db from .xdxf dictationary.
After a quick looking through a few articles I got that the common use case is to create a new schema every time when application starts and destroy it on exit. Did I get it correctly?
Is there a way to keep created schema after application was stopped?
P.S. any link for suitable tutorial will be enougth for me. Thanks.
You are referring to what Spring Boot does by default. You can configure it in many ways, reading the documentation should help.
H2 can also be configured in many ways, including file-based persistence (i.e. surviving a restart of the application).
With your current setup that works with H2 in memory, you could give this configuration a try and look at the doc for the remaining pieces:
spring.datasource.url = jdbc:h2:file:~/testdb
We will figure out the driver based on the URL. Note that since you took control over this setup, Hibernate won't be configured to create the schema automatically on startup (if you were relying on that). Check this question for more details.

HealthChecks in DropWizard 0.8.0-rc1

I am following the documentation to create a healthcheck for my dropwizard application for database connectivity. I wanted to know how does the healthScript get a Database class? What do I need to import in order to use teh Database class mentioned in the Documentation?
http://dropwizard.io/0.8.0-rc1/docs/manual/core.html#health-checks
This is just an example providing a general idea of what can be accomplished with healthchecks. There isn't a concrete Database class that you can import. The actual healthcheck would depend on your choice of DB connection: JDBI or Hibernate. Dropwizard also easily allows you to define your own DAO for whatever vendor / protocol you need. And if you read through that documentation you'll see that those database objects are configured just like everything else - with YAML.
Note that you can upgrade to the 0.8.0 release instead of the release candidate.

Zend Framework: Getting started using SQLite

Sorry if this is overly simplistic.
I've decided that I want to use an SQLite database instead of a MySQL database. I'm trying to wrap my head around how simple SQLite is and would like a simple, one answer tutorial on how to use SQLite with the Zend Framework, where to put my SQLite database in my directory structure, how to create the database, etc.
#tuinstoel is correct, attaching to an SQLite database implicitly creates it if it does not exist.
SQLite also supports a command-line client that is more or less like MySQL's command shell, allowing you to issue ad hoc commands or run SQL scripts. See documentation here: http://www.sqlite.org/sqlite.html
Of course you need to change the Zend_Db adapter in your ZF application. ZF supports only an adapter to the PDO SQLite extension. SQLite doesn't support user/password credentials. Also since SQLite is an embedded database instead of client/server, the "host" parameter is meaningless.
$db = Zend_Db::factory("pdo_sqlite", array("dbname"=>"/path/to/mydatabase.db"));
One more caveat: when you get query results in associative-array format, some versions of SQLite insist on using "tablename.columnname" as the keys in the array, whereas other brands of database return keys as simply "columnname". There's an outstanding bug in ZF about this, to try to compensate and make SQLite behave consistently with the other adapters, but the bug is unresolved.
If you make a connection to a not existing database, a database is created on the fly. (You can turn this behavour off)
This is now covered in the Zend Framework quickstart tutorial (version 1.9.5 as of this writing). Just make a new project (with zf command line tool. look here for a great tutorial on setting it up), add these lines to your config.ini file and you're good to go:
; application/configs/application.ini
[production]
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/databaseName.db"
Now when you ask for your default database adapter, it will use this one. I would also recommend downloading the quickstart tutorial source code and making use of the load.sqlite.php script. You can create a schema and data file and load the database with these tables/columns/values. It's very helpful! Just check out the tutorial. It's all in there.
This answer was moved out of the question into a CW answer to disavow ownership over the content.

Resources