This sample app is aimed at demonstrating how to model a double entry financial ledger application with QLDB.
This sample app is aimed at demonstrating how to model a double entry financial ledger application with QLDB.
Install Gradle. See Instructions.
Clone the sample app using the following command
git clone https://github.com/aws-samples/amazon-qldb-double-entry-sample-java
Make sure you have valid AWS credentials setup. The AWS SDK used in the app will load the credentials automatically as long as you have setup the credentials using one of the methods mentioned in setup-credentials guide
Build the project
./gradlew build
Run the gradle tasks
./gradlew run -Dtask=<NameOfTheTask>
There are three tasks in the sample which help you understand how to use Amazon QLDB:
To run this task, do
./gradlew run -Dtask=SetupLedger
We perform three actions as part of this task
We first call the CreateLedger
API of QLDB, which starts the ledger creation process. At this point of time, the status of the ledger will be CREATING
as QLDB takes short amount of time to create the ledger.
In order to confirm if the ledger creation is complete, we poll QLDB via DescribeLedger
API, at a fixed interval and check if the status has changed to ACTIVE
. Once the status changes to ACTIVE
, we are sure that the ledger creation has succeeded.
Finally, we use the ListLedgers
API to list all the ledgers that we have created up until now.
To run this task, do:
./gradlew run -Dtask=SetupTables
We perform three actions as part of this task:
For this sample app, we have three tables:
Note: In Accounting, double entry is a concept where for each transaction(in our case transfer of money), we record two entries in the journal. One debit entry to show deduction from Sender account and another credit entry to show increment in the receiver account.
As Amazon QLDB uses documents to store the data, we can create a single document which club together the debit and credit entries. This gives us the ability to view the transaction on the whole with just one entry.
Furthermore, using this model gives a flexibility to extend the document to accommodate compound entries, which is used to record a transfer where multiple credit accounts and/or multiple debit accounts are involved
The attributes of the tables are
Businesses | Accounts | Transactions |
---|---|---|
Name | Account Id | Sender Account Entry* |
Registered Address | Business Id | Receive Account Entry* |
Business Id | Account Type | |
Date of registration | Balances |
*The sender account and receiver account entries will each have following attributes
We also create following indexes:
Read more about QLDB indexes in our developer guide
To run this task, do
./gradlew run -Dtask=TransferMoney --args <Name of the Argument Eg: singleTransfer>
This task also takes additional argument to indicate the kind of transfer to be done. There are two types supported:
To transfer the amount we perform the following steps as a part of a single Amazon QLDB transaction
When there are multiple transfers happening in the system, if there is an OCC while committing the transaction, then the QLDB Driver (or specifically, QLDB session) takes care of retrying the
entire transaction. The failed transaction will start again from, reading the balances, doing the business validations again with the new values, and then updating the balances to the correct values. If this attempt fails as well, the driver will retry all over again.
The driver will keep retrying till either the transaction succeeds or it hits the maximum retry attempt limit (specified when instantiating the PooledQLDBDriver).
Tasks: This module contains all the classes that can be executed from the command line. These also serve as entry points into the application. You can run any task as follows:
./gradlew run -Dtask=<NameOfTheTask>
actions: All the classes that interact with QLDB.
models: Contains the classes for models for the tables in QLDB.
models/qldb: Contains classes to map QLDB models.
helper: Contains all helper classes.
Release 1.0
This library is licensed under the MIT-0 License.