Skip to content

Commit 3407ddb

Browse files
Describe usage of H2 in Java (#2207)
BLI: /home/issues/2453 * H2 is preferred * Describe gaps and known limitations * Document how to deal with HANA breakouts * How to switch to files for Spring dev Tool scenario (how to reset?) * How to inspect (link to tools) --------- Co-authored-by: Rene Jeglinsky <[email protected]>
1 parent 2d4c4a4 commit 3407ddb

File tree

2 files changed

+108
-24
lines changed

2 files changed

+108
-24
lines changed

guides/databases-h2.md

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl-variants: true
99

1010
For local development and testing, CAP Java supports the [H2](https://www.h2database.com/) database, which can be configured to run in-memory.
1111

12-
[Learn more about features and limitations of using CAP with H2](../java/cqn-services/persistence-services#h2){.learn-more}
12+
[Learn more about features and limitations of using CAP with H2.](../java/cqn-services/persistence-services#h2){.learn-more}
1313

1414
<div class="impl node">
1515

@@ -28,28 +28,7 @@ Not supported for CAP Node.js.
2828

2929
## Setup & Configuration {.java}
3030

31-
### Using the Maven Archetype {.java}
32-
33-
When a new CAP Java project is created with the [Maven Archetype](../java/developing-applications/building#the-maven-archetype) or with `cds init`,
34-
H2 is automatically configured as in-memory database used for development and testing in the `default` profile.
35-
36-
### Manual Configuration {.java}
37-
38-
To use H2, just add a Maven dependency to the H2 JDBC driver:
39-
40-
```xml
41-
<dependency>
42-
<groupId>com.h2database</groupId>
43-
<artifactId>h2</artifactId>
44-
<scope>runtime</scope>
45-
</dependency>
46-
```
47-
48-
Next, configure the build to [create an initial _schema.sql_ file](../java/cqn-services/persistence-services#initial-database-schema-1) for H2 using `cds deploy --to h2 --dry`.
49-
50-
In Spring, H2 is automatically initialized as in-memory database when the driver is present on the classpath.
51-
52-
[Learn more about the configuration of H2 ](../java/cqn-services/persistence-services#h2){.learn-more}
31+
There are various options of how to configure the [H2 database for local development and testing in CAP Java.](../java/developing-applications/testing#setup-configuration)
5332

5433
## Features {.java}
5534

@@ -61,4 +40,4 @@ CAP supports most of the major features on H2:
6140
* [Comparison Operators](../java/working-with-cql/query-api#comparison-operators)
6241
* [Predicate Functions](../java/working-with-cql/query-api#predicate-functions)
6342

64-
[Learn about features and limitations of H2](../java/cqn-services/persistence-services#h2){.learn-more}
43+
[Learn about features and limitations of H2.](../java/cqn-services/persistence-services#h2){.learn-more}

java/developing-applications/testing.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,108 @@ public class CatalogServiceITest {
255255
::: tip
256256
Check out the version in our [CAP Java bookshop sample project](https://github.com/SAP-samples/cloud-cap-samples-java/blob/main/srv/src/test/java/my/bookshop/CatalogServiceITest.java) for additional examples of integration testing.
257257
:::
258+
259+
## Testing with H2
260+
261+
**H2** is the preferred database for CAP Java applications, as it offers a combination of features that make it the best candidate for local development and testing:
262+
263+
* **Concurrent access and locking**
264+
265+
The database supports multiple concurrent connections and implements row-level locking, allowing safe parallel data access without data corruption or race conditions.
266+
267+
* **Open Source and Java native**
268+
269+
As an open-source database written entirely in Java, H2 offers transparency, flexibility, and the benefit of being maintained by an active community. Its Java implementation ensures optimal integration with Java-based applications and platforms.
270+
271+
* **Administrative tools**
272+
273+
H2 includes a built-in web console application, providing a user-friendly interface for database administration, query execution, and data inspection without requiring external tools. CAP Java applications configured with the H2 database expose the administration console under `http://localhost:8080/h2-console` (assuming default port `8080`).
274+
275+
### Setup & Configuration
276+
277+
#### Using the Maven Archetype
278+
279+
When a new CAP Java project is created with the [Maven Archetype](../../java/developing-applications/building#the-maven-archetype) or with `cds init`,
280+
H2 is automatically configured as in-memory database used for development and testing in the `default` profile.
281+
282+
#### Manual Configuration
283+
284+
To use H2, just add a Maven dependency to the H2 JDBC driver:
285+
286+
```xml
287+
<dependency>
288+
<groupId>com.h2database</groupId>
289+
<artifactId>h2</artifactId>
290+
<scope>runtime</scope>
291+
</dependency>
292+
```
293+
294+
Next, configure the build to [create an initial _schema.sql_ file](../../java/cqn-services/persistence-services#initial-database-schema-1) for H2 using `cds deploy --to h2 --dry`.
295+
296+
In Spring, H2 is automatically initialized as in-memory database when the driver is present on the classpath.
297+
298+
[Learn more about the configuration of H2.](../../java/cqn-services/persistence-services#h2){.learn-more}
299+
300+
After performing the above mentioned configuration steps, the `application.yaml` should contain the following lines for `default` profile:
301+
::: code-group
302+
```yml [srv/src/main/resources/application.yaml]
303+
spring:
304+
config.activate.on-profile: default
305+
sql.init.platform: h2
306+
cds:
307+
data-source:
308+
auto-config.enabled: false
309+
```
310+
:::
311+
312+
### H2 Limitations
313+
314+
When developing a CAP Java application, it's important to understand the limits and constraints of the underlying database. Every database has its own performance characteristics, data type restrictions, indexing behavior, and transaction handling rules.
315+
316+
Read more about known limitations in the H2 section of the [Persistence Services guide.](../../java/cqn-services/persistence-services#h2-database)
317+
318+
::: warning Test local MTXS with H2 not possible
319+
Besides the limitations mentioned above, it is not possible to use H2 database when it comes to testing multitenancy and extensibility (MTXS) scenarios on a local environment.
320+
:::
321+
322+
### Hybrid Testing - a way to overcome limitations
323+
324+
Although CAP Java enables running and testing applications with a local H2 database, still there are cases when it is not possible, due to some limitations mentioned previously. In that case, hybrid testing capabilities help you to stay in a local development environment avoiding long turnaround times of cloud deployments. You just selectively connect to services in the cloud.
325+
326+
The section [Hybrid Testing](../../advanced/hybrid-testing#run-cap-java-apps-with-service-bindings) describes the steps on how to configure and consume the remote services, including SAP HANA, in a local environment.
327+
328+
### H2 and Spring Dev Tools Integration
329+
330+
Most CAP Java projects use Spring Boot. To speed up the edit-compile-verify loop, the Spring Boot DevTools dependency is commonly added to the development classpath. DevTools provide automatic restart and LiveReload integration. For more details check the [Spring Dev Tools](https://docs.spring.io/spring-boot/reference/using/devtools.html) reference.
331+
332+
The automatic restart and LiveReload provided by DevTools can cause an application restart that results in loss of state held by an in-memory H2 database. To avoid losing data between restarts during development, prefer the H2 file-based mode so the database is persisted on disk and survives DevTools restarts. The simplest `application.yaml` configuration would look as follows:
333+
334+
::: code-group
335+
```yml [srv/src/main/resources/application.yaml]
336+
spring:
337+
config.activate.on-profile: default
338+
sql.init.platform: h2
339+
url: "jdbc:h2:file:/data/testdb"
340+
cds:
341+
data-source:
342+
auto-config.enabled: false
343+
```
344+
:::
345+
346+
[Learn more about how to configure file-based H2.](https://www.h2database.com/html/features.html#embedded_databases){.learn-more}
347+
348+
### Logging SQL to Console
349+
350+
To view the generated SQL statements, which will be run on the H2 database, it is possible to switch to `DEBUG` log output by adding the following log-levels:
351+
352+
::: code-group
353+
```yml [srv/src/main/resources/application.yaml]
354+
logging:
355+
level:
356+
com.sap.cds.persistence.sql: DEBUG
357+
```
358+
:::
359+
360+
This is beneficial, when you need to track runtime or a Java Application behavior.
361+
362+
[Learn more about Predefined Loggers.](../../java/operating-applications/observability#predefined-loggers){.learn-more}

0 commit comments

Comments
 (0)