generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 139
Describe usage of H2 in Java #2207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eugene-andreev
wants to merge
20
commits into
main
Choose a base branch
from
h2-in-java
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
fa93774
H2 in Java
eugene-andreev 57e6207
wip
eugene-andreev 4efb33c
wip
eugene-andreev 95a58d4
Inspecting H2 in-memory database on the fly
eugene-andreev 3ccf4c3
typos
eugene-andreev af64834
Merge branch 'main' into h2-in-java
eugene-andreev 1e1ed26
Merge branch 'main' into h2-in-java
eugene-andreev 61246b1
dev tools
eugene-andreev 0a0a109
Merge remote-tracking branch 'origin/h2-in-java' into h2-in-java
eugene-andreev 442b09e
Merge branch 'main' into h2-in-java
eugene-andreev dc001a4
improve
eugene-andreev 0be9942
Merge remote-tracking branch 'origin/h2-in-java' into h2-in-java
eugene-andreev f9b9d9d
Merge remote-tracking branch 'origin/main' into h2-in-java
eugene-andreev 45fd77f
address comments 1
eugene-andreev 35114d6
Merge remote-tracking branch 'origin/main' into h2-in-java
eugene-andreev 05ec1ba
yamls
eugene-andreev f680ada
address comments 2
eugene-andreev 6484a5d
fix dead link
eugene-andreev d26bd84
fix punctuation
renejeglinsky b1d69cc
edit
renejeglinsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,3 +255,108 @@ public class CatalogServiceITest { | |
| ::: tip | ||
| 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. | ||
| ::: | ||
|
|
||
| ## Testing with H2 | ||
|
|
||
| **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: | ||
|
|
||
| * **Concurrent access and locking** | ||
|
|
||
| The database supports multiple concurrent connections and implements row-level locking, allowing safe parallel data access without data corruption or race conditions. | ||
|
|
||
| * **Open Source and Java native** | ||
|
|
||
| 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. | ||
|
|
||
| * **Administrative tools** | ||
|
|
||
| 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` (if the port differs from the default `8080`, it should be changed accordingly). | ||
|
|
||
| ### Setup & Configuration | ||
|
|
||
| #### Using the Maven Archetype | ||
|
|
||
| When a new CAP Java project is created with the [Maven Archetype](../../java/developing-applications/building#the-maven-archetype) or with `cds init`, | ||
| H2 is automatically configured as in-memory database used for development and testing in the `default` profile. | ||
|
|
||
| #### Manual Configuration | ||
|
|
||
| To use H2, just add a Maven dependency to the H2 JDBC driver: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>com.h2database</groupId> | ||
| <artifactId>h2</artifactId> | ||
| <scope>runtime</scope> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| 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`. | ||
|
|
||
| In Spring, H2 is automatically initialized as in-memory database when the driver is present on the classpath. | ||
|
|
||
| [Learn more about the configuration of H2.](../../java/cqn-services/persistence-services#h2){.learn-more} | ||
|
|
||
| After performing the above mentioned configuration steps, the `application.yaml` should contain the following lines for `default` profile: | ||
| ::: code-group | ||
| ```yml [srv/src/main/resources/application.yaml] | ||
| spring: | ||
| config.activate.on-profile: default | ||
| sql.init.platform: h2 | ||
| cds: | ||
| data-source: | ||
| auto-config.enabled: false | ||
| ``` | ||
| ::: | ||
|
|
||
| ### H2 Limitations | ||
|
|
||
| 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. | ||
|
|
||
| Read more about known limitations in the H2 section of the [Persistence Services guide.](../../java/cqn-services/persistence-services#h2-database) | ||
|
|
||
| ::: warning Constraints with local MTXS | ||
| In addition to the limitations of the H2 database mentioned above, there are also constraints when it comes to testing multitenancy and extensibility (MTXS) scenarios on a local environment. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which constraint? |
||
| ::: | ||
|
|
||
| ### Hybrid Testing - a way to overcome limitations | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| ### H2 and Spring Dev Tools integration | ||
|
|
||
| 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. | ||
|
|
||
| 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: | ||
|
|
||
| ::: code-group | ||
| ```yml [srv/src/main/resources/application.yaml] | ||
| spring: | ||
| config.activate.on-profile: default | ||
| sql.init.platform: h2 | ||
| url: "jdbc:h2:file:/data/testdb" | ||
| cds: | ||
| data-source: | ||
| auto-config.enabled: false | ||
| ``` | ||
| ::: | ||
|
|
||
| [Learn more about how to configure file-based H2.](https://www.h2database.com/html/features.html#embedded_databases){.learn-more} | ||
|
|
||
| ### Logging SQL to console | ||
|
|
||
| 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: | ||
|
|
||
| ::: code-group | ||
| ```yml [srv/src/main/resources/application.yaml] | ||
| logging: | ||
| level: | ||
| com.sap.cds.persistence.sql: DEBUG | ||
| ``` | ||
| ::: | ||
|
|
||
| This is beneficial, when you need to track runtime or a Java Application behavior. | ||
|
|
||
| [Learn more about Predefined Loggers.](../../java/operating-applications/observability#predefined-loggers){.learn-more} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.