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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
/*
* $Id$
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2012 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
package virtuoso.jdbc2;
import java.io.IOException;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.Vector;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
public class VirtuosoXAResource implements XAResource
{
private final static int SQL_XA_ENLIST = 0x00f6;
private final static int SQL_XA_PREPARE = 0x0f01;
private final static int SQL_XA_COMMIT = 0x0f02;
private final static int SQL_XA_ROLLBACK = 0x0f03;
private final static int SQL_XA_END = 0x0f04;
private final static int SQL_XA_JOIN = 0x0f05;
private final static int SQL_XA_RESUME = 0x0f06;
private final static int SQL_XA_SUSPEND = 0x0f07;
private final static int SQL_XA_WAIT = 0x0f00;
private final String GET_ALL_XIDS = "_2PC.DBA.XA_GET_ALL_XIDS()";
private VirtuosoPooledConnection vConnection;
private XAResourceManager manager;
int txn_timeout = 0;
private boolean stored_auto_commit;
private Xid r_currentXid = null;
VirtuosoXAResource(VirtuosoPooledConnection vConnection, String server, int port) {
this.vConnection = vConnection;
this.manager = XAResourceManager.getManager(server, port);
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("new VirtuosoXAResource (+ con=" + vConnection.hashCode() + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
}
void reset_XA()
{
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.reset_XA () :" + hashCode());
VirtuosoFuture.rpc_log.flush();
}
}
if (vConnection == null)
return;
if (r_currentXid != null)
try {
end(r_currentXid, XAResource.TMFAIL);
}
catch (Exception e) {}
}
public int getTransactionTimeout() throws XAException {
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.getTransactionTimeout () ret " + txn_timeout + " :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
return txn_timeout;
}
public boolean setTransactionTimeout(int seconds) throws XAException {
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.setTransactionTimeout (" + seconds + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
if (seconds < 0) {
XAException xaex = new XAException("Invalid number of seconds : " + seconds);
xaex.errorCode = XAException.XAER_INVAL;
throw xaex;
} else if (seconds == 0) {
try {
txn_timeout = vConnection.getVirtuosoConnection().getTimeout();
} catch (SQLException ex) {
throw new XAException(ex.toString());
}
} else {
txn_timeout = seconds;
}
return true;
}
public boolean isSameRM(XAResource xaResource) throws XAException {
boolean ret;
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.isSameRM (res=" + xaResource.hashCode() + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
if (xaResource == null || !(xaResource instanceof VirtuosoXAResource))
ret = false;
else if (xaResource.equals (this))
ret = true;
else
{
VirtuosoXAResource that = (VirtuosoXAResource) xaResource;
ret = this.manager == that.manager;
}
return ret;
}
public void start(Xid xid, int param) throws XAException {
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.start (xid=" + xid.hashCode() + ", param=" + param + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
if (xid == null) {
throw new XAException(XAException.XAER_INVAL);
}
if (param != XAResource.TMNOFLAGS
&& param != XAResource.TMJOIN
&& param != XAResource.TMRESUME) {
throw new XAException(XAException.XAER_INVAL);
}
XATransaction transaction = null;
int start_param;
if (param == XAResource.TMJOIN) {
transaction = manager.getTransaction(xid);
if (transaction.getStatus() != XATransaction.ACTIVE
&& transaction.getStatus() != XATransaction.IDLE) {
throw new XAException(XAException.XAER_PROTO);
}
start_param = SQL_XA_JOIN;
} else if (param == XAResource.TMNOFLAGS) {
transaction = manager.createTransaction(xid, XATransaction.ACTIVE);
start_param = SQL_XA_ENLIST;
} else if (param == XAResource.TMRESUME) {
transaction = manager.getTransaction(xid);
if (transaction.getStatus() != XATransaction.ACTIVE) {
throw new XAException(XAException.XAER_PROTO);
}
start_param = SQL_XA_RESUME;
} else {
throw new XAException("Unsupported mode");
}
VirtuosoConnection con;
try {
con = vConnection.getVirtuosoConnection();
} catch (SQLException ex) {
throw new XAException(ex.toString());
}
enterGlobalTransaction(con);
try {
rpc(con, start_param, transaction.getXid().encode());
} catch (XAException ex) {
leaveGlobalTransaction(con);
throw ex;
}
r_currentXid = xid;
}
public void end(Xid xid, int param) throws XAException {
int end_param;
int nstatus = XATransaction.IDLE;
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.end (xid=" + xid.hashCode() + ", param=" + param + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
if (xid == null)
throw new XAException(XAException.XAER_INVAL);
XATransaction ctx = manager.getTransaction(xid);
if ((param & XAResource.TMSUSPEND) != 0)
{
end_param = SQL_XA_SUSPEND;
nstatus = XATransaction.ACTIVE;
}
else if (param == XAResource.TMSUCCESS)
{
end_param = SQL_XA_END;
}
else if (param == XAResource.TMFAIL)
{
end_param = SQL_XA_END;
nstatus = XATransaction.ROLLBACKONLY;
}
else
{
throw new XAException("Invalid flag.");
}
ctx.checkNewStatus(nstatus);
VirtuosoConnection con;
try {
con = vConnection.getVirtuosoConnection();
} catch (SQLException ex) {
throw new XAException(ex.toString());
}
rpc(con, end_param, ctx.getXid().encode());
leaveGlobalTransaction(con);
ctx.changeStatus(nstatus);
}
public int prepare(Xid xid) throws XAException {
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.prepare (xid=" + xid.hashCode() + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
if (xid == null)
throw new XAException(XAException.XAER_INVAL);
XATransaction ctx = manager.getTransaction(xid);
ctx.checkNewStatus(XATransaction.PREPARED);
VirtuosoConnection con;
try {
con = vConnection.getVirtuosoConnection();
} catch (SQLException ex) {
throw new XAException(ex.toString());
}
rpc(con, SQL_XA_PREPARE, ctx.getXid().encode());
ctx.changeStatus(XATransaction.PREPARED);
return XAResource.XA_OK;
}
public void commit(Xid xid, boolean onePhase) throws XAException {
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.commit (xid=" + xid.hashCode() + ", onePhase=" + onePhase + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
if (xid == null)
throw new XAException(XAException.XAER_INVAL);
XATransaction ctx = manager.getTransaction(xid);
ctx.checkNewStatus(XATransaction.COMMITTED, onePhase);
transact(ctx, SQL_XA_COMMIT);
ctx.changeStatus(XATransaction.COMMITTED, onePhase);
manager.removeTransaction(xid);
}
public void rollback(Xid xid) throws XAException {
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.rollback (xid=" + xid.hashCode() + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
if (xid == null)
throw new XAException(XAException.XAER_INVAL);
XATransaction ctx = manager.getTransaction(xid);
ctx.checkNewStatus(XATransaction.ROLLEDBACK);
transact(ctx, SQL_XA_ROLLBACK);
ctx.changeStatus(XATransaction.ROLLEDBACK);
manager.removeTransaction(xid);
}
public Xid[] recover(int param) throws XAException {
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.recover (param=" + param + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
if (param != XAResource.TMSTARTRSCAN)
return new Xid[0];
#if JDK_VER >= 16
Vector<VirtuosoXid> xidv = new Vector<VirtuosoXid> ();
#else
Vector xidv = new Vector ();
#endif
try {
VirtuosoConnection con = vConnection.getVirtuosoConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery (GET_ALL_XIDS);
while (rs.next ()) {
String s = rs.getString (1);
VirtuosoXid xid = VirtuosoXid.decode (s);
xidv.add (xid);
manager.createTransaction(xid, XATransaction.PREPARED);
}
} catch (SQLException e) {
throw new XAException (e.toString());
}
Xid[] xids = new Xid[xidv.size()];
for (int i = 0; i < xids.length; i++)
xids[i] = (Xid) xidv.get (i);
return xids;
}
public void forget(Xid xid) throws XAException {
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.forget (xid=" + xid.hashCode() + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
if (xid == null)
throw new XAException(XAException.XAER_INVAL);
manager.removeTransaction(xid);
}
private void enterGlobalTransaction(VirtuosoConnection connection) {
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.enterGlobalTransaction (conn=" + connection.hashCode() + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
if (connection == null)
return;
try {
stored_auto_commit = connection.getAutoCommit();
connection.setAutoCommit(false);
} catch (Exception e) {
}
connection.setGlobalTransaction(true);
}
private void leaveGlobalTransaction(VirtuosoConnection connection) {
if (VirtuosoFuture.rpc_log != null)
{
synchronized (VirtuosoFuture.rpc_log)
{
VirtuosoFuture.rpc_log.println ("VirtuosoXAResource.leaveGlobalTransaction (conn=" + connection.hashCode() + ") :" + hashCode() + ")");
VirtuosoFuture.rpc_log.flush();
}
}
if (connection == null)
return;
connection.setGlobalTransaction(false);
try {
connection.setAutoCommit(stored_auto_commit);
} catch (Exception e) {
}
r_currentXid = null;
}
private void transact(XATransaction ctx, int action) throws XAException {
Object encodedXid = ctx.getXid().encode();
VirtuosoConnection con;
try {
con = vConnection.getVirtuosoConnection();
} catch (SQLException ex) {
throw new XAException(ex.toString());
}
rpc(con, action, encodedXid);
rpc(con, SQL_XA_WAIT, encodedXid);
}
private void rpc(VirtuosoConnection connection, int action, Object encodedXid)
throws XAException {
Object[] args = new Object[2];
args[0] = new Integer(action);
args[1] = encodedXid;
try {
synchronized (connection) {
VirtuosoFuture future =
connection.getFuture(
VirtuosoFuture.tp_transaction,
args,
connection.timeout);
openlink.util.Vector res = future.nextResult();
Object err = (res == null ? null : res.firstElement());
if (err instanceof openlink.util.Vector) {
throw new XAException();
}
connection.removeFuture(future);
}
} catch (IOException ex) {
//System.out.println("VirtuosoXAResource.rpc(): Exception caught: " + ex);
throw new XAException(XAException.XAER_RMERR);
} catch (VirtuosoException ex) {
//System.out.println("VirtuosoXAResource.rpc(): Exception caught: " + ex);
throw new XAException(ex.toString());
}
}
}
|