2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2004
5
* Sleepycat Software. All rights reserved.
7
* $Id: TestXAServlet.java,v 1.4 2004/01/28 03:36:34 bostic Exp $
11
* Simple test of XA, using WebLogic.
14
package com.sleepycat.test;
16
import com.sleepycat.db.*;
17
import com.sleepycat.db.xa.*;
19
import java.io.FileNotFoundException;
20
import java.io.IOException;
21
import java.io.PrintWriter;
22
import java.util.Hashtable;
23
import javax.servlet.*;
24
import javax.servlet.http.*;
25
import javax.transaction.*;
26
import javax.transaction.xa.*;
27
import javax.naming.Context;
28
import javax.naming.InitialContext;
29
import javax.naming.NamingException;
30
import weblogic.transaction.TxHelper;
31
import weblogic.transaction.TransactionManager;
33
public class TestXAServlet extends HttpServlet
35
public static final String ENV_HOME = "TESTXADIR";
36
public static final String DEFAULT_URL = "t3://localhost:7001";
37
public static String filesep = System.getProperty("file.separator");
39
private static TransactionManager tm;
40
private static DbXAResource xaresource;
41
private static boolean initialized = false;
44
* Utility to remove files recursively.
46
public static void removeRecursive(File f)
48
if (f.isDirectory()) {
49
String[] sub = f.list();
50
for (int i=0; i<sub.length; i++)
51
removeRecursive(new File(f.getName() + filesep + sub[i]));
57
* Typically done only once, unless shutdown is invoked. This
58
* sets up directories, and removes any work files from previous
59
* runs. Also establishes a transaction manager that we'll use
60
* for various transactions. Each call opens/creates a new DB
61
* environment in our work directory.
63
public static synchronized void startup()
69
File dir = new File(ENV_HOME);
73
System.out.println("Getting context");
74
InitialContext ic = getInitialContext(DEFAULT_URL);
75
System.out.println("Creating XAResource");
76
xaresource = new DbXAResource(ENV_HOME, 77, 0);
77
System.out.println("Registering with transaction manager");
78
tm = TxHelper.getTransactionManager();
79
tm.registerStaticResource("DbXA", xaresource);
83
System.err.println("Exception: " + e);
90
* Closes the XA resource manager.
92
public static synchronized void shutdown(PrintWriter out)
98
out.println("Closing the resource.");
100
out.println("Shutdown complete.");
106
* Should be called once per chunk of major activity.
108
public void initialize()
113
private static int count = 1;
114
private static boolean debugInited = false;
115
private Xid bogusXid;
117
public static synchronized int incrCount()
122
public void debugSetup(PrintWriter out)
123
throws ServletException, IOException
128
catch (Exception e) {
129
out.println("got exception during load: " + e);
130
System.out.println("got exception during load: " + e);
132
out.println("The servlet has been restarted, and Berkeley DB is loaded");
133
out.println("<p>If you're debugging, you should now start the debugger and set breakpoints.");
136
public void doXATransaction(PrintWriter out, String key, String value,
138
throws ServletException, IOException
141
int counter = incrCount();
142
if (key == null || key.equals(""))
143
key = "key" + counter;
144
if (value == null || value.equals(""))
145
value = "value" + counter;
147
out.println("Adding (\"" + key + "\", \"" + value + "\")");
149
System.out.println("XA transaction begin");
151
System.out.println("getting XA transaction");
152
DbXAResource.DbAttach attach = DbXAResource.xa_attach(null, null);
153
DbTxn txn = attach.get_txn();
154
DbEnv env = attach.get_env();
155
Db db = new Db(env, 0);
156
db.open(txn, "my.db", null, Db.DB_BTREE, Db.DB_CREATE, 0644);
157
System.out.println("DB put " + key);
159
new Dbt(key.getBytes()),
160
new Dbt(value.getBytes()),
163
if (operation.equals("rollback")) {
164
out.println("<p>ROLLBACK");
165
System.out.println("XA transaction rollback");
167
System.out.println("XA rollback returned");
169
// The old db is no good after the rollback
170
// since the open was part of the transaction.
171
// Get another db for the cursor dump
174
db.open(null, "my.db", null, Db.DB_BTREE, Db.DB_CREATE, 0644);
177
out.println("<p>COMMITTED");
178
System.out.println("XA transaction commit");
182
// Show the current state of the database.
183
Dbc dbc = db.cursor(null, 0);
184
Dbt gotkey = new Dbt();
185
Dbt gotdata = new Dbt();
187
out.println("<p>Current database values:");
188
while (dbc.get(gotkey, gotdata, Db.DB_NEXT) == 0) {
189
out.println("<br> " + getDbtString(gotkey) + " : "
190
+ getDbtString(gotdata));
195
catch (DbException dbe) {
196
System.err.println("Db Exception: " + dbe);
197
out.println(" *** Exception received: " + dbe);
198
dbe.printStackTrace();
200
catch (FileNotFoundException fnfe) {
201
System.err.println("FileNotFoundException: " + fnfe);
202
out.println(" *** Exception received: " + fnfe);
203
fnfe.printStackTrace();
205
// Includes SystemException, NotSupportedException, RollbackException
206
catch (Exception e) {
207
System.err.println("Exception: " + e);
208
out.println(" *** Exception received: " + e);
213
private static Xid getBogusXid()
216
return new DbXid(1, "BOGUS_gtrid".getBytes(),
217
"BOGUS_bqual".getBytes());
220
private static String getDbtString(Dbt dbt)
222
return new String(dbt.get_data(), 0, dbt.get_size());
226
* doGet is called as a result of invoking the servlet.
228
public void doGet(HttpServletRequest req, HttpServletResponse resp)
229
throws ServletException, IOException
232
resp.setContentType("text/html");
233
PrintWriter out = resp.getWriter();
235
String key = req.getParameter("key");
236
String value = req.getParameter("value");
237
String operation = req.getParameter("operation");
239
out.println("<HTML>");
240
out.println("<HEAD>");
241
out.println("<TITLE>Berkeley DB with XA</TITLE>");
242
out.println("</HEAD><BODY>");
243
out.println("<a href=\"TestXAServlet" +
244
"\">Database put and commit</a><br>");
245
out.println("<a href=\"TestXAServlet?operation=rollback" +
246
"\">Database put and rollback</a><br>");
247
out.println("<a href=\"TestXAServlet?operation=close" +
248
"\">Close the XA resource manager</a><br>");
249
out.println("<a href=\"TestXAServlet?operation=forget" +
250
"\">Forget an operation (bypasses TM)</a><br>");
251
out.println("<a href=\"TestXAServlet?operation=prepare" +
252
"\">Prepare an operation (bypasses TM)</a><br>");
256
// Don't initialize XA yet, give the user
257
// a chance to attach a debugger if necessary.
263
if (operation == null)
264
operation = "commit";
266
if (operation.equals("close")) {
269
else if (operation.equals("forget")) {
270
// A bogus test, we just make sure the API is callable.
271
out.println("<p>FORGET");
272
System.out.println("XA forget bogus XID (bypass TM)");
273
xaresource.forget(getBogusXid());
275
else if (operation.equals("prepare")) {
276
// A bogus test, we just make sure the API is callable.
277
out.println("<p>PREPARE");
278
System.out.println("XA prepare bogus XID (bypass TM)");
279
xaresource.prepare(getBogusXid());
282
// commit, rollback, prepare, forget
283
doXATransaction(out, key, value, operation);
286
out.println("</BODY></HTML>");
288
System.out.println("Finished.");
290
// Includes SystemException, NotSupportedException, RollbackException
291
catch (Exception e) {
292
System.err.println("Exception: " + e);
300
* From weblogic's sample code:
301
* samples/examples/jta/jmsjdbc/Client.java
303
private static InitialContext getInitialContext(String url)
304
throws NamingException
306
Hashtable env = new Hashtable();
307
env.put(Context.INITIAL_CONTEXT_FACTORY,
308
"weblogic.jndi.WLInitialContextFactory");
309
env.put(Context.PROVIDER_URL, url);
310
return new InitialContext(env);