| 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.command.dml; |
| 7 |
|
| 8 |
import org.h2.command.CommandInterface; |
| 9 |
import org.h2.command.Prepared; |
| 10 |
import org.h2.engine.Database; |
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.result.ResultInterface; |
| 14 |
|
| 15 |
/** |
| 16 |
* Represents a transactional statement. |
| 17 |
*/ |
| 18 |
public class TransactionCommand extends Prepared { |
| 19 |
|
| 20 |
private final int type; |
| 21 |
private String savepointName; |
| 22 |
private String transactionName; |
| 23 |
|
| 24 |
public TransactionCommand(SessionLocal session, int type) { |
| 25 |
super(session); |
| 26 |
this.type = type; |
| 27 |
} |
| 28 |
|
| 29 |
public void setSavepointName(String name) { |
| 30 |
this.savepointName = name; |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
public long update() { |
| 35 |
switch (type) { |
| 36 |
case CommandInterface.SET_AUTOCOMMIT_TRUE: |
| 37 |
session.setAutoCommit(true); |
| 38 |
break; |
| 39 |
case CommandInterface.SET_AUTOCOMMIT_FALSE: |
| 40 |
session.setAutoCommit(false); |
| 41 |
break; |
| 42 |
case CommandInterface.BEGIN: |
| 43 |
session.begin(); |
| 44 |
break; |
| 45 |
case CommandInterface.COMMIT: |
| 46 |
session.commit(false); |
| 47 |
break; |
| 48 |
case CommandInterface.ROLLBACK: |
| 49 |
session.rollback(); |
| 50 |
break; |
| 51 |
case CommandInterface.CHECKPOINT: |
| 52 |
session.getUser().checkAdmin(); |
| 53 |
getDatabase().checkpoint(); |
| 54 |
break; |
| 55 |
case CommandInterface.SAVEPOINT: |
| 56 |
session.addSavepoint(savepointName); |
| 57 |
break; |
| 58 |
case CommandInterface.ROLLBACK_TO_SAVEPOINT: |
| 59 |
session.rollbackToSavepoint(savepointName); |
| 60 |
break; |
| 61 |
case CommandInterface.CHECKPOINT_SYNC: |
| 62 |
session.getUser().checkAdmin(); |
| 63 |
getDatabase().sync(); |
| 64 |
break; |
| 65 |
case CommandInterface.PREPARE_COMMIT: |
| 66 |
session.prepareCommit(transactionName); |
| 67 |
break; |
| 68 |
case CommandInterface.COMMIT_TRANSACTION: |
| 69 |
session.getUser().checkAdmin(); |
| 70 |
session.setPreparedTransaction(transactionName, true); |
| 71 |
break; |
| 72 |
case CommandInterface.ROLLBACK_TRANSACTION: |
| 73 |
session.getUser().checkAdmin(); |
| 74 |
session.setPreparedTransaction(transactionName, false); |
| 75 |
break; |
| 76 |
case CommandInterface.SHUTDOWN: |
| 77 |
case CommandInterface.SHUTDOWN_COMPACT: |
| 78 |
case CommandInterface.SHUTDOWN_DEFRAG: |
| 79 |
session.commit(false); |
| 80 |
//$FALL-THROUGH$ |
| 81 |
case CommandInterface.SHUTDOWN_IMMEDIATELY: { |
| 82 |
session.getUser().checkAdmin(); |
| 83 |
// throttle, to allow testing concurrent |
| 84 |
// execution of shutdown and query |
| 85 |
session.throttle(); |
| 86 |
Database db = getDatabase(); |
| 87 |
if (db.setExclusiveSession(session, true)) { |
| 88 |
db.setCompactMode(type); |
| 89 |
// close the database, but don't update the persistent setting |
| 90 |
db.setCloseDelay(0); |
| 91 |
session.close(); |
| 92 |
} |
| 93 |
break; |
| 94 |
} |
| 95 |
default: |
| 96 |
throw DbException.getInternalError("type=" + type); |
| 97 |
} |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public boolean isTransactional() { |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public boolean needRecompile() { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
public void setTransactionName(String string) { |
| 112 |
this.transactionName = string; |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public ResultInterface queryMeta() { |
| 117 |
return null; |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public int getType() { |
| 122 |
return type; |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public boolean isCacheable() { |
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
} |