Search

    Databases

    Class cds.DatabaseService and subclasses thereof are technical services representing persistent storage.

    .DatabaseService class

    class .DatabaseService extends .Service

    .DatabaseService — Consumption

    InsertResult (Beta)

    • On INSERT, DatabaseServices return an instance of InsertResult defined as follows:
      • Iterator that returns the keys of the created entries, for example:
        • Example: [...result] -> [{ ID: 1 }, { ID: 2 }, ...]
        • In case of INSERT...as(SELECT...), the iterator returns {} for each row
      • affectedRows: the number inserted (root) entries or the number of affectedRows in case of INSERT into SELECT
      • valueOf(): returns affectedRows such that comparisons like result > 0 can be used

        === can’t be used as it also compares the type

    .DatabaseService — Configuration

    Pool

    Instead of opening and closing a database connection for every request, we use a pool to reuse connections. By default, the following pool configuration is used:

    {
      "acquireTimeoutMillis": <if (NODE_ENV='production') 1000 else 10000>,
      "evictionRunIntervalMillis": <2 * (idleTimeoutMillis || softIdleTimeoutMillis || 30000)>,
      "min": 0,
      "max": 100,  
      "numTestsPerEvictionRun": <(max - min) / 3>,
      "softIdleTimeoutMillis": 30000,
      "idleTimeoutMillis": 30000,
      "testOnBorrow": true
    }
    

    The generic-pool has a built-in pool evictor, which inspects idle database connections in the pool and destroys them if they are too old.

    The following parameters are provided in the pool configuration:

    • acquireTimeoutMillis: The parameter specifies how much time it is allowed to wait an existing connection is fetched from the pool or a new connection is established.
    • evictionRunIntervalMillis: The parameter specifies how often to run eviction checks. In case of 0 the check is not run.
    • min: Maximum number of database connections to keep in pool at any given time.
    • max: Minimum number of database connections to keep in pool at any given time.
    • numTestsPerEvictionRun: Number of database connections to be checked with one eviction run.
    • softIdleTimeoutMillis: Amount of time database connection may sit idle in the pool before it is eligible for eviction. At least “min” connections should stay in the pool. In case of -1 no connection can get evicted.
    • idleTimeoutMillis: The minimum amount of time that a database connection may stay idle in the pool before it is eligible for eviction due to idle time. This parameter supercedes softIdleTimeoutMillis.
    • testOnBorrow: Should the pool validate the database connections before giving them to the clients?

    Pool configuration can be adjusted by setting the pool option as shown in the following example:

    {
      "cds": {
        "requires": {
          "db": {
            "kind": "hana",
            "pool": {
              "acquireTimeoutMillis": 5000,
              "min": 10,
              "max": 100          
            }
          }
        }
      }
    }
    

    ❗ Warning The parameters are very specific to the current technical setup, such as the application environment and database location. Even though we provide a default pool configuration, we expect that each application provides its own configuration based on its specific needs.

    More to Come

    This documentation is not complete yet, or the APIs are not released for general availability. There’s more to come in this place in upcoming releases.

    Show/Hide Beta Features