How to use jdbc and transactions in Tomcat with jotm
Target Audience
This Howto is intended to servlet / jsp developpers Using Tomcat Who Want To Take Advantage Of Distributed Transactions When Using JDBC Code In Their Servlets
This Howto Was Written Based on the jndi datasource how-to provided by tomcat.
IT HAS BEEN WRITTEN for:
Tomcat 4.1.18JOTM 1.4
IT Has Been SuccessFully Reported to Work ON
Mysql
Version 4.0.12, using Innodbwith MySQL-Connector-Java Version 2.0.14PostgreSQL
Version 7.3.1with pg73jdbc2.jar
INTRODUCTION
JOTM (Java Open Transaction Manager) IS A Transaction Manager Written in Java and Implementating JTA (Java Transaction API). It is an open source project released under a bsd-style license.
Tomcat is The servlet Container That IS Used in The Official Reference Implementation for The Java Servlet and JavaServer Pages (JSP) Technologies. Tomcat IS Released Under The Apache Software License
MySQL Configuration
Ensure That You Follow these Instructions as variations can Cause Problems.
Create a New Test User, A New Database and A Single Test Table. Your MySQL User Must Have a Password Assigned. The Driver Will Fail If You Try To Connect with an an an an an an an escher password.
MySQL> Grant All privileges on *. * to mojo @ localhost
-> Identified by 'Jojo' with grant option;
Mysql> Create Database Javates
Mysql> Use javatest;
Mysql> Create Table TestData
-> ID INT NOT NULL AUTO_INCREMENT Primary Key,
-> foo int) TYPE = INNODB;
MySQL>
NOTE: The Above User Should Be Removed Once Testing IS Complete!
Next INSERT Some Test Data Into The TestData Table.
Mysql> Insert Into TestData Values (NULL, 1);
Query Ok, 1 Row Affected (0.00 sec)
Mysql> Select * from testdata; -- -----
| ID | foo |
-- -----
| 1 | 1 |
-- -----
1 row in set (0.00 sec)
MySQL>
PostgreSQL Configuration IS Similar.
Installing Required Jars
In Order for A Web Application To Use Jotm, The Webapp And Tomcat Need To Have Access To The Jotm Jars, AS Well As The Jars IT Requires
Here is a list of the required jars. They area all include the jotm 1.4 binary distribution, available at http://www.objectweb.org/jotm
In the lib / Directory
Jotm.jarjotm_jrmp_stubs.jarjonas_timer.jarcarol.jarjta-Spec1_0_1.jarjts1_0.jarobjectWeb-DataSource.jarxapool.jar
All these Jar Files Need to Be Placed On $ Tomcat_Home / Common / Lib / So That Both Tomcat and your Web Application Will See THEM.
You Also Need To Copy The Jar of Your JDBC Driver To $ Tomcat_Home / Common / LIB /.
Configuring Jotm
You'll Need to create a file named carol.properties with the folowing property:
# l l s s l v v (it's a "fake" RMI)
Carol.Protocols = LMI
# do not use carol jndi wrapper
Carol.start.jndi = false
# do not start a name server
Carol.start.ns = false
This File Needs to Be Placed On $ Tomcat_Home / Common / Classes / SO That Tomcat and Jotm Will See IT.
Configuring Tomcat
Server.xml configuration
Now that you've installed JOTM in Tomcat, you need to configure Tomcat so that it can access JDBC and transaction resources. This is done in the dbtest.xml file which will be used by Tomcat to initialize the resources used by your web application.
Reloadable = "true" crossText = "true">
-> TYPE = "javax.sql.datasource" /> parameter>
To set it for your database ->
-> Resourceparams>
Use jotm -> TYPE = "javax.transaction.usertransaction" /> parameter> parameter> resourceparams> Context> Web.xml configuration Now you have to configure the web.xml file of your web copLication Xml Version = "1.0" encoding = "ISO-8859-1"?>
"- // Sun Microsystems, Inc.//dtd Web Application 2.3 // En" "http://java.sun.com/dtd/web-app_2_3.dtd"> DB Connection description> JDBC / MYDB resource-env-ref-name> Javax.sql.datasource resource-env-ref-type> resource-env-ref> web-app> Test Code Now Create a Simple Test.jsp for Use. hEAD>
<%
String completion = request.getParameter ("completion");
Foo.dbtest test = new foo.dbtest ();
TEST.INIT (Completion);
%>
Transaction completion is: <% = completion%> strong>
Int Stored in JDBC: <% = Test.getfoo ()%> strong>