JOTM can be used with any database (with a JDBC driver) to provide
distributed transactional access to databases.
The JDBC example is a very simple example showing how to use JTA
transactions with XAPool to provide transactional access to a database
(configuration files for PostgreSQL and MySQL are included).
All Ant commands are to be executed from the examples/jdbc/ directory of a JOTM distribution (examples won't work from JOTM source directory).
Before starting the example, the database needs to be properly configured. The JDBC example can work with any database providing a JDBC driver. It uses XAPool to take care of the transactional behaviors of JDBC objects. The setup is explained for MySQL. For other databases, it should be straightforward to configure them properly.
The example expects:
| ID | FOO |
| 1 | 1 |
For example on MySQL:
mysql> GRANT ALL PRIVILEGES ON *.* TO mojo
-> IDENTIFIED BY 'jojo' WITH GRANT OPTION;
mysql> create database javatest;
mysql> use javatest;
mysql> create table testdata (
-> id int not null auto_increment primary key,
-> foo int)type=InnoDB;
mysql> insert into testdata values(null, 1);
Do not forget to set testdata type to InnoDB to enable
transaction support.
$ ant compileto compile the example
To run the example, first check that only RMI protocol will be activated (in the ../../config/carol.properties, carol.protocols should be set to jrmp); then type in $JOTM_HOME/lib/ directory
$ rmiregistry -J-classpath -Jjotm.jar:jotm_jrmp_stubs.jar \
-J-Djava.security.policy=../config/java.policy &
to start a RMI registry on default port (i.e. 1099).
Set the classpath
$ export CLASSPATH=../../lib/jotm.jar:../../lib/jotm_jrmp_stubs.jar\
:../../lib/xapool.jar:../../config:.:$JDBC_JARS
where JDBC_JARS is the location of the JDBC driver jar
file(s) you want to use
Start the example
$ java JdbcExample postgresql commit 2to set foo value to 2 and commit the transaction on PostgreSQL
$ java JdbcExample mysql rollback 0to set foo value to 0 but rollback the transaction on MySQL
$ java JdbcExample [database] [completion] [number]where
For example, command line
$ java JdbcExample postgresql commit 2will ouptut something like
start server
postgresql configuration:
-- listing properties --
login=mojo
url=jdbc:postgresql://localhost/javatest
password=jojo
driver=org.postgresql.Driver
------------------------
create initial context
lookup UserTransaction at : UserTransaction
get a connection
before transaction, table is:
id foo
1 0
begin a transaction
update the table
*commit* the transaction
after transaction, table is:
id foo
1 2
close connection
stop server
JDBC example is ok.
As stated, the transaction has been committed and foo value
has been set to 2 in the database.
Another command line like
$ java JdbcExample mysql rollback 3will output something like
start server
mysql configuration:
-- listing properties --
login=mojo
url=jdbc:mysql://localhost/javatest
password=jojo
driver=org.gjt.mm.mysql.Driver
------------------------
create initial context
lookup UserTransaction at : UserTransaction
get a connection
before transaction, table is:
id foo
1 1
begin a transaction
update the table
*rollback* the transaction
after transaction, table is:
id foo
1 0
close connection
stop server
JDBC example is ok.
Here, the value of foo has not been changed in the database
because the transaction has been rolled back.