Classy Tools

People and Pets

People and Pets presents an example of using 2 different persistence units (PUs) in a single database. Each PU has it's own context and ensures there is no unintended interactions. Two versions of this application are presented to make the following points about Jpalite persistence:

  1. Persistence units are defined in the persistence.xml
  2. Handlers can be hooked into database creation and upgrade events

The Project

Jpalite people-and-pets-example Java project contains version 1. of the application in package au.com.cybersearch2.pp. The database created by running this application is also used by version 2 to demonstrate it being updated automatically the first time version 2 starts.

Persistence Unit Configuration

The persistence units, named "pets" and "people" are configured in file "jpalite.json" located in the "src/main/resources/v1" project folder. These units coexist in the same database and this is evident in both having the same "databaseName" setting

"databaseName = people-and-pets.db"

There is also a "databaseVersion = 1" setting in anticipation that the database schema will be updated sometime in the future.

Run Version 1

Run PeopleAndPetsMain as a Java application, and you will see console output starting with info and warning level log messages from database set up operations and then a play back of an application activity recorder. The application performs various actions involving records of both persistence units to simulate typical record create, read and delete operations.

Jpalite Container

Jpalite is packaged into a single JpaContainer object which provides the application interface for all persistence operations. At startup, JpaContainer locates the jpalile.json configuration file and sets up the persistence units that are defined in the file. A first time set up of a persistence unit involves creation of a database and a table for each entity class. There is also the option of populating the database and creating additional features using an SQL language file specified in the jpalite.json configuration. Each persistence unit requires 2 parameters to specify the database

  • A database type - H2 or SQLite
  • A connection type = file, memory or pooled

The model for executing a persistence task is taken from Java runtime java.lang.Process class. Normally each call is assigned a ThreadPoolExecutor, but there is also the option to proceed with the task on the caller's thread (option "synchronousMode"). Here is a code snippet showing a call to the JpaContainer

Execute Jpalite task