Using a Spring configured full-blown transaction manager across modules in Z2¶
As does Sample-jta-plain, so does this sample demonstrate how to use Z2 with the Atomikos transaction manager. In this case however, we choose to use the Spring framework to configure the transaction manager and enable re-use using Z2's support for modular Spring applications (see also How to Spring).
The Wiki page How_to_TransactionManager explains the general principles behind transaction handling in Z2.
Z2 has the following Java Version requirements
|Z2 Version||Min. Java version required||Max Java version supported||Max language level|
|2.1 - 2.3.1||Java 6||Java 7||Java 7|
|2.4 - 2.5||Java 8||Java 8||Java 8|
|2.6||Java 9||Java 11||Java 10|
|2.7||Java 9||Java 11||Java 11|
|master||Java 9||?||Java 11|
Note: Most samples suggest to use the master branch. You may choose another version branch (please check the respective repository).
Make sure you have a corresponding Java Development Kit (JDK) or Java Runtime Environment (JRE) installed. If in doubt, go to Download Java SE.
Note: Running v2.1-v2.3.1 on Java 8 is supported by specifying
(or 6, if that is your desired compilation language level) in
<home>/run/bin/runtime.properties. By this the Java compiler version detection does not fall back to a lower level.
You need to run Java DB as network server on localhost. This is explained next.
The application will create a database "z2-samples"
Running a Java DB Network Server¶
Previously to Java 9, the Java SE Development Kit (JDK) by Oracle provided the Java DB - essentially the same as the Apache Derby DB. That is not the case anymore. However, we use that Database implementation in our samples. In order to run those samples that illustrate use of a relational database, please follow the instructions below to install and run Apache Derby. Could hardly be simpler.
Step 1: Download and Install¶
Note: You do not need to unpack Apache Derby into some global folder on your system. Instead you may want to use some local folder under your user's home folder. There is no problem installing and runnning different instances and configurations at any time.
Step 2: Run¶
Let's assume you installed (well - unpacked) into a folder $DERBY_INSTALL. Also, let's assume some Java Runtime Environment is installed and ready.
Simply run the following on Linux or Mac OS:
cd $DERBY_INSTALL java -jar lib/derbyrun.jar server start
On Windows run
cd %DERBY_INSTALL java -jar lib\derbyrun.jar server start
That's it. Apache Derby will be waiting for connections on port 1527.
Running the sample¶
mkdir install cd install
On Mac-OS or Linux run:
wget http://download.z2-environment.net/z2/z2-base-v2.7.zip unzip z2-base-v2.7.zip
On Windows download the archive and unpack using the Windows explorer. Make sure to unpack into the installation folder previously created.
This will create a folder z2-base.core that contains the complete version 2.7 z2-base installation.
Check out the sample
git clone -b v2.7 http://git.z2-environment.net/z2-samples.jta-spring
On Mac OS or Linux run:
cd z2-base.core/bin ./gui.sh
On Windows run:
cd z2-base.core\bin gui.bat
(In order to check that z2 is up, when you see "Completed home process initialization", try http://localhost:8080/adm with user "z*" and password "z".)
When everything is up, go to http://localhost:8080/jta-spring. You see something like this:
And indeed, it's the same database we use elsewhere.
Now to the point...¶
This sample illustrates how to use a third-party transaction manager, the Atomikos implementation in this case, in a modular application as indicated in How_to_TransactionManager. This in conjunction with Spring resulting in a full-blown, highly adaptable and completely modular environment, scalable in deployment and development.
Unlike Sample-jta-plain, Atomikos configuration and set up of transaction manager and user transaction is done by Spring. This may be perceived a more consistent approach by Spring users.
Instead of invoking the JTA Transaction Manager interface directly, transaction demarcation is done using Spring's \@Transactional annotation (in the Controller). Note that Spring transaction de-marcation cannot be used with direct transaction manager usage at the same time (as Spring keeps additional context).