1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1997, 1998, 1999, 2000
* Sleepycat Software. All rights reserved.
*
* $Id: TestXAServlet.java,v 1.1.1.1 2003/11/20 22:14:06 toshok Exp $
*/
/*
* Simple test of XA, using WebLogic.
*/
package com.sleepycat.test;
import com.sleepycat.db.*;
import com.sleepycat.db.xa.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.transaction.*;
import javax.transaction.xa.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import weblogic.transaction.TxHelper;
import weblogic.transaction.TransactionManager;
public class TestXAServlet extends HttpServlet
{
public static final String ENV_HOME = "TESTXADIR";
public static final String DEFAULT_URL = "t3://localhost:7001";
public static String filesep = System.getProperty("file.separator");
private static TransactionManager tm;
private static DbXAResource xaresource;
private static boolean initialized = false;
/**
* Utility to remove files recursively.
*/
public static void removeRecursive(File f)
{
if (f.isDirectory()) {
String[] sub = f.list();
for (int i=0; i<sub.length; i++)
removeRecursive(new File(f.getName() + filesep + sub[i]));
}
f.delete();
}
/**
* Typically done only once, unless shutdown is invoked. This
* sets up directories, and removes any work files from previous
* runs. Also establishes a transaction manager that we'll use
* for various transactions. Each call opens/creates a new DB
* environment in our work directory.
*/
public static synchronized void startup()
{
if (initialized)
return;
try {
File dir = new File(ENV_HOME);
removeRecursive(dir);
dir.mkdirs();
System.out.println("Getting context");
InitialContext ic = getInitialContext(DEFAULT_URL);
System.out.println("Creating XAResource");
xaresource = new DbXAResource(ENV_HOME, 77, 0);
System.out.println("Registering with transaction manager");
tm = TxHelper.getTransactionManager();
tm.registerStaticResource("DbXA", xaresource);
initialized = true;
}
catch (Exception e) {
System.err.println("Exception: " + e);
e.printStackTrace();
}
initialized = true;
}
/**
* Closes the XA resource manager.
*/
public static synchronized void shutdown(PrintWriter out)
throws XAException
{
if (!initialized)
return;
out.println("Closing the resource.");
xaresource.close(0);
out.println("Shutdown complete.");
initialized = false;
}
/**
* Should be called once per chunk of major activity.
*/
public void initialize()
{
startup();
}
private static int count = 1;
private static boolean debugInited = false;
private Xid bogusXid;
public static synchronized int incrCount()
{
return count++;
}
public void debugSetup(PrintWriter out)
throws ServletException, IOException
{
try {
Db.load_db();
}
catch (Exception e) {
out.println("got exception during load: " + e);
System.out.println("got exception during load: " + e);
}
out.println("The servlet has been restarted, and Berkeley DB is loaded");
out.println("<p>If you're debugging, you should now start the debugger and set breakpoints.");
}
public void doXATransaction(PrintWriter out, String key, String value,
String operation)
throws ServletException, IOException
{
try {
int counter = incrCount();
if (key == null || key.equals(""))
key = "key" + counter;
if (value == null || value.equals(""))
value = "value" + counter;
out.println("Adding (\"" + key + "\", \"" + value + "\")");
System.out.println("XA transaction begin");
tm.begin();
System.out.println("getting XA transaction");
DbXAResource.DbAttach attach = DbXAResource.xa_attach(null, null);
DbTxn txn = attach.get_txn();
DbEnv env = attach.get_env();
Db db = new Db(env, 0);
db.open(txn, "my.db", null, Db.DB_BTREE, Db.DB_CREATE, 0644);
System.out.println("DB put " + key);
db.put(txn,
new Dbt(key.getBytes()),
new Dbt(value.getBytes()),
0);
if (operation.equals("rollback")) {
out.println("<p>ROLLBACK");
System.out.println("XA transaction rollback");
tm.rollback();
System.out.println("XA rollback returned");
// The old db is no good after the rollback
// since the open was part of the transaction.
// Get another db for the cursor dump
//
db = new Db(env, 0);
db.open(null, "my.db", null, Db.DB_BTREE, Db.DB_CREATE, 0644);
}
else {
out.println("<p>COMMITTED");
System.out.println("XA transaction commit");
tm.commit();
}
// Show the current state of the database.
Dbc dbc = db.cursor(null, 0);
Dbt gotkey = new Dbt();
Dbt gotdata = new Dbt();
out.println("<p>Current database values:");
while (dbc.get(gotkey, gotdata, Db.DB_NEXT) == 0) {
out.println("<br> " + getDbtString(gotkey) + " : "
+ getDbtString(gotdata));
}
dbc.close();
db.close(0);
}
catch (DbException dbe) {
System.err.println("Db Exception: " + dbe);
out.println(" *** Exception received: " + dbe);
dbe.printStackTrace();
}
catch (FileNotFoundException fnfe) {
System.err.println("FileNotFoundException: " + fnfe);
out.println(" *** Exception received: " + fnfe);
fnfe.printStackTrace();
}
// Includes SystemException, NotSupportedException, RollbackException
catch (Exception e) {
System.err.println("Exception: " + e);
out.println(" *** Exception received: " + e);
e.printStackTrace();
}
}
private static Xid getBogusXid()
throws XAException
{
return new DbXid(1, "BOGUS_gtrid".getBytes(),
"BOGUS_bqual".getBytes());
}
private static String getDbtString(Dbt dbt)
{
return new String(dbt.get_data(), 0, dbt.get_size());
}
/**
* doGet is called as a result of invoking the servlet.
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
try {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String key = req.getParameter("key");
String value = req.getParameter("value");
String operation = req.getParameter("operation");
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Berkeley DB with XA</TITLE>");
out.println("</HEAD><BODY>");
out.println("<a href=\"TestXAServlet" +
"\">Database put and commit</a><br>");
out.println("<a href=\"TestXAServlet?operation=rollback" +
"\">Database put and rollback</a><br>");
out.println("<a href=\"TestXAServlet?operation=close" +
"\">Close the XA resource manager</a><br>");
out.println("<a href=\"TestXAServlet?operation=forget" +
"\">Forget an operation (bypasses TM)</a><br>");
out.println("<a href=\"TestXAServlet?operation=prepare" +
"\">Prepare an operation (bypasses TM)</a><br>");
out.println("<br>");
if (!debugInited) {
// Don't initialize XA yet, give the user
// a chance to attach a debugger if necessary.
debugSetup(out);
debugInited = true;
}
else {
initialize();
if (operation == null)
operation = "commit";
if (operation.equals("close")) {
shutdown(out);
}
else if (operation.equals("forget")) {
// A bogus test, we just make sure the API is callable.
out.println("<p>FORGET");
System.out.println("XA forget bogus XID (bypass TM)");
xaresource.forget(getBogusXid());
}
else if (operation.equals("prepare")) {
// A bogus test, we just make sure the API is callable.
out.println("<p>PREPARE");
System.out.println("XA prepare bogus XID (bypass TM)");
xaresource.prepare(getBogusXid());
}
else {
// commit, rollback, prepare, forget
doXATransaction(out, key, value, operation);
}
}
out.println("</BODY></HTML>");
System.out.println("Finished.");
}
// Includes SystemException, NotSupportedException, RollbackException
catch (Exception e) {
System.err.println("Exception: " + e);
e.printStackTrace();
}
}
/**
* From weblogic's sample code:
* samples/examples/jta/jmsjdbc/Client.java
*/
private static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}
|