Skip to content

vmj/jta-jpa-se-example

Repository files navigation

Build Status

Example Java SE + JTA + JPA application

This repository contains an application that demonstrates transactions across two databases. The application is using BTM as the transaction manager (JTA) and Hibernate as the ORM (JPA). They are both readily usable in a Java SE environment. For example, small apps or 12 factor apps.

Why and What?

Think what you will about the enterprise Java as a whole, some of their technologies are really neat. Also, since they are more and more usable without any heavy frameworks or boilerplate or configuration, they are a real contender in Java SE space.

JTA allows you to cleanly separate the transaction management concern from the connection management concern, even if you’re only planning to use a single database in your application. BTM as a transaction manager also optimizes away the 2PC (2 Phase Commit) when it sees that there’s only one data source. And still, 2PC is right behind the corner, if you need it, for example to do transactions with a database and a messaging system.

JPA is a standard persistence API, and the Hibernate ORM is just a nugget of gold :)

How it works

For simplicity, this project contains just one module: three classes and three resource files.

Make sure you’ve got Java 8 (JDK) or newer installed. To build and run this example, use the provided Gradle wrapper as shown here. For example, to run the app on MacOS X or Linux or some other unixish thing, run:

$ ./gradlew run

or, if you’re on Windows, use this instead:

$ ./gradlew.bat run

When you run the application, it will

  1. insert one user object into a database called users1,

  2. then move all the users from users1 to another database called users2.

  3. and finally list the users in users2.

The first step is one transaction, and the second step is another. I.e. if the moving fails in the middle, the whole second step (both databases) is rolled back.

Note that the example application will create the databases if they do not exist. Derby is used as the database engine, so the databases will reside in the current directory, unless you change that by providing a Java system property derby.system.home. Also, the schemas will be created if they do not exist. You can remove all the generated files with reallyclean and clean Gradle tasks.

Have fun!

About

Example Java SE + JTA + JPA application

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages