img
. .
Infrastructure for Implementing the JTA Sample
We are using the same table as the previous samples in this chapter. However, the embedded H2
database doesn't fully support XA (at least at the time of writing), so in this example, we will use MySQL
as the backend database.
We also want to show how to implement global transactions with JTA in a stand-alone application
or web container environment. So, in this example, we will use Atomikos
(www.atomikos.com/Main/TransactionsEssentials), which is a widely used open source JTA transaction
manager for use in a non-JEE environment.
To show how global transactions work, we need at least two different backend resources. To make
things simple, we will use one MySQL database but two JPA entity managers to simulate the use case.
The effect is the same because you have multiple JPA persistence units to distinct backend databases.
The version we are using in developing this sample is the MySQL 5.1.58 Community Server edition.
In the MySQL database, we will create two schemas, as shown in Table 13-5.
Table 13-5. MySQL Database Schemas
Schema Name
Connection Information
Data Population Scripts
User: prospring3_ch13a
prospring3_ch13a
schema.sql
Password: prospring3_ch13a
test-data.sql
User: prospring3_ch13b
prospring3_ch13b
schema.sql
test-data.sql
Password: prospring3_ch13b
I am using phpMyAdmin (www.phpmyadmin.net) to set up the schema and scripts. However, you can
use whatever tools you feel familiar with to set up the schemas and users.
Then, we will need to add the required dependencies on MySQL and Atomikos to the project. Table
13-6 shows the dependencies required.
Table 13-6. Maven Dependencies for MySQL and Atomikos
Group ID
Artifact ID
Version
Description
5.1.18
The Java library for MySQL 5
mysql
mysql-connector-java
3.7.0
The JTA transaction library for Atomikos
com.atomikos
transactions-jdbc
3.7.0
The library for Atomikos integration with
com.atomikos
transactions-hibernate3
Hibernate 3
After the setup has completed, we can proceed to the Spring configuration and implementation.
Implementing Global Transactions with JTA
First let's take a look at Spring's configuration. Listing 13-24 shows the file content (tx-jta-app-
context.xml).
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home