Skip to content
grob edited this page Feb 17, 2013 · 8 revisions

Requirements

Depending on the database you want to use you need to place the driver .jar file into the "jars" subdirectory of ringo-sqlstore.

Important: sqlstore expects driver files named "mysql.jar", "oracle.jar" resp. "postgresql.jar".

Basics

Setting up Ringo SQLstore is done in a few lines of code. Require the module, create an instance of ConnectionPool with the connection parameters as argument and pass it to the Store constructor:

var {ConnectionPool, Store} = require("ringo-sqlstore");
var connectionPool = module.singleton("connectionPool", function() {
    return new ConnectionPool({
        "url": "jdbc:h2:mem:test",
        "driver": "org.h2.Driver"
    });
});
var store = new Store(connectionPool);

The above example would operate on a H2 in-memory database, which is created when the first connection to the database is opened. An example for an existing MySQL database "test" would look like this:

var connectionPool = module.singleton("connectionPool", function() {
    return new ConnectionPool({
        "url": "jdbc:mysql://localhost/test",
        "driver": "com.mysql.jdbc.Driver",
        "username": "test",
        "password": "test"
    });
});
var store = new Store(connectionPool);

Within one application you can instantiate as many stores as needed, each operating on it's own database. Once the store is set up correctly, you should continue defining entities managed by this store instance.

Why using a singleton for ConnectionPool?

The above examples instantiate the connection pool as a singleton, meaning it will be created only once, regardless how many threads an application uses. This is important: if Ringo SQLstore is used in a multithreaded application, it is viable to use a singleton for the connection pool, otherwise every new thread started in the application would create its own pool, and basically render the connection pool useless.

The same applies to the two caches SQLstore can utilize. Read more about them in the caches section

Clone this wiki locally