Search

Development

Content

Application Configuration

This section describes how to configure applications.

SAP HANA

SAP HANA can be configured when running locally as well as when running productively in the cloud. The datasource is auto-configured based on available service bindings in the VCAP_SERVICES environment variable or locally the default-env.json. This only works if an application profile is used, that does not explicitly configure a datasource using spring.datasource.url. Such an explicit configuration always takes precedence over service bindings from the environment.

SQLite

For local development, SQLite can be configured to run in-memory or in the file-based mode.

File-Based Storage

The database content is stored in a file, sqlite.db as in the following example. Since the schema is initialized using cds deploy command, the initialization mode is set to never.

---
spring:
  profiles: sqlite
  datasource:
    url: "jdbc:sqlite:sqlite.db"
    driver-class-name: org.sqlite.JDBC
    initialization-mode: never
    hikari:
      maximum-pool-size: 1

In-Memory Storage

The database content is stored in-memory only. The schema initialization done by Spring, executes the schema.sql script. Hence, the initialization mode is set to always. If Hikari closes the last connection from the pool, the in-memory database is automatically deleted. To prevent this situation, set max-lifetime to 0.

---
spring:
  profiles: default
  datasource:
    url: "jdbc:sqlite:file::memory:?cache=shared"
    driver-class-name: org.sqlite.JDBC
    initialization-mode: always
    hikari:
      maximum-pool-size: 1
      max-lifetime: 0

H2

H2 can be used as an alternative to SQLite for local development. In Spring, H2 is automatically initialized in-memory when present on the classpath. Refer to official H2 documentation for file-based database configuration.

Database Support in Java

This section describes the different databases, which are supported by CAP Java SDK and any differences between them with respect to CAP features. There’s out of the box support for SAP HANA with CAP currently as well as SQLite. However, it’s important to note that SQLite isn’t an enterprise grade database and is recommended for nonproductive use like local development or CI tests only.

SAP HANA

SAP HANA is supported as the CAP standard database and recommended for productive use with needs for schema evolution and multitenancy.

PostgreSQL

CAP doesn’t support PostgreSQL out of the box. However, CAP Java SDK is tested on PostgreSQL 12 and supports in general most of the features of CAP. Nevertheless, find here a list of unsupported features/differences:

  1. CAP doesn’t support PostgreSQL specific schema generation.
  2. There’s no support for Schema Evolution for PostgreSQL.
  3. Localization is supported just as in SQLite:
    • PostgreSQL doesn’t support the SESSION_CONTEXT method to dynamically determine the locale from a connection.
    • Instead, cds creates two SQL views for the languages German and French by default, to be able to determine the locale at runtime.
  4. The sort order of queries behaves as configured on the database. Locale specific sorting isn’t supported.

SQLite

CAP supports SQLite out of the box. When working with Java, it’s recommended to use SQLite only for development and testing purposes. There’s no production support for SQLite from CAP.

CAP does support most of the major features on SQLite, although there are a few shortcomings that are listed below.

  1. Localization is supported for all locales. However, a system property supported_locales needs to be set with a comma-delimited string literal containing all language codes defined for the data model.
	System.setProperty("supported_locales","en,fr,es,it");
  1. Any views generated for SQLite are read-only by default, inserting records can only happen with a one to one projection.
  2. SQLite has only limited support for concurrent database access. You’re advised to limit the connection pool to 1 as shown above (parameter maximum-pool-size: 1), which effectively serializes all database transactions.
  3. The predicate function contains is supported. However, the search for characters in the word or phrase is case-insensitive in SQLite. In the future, we might provide an option to make the case-sensitivity locale dependent.
  4. SQLite doesn’t support pessimistic locking (SELECT FOR UPDATE) on the query level. Thus, to avoid SQLException at runtime, the generated SQL statements, passed to a database, don’t contain FOR UPDATE clause. Nevertheless, the data consistency can be guaranteed by the fact, that only one process can be making changes to the database at any moment in time.
  5. Streaming of large object data isn’t supported by SQLite. Hence, when reading or writing data of type cds.LargeString and cds.LargeBinary as a stream the framework temporarily materializes the content. Thus, storing large objects on SQLite can impact the performance.
  6. Sorting of character-based columns isn’t lexicographical but if any locale is specified in the context of a query then case insensitive sorting is performed.

H2 Database

H2 is one of the recommended in-memory databases for local development. There’s no production support for H2 from CAP and there are the following support limitations:

  1. H2 requires the SQL statements to create the tables and views to be in order. In the generated SQL, views aren’t ordered. This can result in errors when creating the schema.
  2. Localization is supported for all locales. Unlike SAP HANA, H2 doesn’t support to set the locale for a connection. As in SQLite, a system property supported_locales needs to be set with a comma-separated string literal containing all language codes defined for the data model.
  3. H2 only supports database level collation. Lexicographical sorting on character-based columns isn’t supported.
  4. Views in H2 aren’t updatable but a work-around can be employed manually as described in the official documentation.
  5. Case-insensitive comparison isn’t yet supported.

Spring Boot Integration

This section describes the Spring Boot integration of the CAP Java runtime. Classic Spring isn’t supported. Running your application with Spring Boot framework offers a number of helpful benefits that simplify the development and maintenance of the application to a high extend. Spring not only provides a rich set of libraries and tools for most common challenges in development, you also profit from a huge community, which constantly contributes optimizations, bug fixes and new features. As Spring Boot not only is widely accepted but also most popular application framework, CAP Java SDK comes with a seamless integration of Spring Boot as described in the next sections.

Spring Dependencies

To make your web application ready for Spring Boot, you need to make sure that the following Spring dependencies are referenced in your pom.xml (group ID org.springframework.boot):

  • spring-boot-starter-web
  • spring-boot-starter-jdbc
  • spring-boot-starter-security (optional)

In addition, for activating the Spring integration of CAP Java SDK, the following runtime dependency is required:

<dependency>
	<groupId>com.sap.cds</groupId>
	<artifactId>cds-framework-spring-boot</artifactId>
	<version>${revision}</version>
	<scope>runtime</scope>
/dependency>

It might be more convenient to make use of CDS starter bundle cds-starter-spring-boot-odata, which not only comprises the necessary Spring dependencies, but also configures the OData V4 protocol adapter:

<dependency>
	<groupId>com.sap.cds</groupId>
	<artifactId>cds-starter-spring-boot-odata</artifactId>
	<version>${revision}</version>
</dependency> 

Beside the common Spring features such as dependency injection and a sophisticated test framework, the following features are available in Spring CAP applications in addition:

  • A various number of CAP Java Runtime interfaces are exposed as Spring beans and are available in Spring application context such as model and technical services, the CdsModel or the UserInfo in current request scope.
  • CDS event handler methods within custom Spring beans are automatically registered at startup.
  • Full integration into Spring transaction management (@Transactional is supported).
  • Automatic configuration of XSUAA and Mock user authentication by means of Spring security configuration.
  • Integration of cds-Property section into Spring properties.
  • Integration of cds actuator exposing monitoring information about CDS runtime and security.
  • Integration of DB health check indicator, which also applies to tenant-aware DB connections.

Note that none of the listed features will be available out of the box in case you choose to pack and deploy your web application as plain Java Servlet in a war file.

CDS Maven Plugin

See CDS Maven Plugin documentation for further details.