| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.jdbcx; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.ResultSet; |
| 10 |
import java.sql.SQLException; |
| 11 |
import java.sql.Statement; |
| 12 |
import java.util.ArrayList; |
| 13 |
|
| 14 |
import javax.sql.ConnectionEvent; |
| 15 |
import javax.sql.ConnectionEventListener; |
| 16 |
import javax.sql.StatementEventListener; |
| 17 |
import javax.sql.XAConnection; |
| 18 |
import javax.transaction.xa.XAException; |
| 19 |
import javax.transaction.xa.XAResource; |
| 20 |
import javax.transaction.xa.Xid; |
| 21 |
|
| 22 |
import org.h2.api.ErrorCode; |
| 23 |
import org.h2.jdbc.JdbcConnection; |
| 24 |
import org.h2.message.DbException; |
| 25 |
import org.h2.message.TraceObject; |
| 26 |
import org.h2.util.Utils; |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* This class provides support for distributed transactions. |
| 31 |
* An application developer usually does not use this interface. |
| 32 |
* It is used by the transaction manager internally. |
| 33 |
*/ |
| 34 |
public final class JdbcXAConnection extends TraceObject implements XAConnection, |
| 35 |
XAResource { |
| 36 |
|
| 37 |
private final JdbcDataSourceFactory factory; |
| 38 |
|
| 39 |
// this connection is kept open as long as the XAConnection is alive |
| 40 |
private JdbcConnection physicalConn; |
| 41 |
|
| 42 |
// this connection is replaced whenever getConnection is called |
| 43 |
private volatile Connection handleConn; |
| 44 |
private final ArrayList<ConnectionEventListener> listeners = Utils.newSmallArrayList(); |
| 45 |
private Xid currentTransaction; |
| 46 |
private boolean prepared; |
| 47 |
|
| 48 |
JdbcXAConnection(JdbcDataSourceFactory factory, int id, |
| 49 |
JdbcConnection physicalConn) { |
| 50 |
this.factory = factory; |
| 51 |
setTrace(factory.getTrace(), TraceObject.XA_DATA_SOURCE, id); |
| 52 |
this.physicalConn = physicalConn; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get the XAResource object. |
| 57 |
* |
| 58 |
* @return itself |
| 59 |
*/ |
| 60 |
@Override |
| 61 |
public XAResource getXAResource() { |
| 62 |
debugCodeCall("getXAResource"); |
| 63 |
return this; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Close the physical connection. |
| 68 |
* This method is usually called by the connection pool. |
| 69 |
*/ |
| 70 |
@Override |
| 71 |
public void close() throws SQLException { |
| 72 |
debugCodeCall("close"); |
| 73 |
Connection lastHandle = handleConn; |
| 74 |
if (lastHandle != null) { |
| 75 |
listeners.clear(); |
| 76 |
lastHandle.close(); |
| 77 |
} |
| 78 |
if (physicalConn != null) { |
| 79 |
try { |
| 80 |
physicalConn.close(); |
| 81 |
} finally { |
| 82 |
physicalConn = null; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get a connection that is a handle to the physical connection. This method |
| 89 |
* is usually called by the connection pool. This method closes the last |
| 90 |
* connection handle if one exists. |
| 91 |
* |
| 92 |
* @return the connection |
| 93 |
*/ |
| 94 |
@Override |
| 95 |
public Connection getConnection() throws SQLException { |
| 96 |
debugCodeCall("getConnection"); |
| 97 |
Connection lastHandle = handleConn; |
| 98 |
if (lastHandle != null) { |
| 99 |
lastHandle.close(); |
| 100 |
} |
| 101 |
// this will ensure the rollback command is cached |
| 102 |
physicalConn.rollback(); |
| 103 |
handleConn = new PooledJdbcConnection(physicalConn); |
| 104 |
return handleConn; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Register a new listener for the connection. |
| 109 |
* |
| 110 |
* @param listener the event listener |
| 111 |
*/ |
| 112 |
@Override |
| 113 |
public void addConnectionEventListener(ConnectionEventListener listener) { |
| 114 |
debugCode("addConnectionEventListener(listener)"); |
| 115 |
listeners.add(listener); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Remove the event listener. |
| 120 |
* |
| 121 |
* @param listener the event listener |
| 122 |
*/ |
| 123 |
@Override |
| 124 |
public void removeConnectionEventListener(ConnectionEventListener listener) { |
| 125 |
debugCode("removeConnectionEventListener(listener)"); |
| 126 |
listeners.remove(listener); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* INTERNAL |
| 131 |
*/ |
| 132 |
void closedHandle() { |
| 133 |
debugCodeCall("closedHandle"); |
| 134 |
ConnectionEvent event = new ConnectionEvent(this); |
| 135 |
// go backward so that a listener can remove itself |
| 136 |
// (otherwise we need to clone the list) |
| 137 |
for (int i = listeners.size() - 1; i >= 0; i--) { |
| 138 |
ConnectionEventListener listener = listeners.get(i); |
| 139 |
listener.connectionClosed(event); |
| 140 |
} |
| 141 |
handleConn = null; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get the transaction timeout. |
| 146 |
* |
| 147 |
* @return 0 |
| 148 |
*/ |
| 149 |
@Override |
| 150 |
public int getTransactionTimeout() { |
| 151 |
debugCodeCall("getTransactionTimeout"); |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Set the transaction timeout. |
| 157 |
* |
| 158 |
* @param seconds ignored |
| 159 |
* @return false |
| 160 |
*/ |
| 161 |
@Override |
| 162 |
public boolean setTransactionTimeout(int seconds) { |
| 163 |
debugCodeCall("setTransactionTimeout", seconds); |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Checks if this is the same XAResource. |
| 169 |
* |
| 170 |
* @param xares the other object |
| 171 |
* @return true if this is the same object |
| 172 |
*/ |
| 173 |
@Override |
| 174 |
public boolean isSameRM(XAResource xares) { |
| 175 |
debugCode("isSameRM(xares)"); |
| 176 |
return xares == this; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Get the list of prepared transaction branches. This method is called by |
| 181 |
* the transaction manager during recovery. |
| 182 |
* |
| 183 |
* @param flag TMSTARTRSCAN, TMENDRSCAN, or TMNOFLAGS. If no other flags are |
| 184 |
* set, TMNOFLAGS must be used. |
| 185 |
* @return zero or more Xid objects |
| 186 |
*/ |
| 187 |
@Override |
| 188 |
public Xid[] recover(int flag) throws XAException { |
| 189 |
debugCodeCall("recover", quoteFlags(flag)); |
| 190 |
checkOpen(); |
| 191 |
try (Statement stat = physicalConn.createStatement()) { |
| 192 |
ResultSet rs = stat.executeQuery("SELECT * FROM INFORMATION_SCHEMA.IN_DOUBT ORDER BY TRANSACTION_NAME"); |
| 193 |
ArrayList<Xid> list = Utils.newSmallArrayList(); |
| 194 |
while (rs.next()) { |
| 195 |
String tid = rs.getString("TRANSACTION_NAME"); |
| 196 |
int id = getNextId(XID); |
| 197 |
Xid xid = new JdbcXid(factory, id, tid); |
| 198 |
list.add(xid); |
| 199 |
} |
| 200 |
rs.close(); |
| 201 |
Xid[] result = list.toArray(new Xid[0]); |
| 202 |
if (!list.isEmpty()) { |
| 203 |
prepared = true; |
| 204 |
} |
| 205 |
return result; |
| 206 |
} catch (SQLException e) { |
| 207 |
XAException xa = new XAException(XAException.XAER_RMERR); |
| 208 |
xa.initCause(e); |
| 209 |
throw xa; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Prepare a transaction. |
| 215 |
* |
| 216 |
* @param xid the transaction id |
| 217 |
* @return XA_OK |
| 218 |
*/ |
| 219 |
@Override |
| 220 |
public int prepare(Xid xid) throws XAException { |
| 221 |
if (isDebugEnabled()) { |
| 222 |
debugCode("prepare(" + quoteXid(xid) + ')'); |
| 223 |
} |
| 224 |
checkOpen(); |
| 225 |
if (!currentTransaction.equals(xid)) { |
| 226 |
throw new XAException(XAException.XAER_INVAL); |
| 227 |
} |
| 228 |
|
| 229 |
try (Statement stat = physicalConn.createStatement()) { |
| 230 |
stat.execute(JdbcXid.toString(new StringBuilder("PREPARE COMMIT \""), xid).append('"').toString()); |
| 231 |
prepared = true; |
| 232 |
} catch (SQLException e) { |
| 233 |
throw convertException(e); |
| 234 |
} |
| 235 |
return XA_OK; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Forget a transaction. |
| 240 |
* This method does not have an effect for this database. |
| 241 |
* |
| 242 |
* @param xid the transaction id |
| 243 |
*/ |
| 244 |
@Override |
| 245 |
public void forget(Xid xid) { |
| 246 |
if (isDebugEnabled()) { |
| 247 |
debugCode("forget(" + quoteXid(xid) + ')'); |
| 248 |
} |
| 249 |
prepared = false; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Roll back a transaction. |
| 254 |
* |
| 255 |
* @param xid the transaction id |
| 256 |
*/ |
| 257 |
@Override |
| 258 |
public void rollback(Xid xid) throws XAException { |
| 259 |
if (isDebugEnabled()) { |
| 260 |
debugCode("rollback(" + quoteXid(xid) + ')'); |
| 261 |
} |
| 262 |
try { |
| 263 |
if (prepared) { |
| 264 |
try (Statement stat = physicalConn.createStatement()) { |
| 265 |
stat.execute(JdbcXid.toString( // |
| 266 |
new StringBuilder("ROLLBACK TRANSACTION \""), xid).append('"').toString()); |
| 267 |
} |
| 268 |
prepared = false; |
| 269 |
} else { |
| 270 |
physicalConn.rollback(); |
| 271 |
} |
| 272 |
physicalConn.setAutoCommit(true); |
| 273 |
} catch (SQLException e) { |
| 274 |
throw convertException(e); |
| 275 |
} |
| 276 |
currentTransaction = null; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* End a transaction. |
| 281 |
* |
| 282 |
* @param xid the transaction id |
| 283 |
* @param flags TMSUCCESS, TMFAIL, or TMSUSPEND |
| 284 |
*/ |
| 285 |
@Override |
| 286 |
public void end(Xid xid, int flags) throws XAException { |
| 287 |
if (isDebugEnabled()) { |
| 288 |
debugCode("end(" + quoteXid(xid) + ", " + quoteFlags(flags) + ')'); |
| 289 |
} |
| 290 |
// TODO transaction end: implement this method |
| 291 |
if (flags == TMSUSPEND) { |
| 292 |
return; |
| 293 |
} |
| 294 |
if (!currentTransaction.equals(xid)) { |
| 295 |
throw new XAException(XAException.XAER_OUTSIDE); |
| 296 |
} |
| 297 |
prepared = false; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Start or continue to work on a transaction. |
| 302 |
* |
| 303 |
* @param xid the transaction id |
| 304 |
* @param flags TMNOFLAGS, TMJOIN, or TMRESUME |
| 305 |
*/ |
| 306 |
@Override |
| 307 |
public void start(Xid xid, int flags) throws XAException { |
| 308 |
if (isDebugEnabled()) { |
| 309 |
debugCode("start(" + quoteXid(xid) + ", " + quoteFlags(flags) + ')'); |
| 310 |
} |
| 311 |
if (flags == TMRESUME) { |
| 312 |
return; |
| 313 |
} |
| 314 |
if (flags == TMJOIN) { |
| 315 |
if (currentTransaction != null && !currentTransaction.equals(xid)) { |
| 316 |
throw new XAException(XAException.XAER_RMERR); |
| 317 |
} |
| 318 |
} else if (currentTransaction != null) { |
| 319 |
throw new XAException(XAException.XAER_NOTA); |
| 320 |
} |
| 321 |
try { |
| 322 |
physicalConn.setAutoCommit(false); |
| 323 |
} catch (SQLException e) { |
| 324 |
throw convertException(e); |
| 325 |
} |
| 326 |
currentTransaction = xid; |
| 327 |
prepared = false; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Commit a transaction. |
| 332 |
* |
| 333 |
* @param xid the transaction id |
| 334 |
* @param onePhase use a one-phase protocol if true |
| 335 |
*/ |
| 336 |
@Override |
| 337 |
public void commit(Xid xid, boolean onePhase) throws XAException { |
| 338 |
if (isDebugEnabled()) { |
| 339 |
debugCode("commit(" + quoteXid(xid) + ", " + onePhase + ')'); |
| 340 |
} |
| 341 |
|
| 342 |
try { |
| 343 |
if (onePhase) { |
| 344 |
physicalConn.commit(); |
| 345 |
} else { |
| 346 |
try (Statement stat = physicalConn.createStatement()) { |
| 347 |
stat.execute( |
| 348 |
JdbcXid.toString(new StringBuilder("COMMIT TRANSACTION \""), xid).append('"').toString()); |
| 349 |
prepared = false; |
| 350 |
} |
| 351 |
} |
| 352 |
physicalConn.setAutoCommit(true); |
| 353 |
} catch (SQLException e) { |
| 354 |
throw convertException(e); |
| 355 |
} |
| 356 |
currentTransaction = null; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* [Not supported] Add a statement event listener. |
| 361 |
* |
| 362 |
* @param listener the new statement event listener |
| 363 |
*/ |
| 364 |
@Override |
| 365 |
public void addStatementEventListener(StatementEventListener listener) { |
| 366 |
throw new UnsupportedOperationException(); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* [Not supported] Remove a statement event listener. |
| 371 |
* |
| 372 |
* @param listener the statement event listener |
| 373 |
*/ |
| 374 |
@Override |
| 375 |
public void removeStatementEventListener(StatementEventListener listener) { |
| 376 |
throw new UnsupportedOperationException(); |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* INTERNAL |
| 381 |
*/ |
| 382 |
@Override |
| 383 |
public String toString() { |
| 384 |
return getTraceObjectName() + ": " + physicalConn; |
| 385 |
} |
| 386 |
|
| 387 |
private static XAException convertException(SQLException e) { |
| 388 |
XAException xa = new XAException(e.getMessage()); |
| 389 |
xa.initCause(e); |
| 390 |
return xa; |
| 391 |
} |
| 392 |
|
| 393 |
private static String quoteXid(Xid xid) { |
| 394 |
return JdbcXid.toString(new StringBuilder(), xid).toString().replace('-', '$'); |
| 395 |
} |
| 396 |
|
| 397 |
private static String quoteFlags(int flags) { |
| 398 |
StringBuilder buff = new StringBuilder(); |
| 399 |
if ((flags & XAResource.TMENDRSCAN) != 0) { |
| 400 |
buff.append("|XAResource.TMENDRSCAN"); |
| 401 |
} |
| 402 |
if ((flags & XAResource.TMFAIL) != 0) { |
| 403 |
buff.append("|XAResource.TMFAIL"); |
| 404 |
} |
| 405 |
if ((flags & XAResource.TMJOIN) != 0) { |
| 406 |
buff.append("|XAResource.TMJOIN"); |
| 407 |
} |
| 408 |
if ((flags & XAResource.TMONEPHASE) != 0) { |
| 409 |
buff.append("|XAResource.TMONEPHASE"); |
| 410 |
} |
| 411 |
if ((flags & XAResource.TMRESUME) != 0) { |
| 412 |
buff.append("|XAResource.TMRESUME"); |
| 413 |
} |
| 414 |
if ((flags & XAResource.TMSTARTRSCAN) != 0) { |
| 415 |
buff.append("|XAResource.TMSTARTRSCAN"); |
| 416 |
} |
| 417 |
if ((flags & XAResource.TMSUCCESS) != 0) { |
| 418 |
buff.append("|XAResource.TMSUCCESS"); |
| 419 |
} |
| 420 |
if ((flags & XAResource.TMSUSPEND) != 0) { |
| 421 |
buff.append("|XAResource.TMSUSPEND"); |
| 422 |
} |
| 423 |
if ((flags & XAResource.XA_RDONLY) != 0) { |
| 424 |
buff.append("|XAResource.XA_RDONLY"); |
| 425 |
} |
| 426 |
if (buff.length() == 0) { |
| 427 |
buff.append("|XAResource.TMNOFLAGS"); |
| 428 |
} |
| 429 |
return buff.substring(1); |
| 430 |
} |
| 431 |
|
| 432 |
private void checkOpen() throws XAException { |
| 433 |
if (physicalConn == null) { |
| 434 |
throw new XAException(XAException.XAER_RMERR); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* A pooled connection. |
| 440 |
*/ |
| 441 |
final class PooledJdbcConnection extends JdbcConnection { |
| 442 |
|
| 443 |
private boolean isClosed; |
| 444 |
|
| 445 |
public PooledJdbcConnection(JdbcConnection conn) { |
| 446 |
super(conn); |
| 447 |
} |
| 448 |
|
| 449 |
@Override |
| 450 |
public synchronized void close() throws SQLException { |
| 451 |
if (!isClosed) { |
| 452 |
try { |
| 453 |
rollback(); |
| 454 |
setAutoCommit(true); |
| 455 |
} catch (SQLException e) { |
| 456 |
// ignore |
| 457 |
} |
| 458 |
closedHandle(); |
| 459 |
isClosed = true; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
@Override |
| 464 |
public synchronized boolean isClosed() throws SQLException { |
| 465 |
return isClosed || super.isClosed(); |
| 466 |
} |
| 467 |
|
| 468 |
@Override |
| 469 |
protected synchronized void checkClosed() { |
| 470 |
if (isClosed) { |
| 471 |
throw DbException.get(ErrorCode.OBJECT_CLOSED); |
| 472 |
} |
| 473 |
super.checkClosed(); |
| 474 |
} |
| 475 |
|
| 476 |
} |
| 477 |
|
| 478 |
} |