Skip to content

Commit

Permalink
Add Narayana Transaction Logs QuickStart
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvavrik committed Aug 15, 2023
1 parent dd2d4dc commit 289fcb9
Show file tree
Hide file tree
Showing 19 changed files with 1,431 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ See [CONTRIBUTING](CONTRIBUTING.md) for how to build these examples.
* [Google Cloud Functions](./google-cloud-functions-quickstart): How to create Google Cloud Functions
* [Google Cloud Functions HTTP](./google-cloud-functions-http-quickstart): How to bind our HTTP layer (JAX-RS, Servlet or Reactive Route) to Google Cloud Functions
* [JTA](./jta-quickstart): How use JTA transactions with HTTP/REST
* [Quarkus transaction logs in a database](https://quarkus.io/guides/transaction#jdbcstore) How to configure app for automatic transaction recovery
* [Reactive Messaging with HTTP](./reactive-messaging-http-quickstart): How to consume and produce HTTP messages with Reactive Messaging
* [Reactive Messaging with Web Sockets](./reactive-messaging-websockets-quickstart): How to consume and produce messages via Web Sockets with Reactive Messaging
* [Elasticsearch](./elasticsearch-quickstart): How to use the Elasticsearch REST client and the Java API client
Expand Down
4 changes: 4 additions & 0 deletions narayana-transaction-logs-quickstart/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*
!target/*-runner
!target/*-runner.jar
!target/lib/*
35 changes: 35 additions & 0 deletions narayana-transaction-logs-quickstart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Eclipse
.project
.classpath
.settings/
bin/

# IntelliJ
.idea
*.ipr
*.iml
*.iws

# NetBeans
nb-configuration.xml

# Visual Studio Code
.vscode

# OSX
.DS_Store

# Vim
*.swp
*.swo

# patch
*.orig
*.rej

# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
release.properties
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.6/apache-maven-3.8.6-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar
36 changes: 36 additions & 0 deletions narayana-transaction-logs-quickstart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Quarkus - Narayana - Transaction logs
========================

This QuickStart demonstrates how your Quarkus application can be configured for automatic transaction recovery.

## Test crash recovery

- build application:
- `./mvnw clean package -DskipTests -DskipITs`
- remove previous database volumes, so that we start with a clean sheet:
- `docker-compose down --volumes`
- start database:
- `docker compose up`
- wait until container has started and is ready to accept connections
- start application:
- `java -jar ./target/quarkus-app/quarkus-run.jar`
- send a message to make transaction and crash application:
- `curl -v localhost:8080/transaction-logs/transaction-recovery`
- inspect JDBC Object Store:
- connect to Postgres container:
- execute `docker exec -it $(docker ps | grep 'postgres' | awk '{print $1}') psql -U quarkus_test -W narayana_transaction_logs_db`
- enter password `quarkus_test`
- now you can see that JDBC object store contains transaction waiting for recovery:
- enter `SELECT * FROM quarkus_jbosststxtable;`
- table we were trying to insert when application crashed is empty:
- enter `SELECT * FROM audit_log;`
- start application again:
- `java -jar ./target/quarkus-app/quarkus-run.jar`
- in a moment, you will see that the `audit_log` database table contains 2 inserts:
- enter `SELECT * FROM audit_log;`
```
id | message | datasource
----+---------+-----------------
1 | crash | <default>
2 | crash | object-store-ds
```
16 changes: 16 additions & 0 deletions narayana-transaction-logs-quickstart/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '2.4'

services:
postgres:
container_name: narayana-transaction-logs-database
image: "postgres:latest"
restart: always
volumes:
- ./init-script.sql:/docker-entrypoint-initdb.d/init-script.sql
command: "--max_prepared_transactions=100"
environment:
POSTGRES_USER: "quarkus_test"
POSTGRES_PASSWORD: "quarkus_test"
POSTGRES_DB: "narayana_transaction_logs_db"
ports:
- "5432:5432"
14 changes: 14 additions & 0 deletions narayana-transaction-logs-quickstart/init-script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE quarkus_jbosststxtable (statetype integer not null, hidden integer not null, typename character varying(255) not null, uidstring character varying(255) not null, objectstate bytea);
CREATE TABLE quarkus_jbosststxtable_historical_data AS SELECT * FROM quarkus_jbosststxtable;
CREATE OR REPLACE FUNCTION object_store_historical_data() RETURNS TRIGGER AS '
BEGIN
INSERT INTO quarkus_jbosststxtable_historical_data(statetype, hidden, typename, uidstring, objectstate) VALUES (NEW.statetype, NEW.hidden, NEW.typename, NEW.uidstring, NEW.objectstate);
RETURN NULL;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER object_store_historical_data_trigger AFTER INSERT ON quarkus_jbosststxtable FOR EACH ROW EXECUTE FUNCTION object_store_historical_data();
CREATE TABLE audit_log (id BIGSERIAL PRIMARY KEY, message VARCHAR(10), datasource VARCHAR(40));
CREATE TABLE example (data VARCHAR(6) NOT NULL);
INSERT INTO example VALUES ('first');
INSERT INTO example VALUES ('second');
INSERT INTO example VALUES ('third');
Loading

0 comments on commit 289fcb9

Please sign in to comment.