001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.command.dml;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.command.Prepared;
011: import org.h2.engine.Database;
012: import org.h2.engine.Session;
013: import org.h2.log.LogSystem;
014: import org.h2.message.Message;
015: import org.h2.result.LocalResult;
016: import org.h2.util.TempFileDeleter;
017:
018: /**
019: * Represents a transactional statement.
020: */
021: public class TransactionCommand extends Prepared {
022: public static final int AUTOCOMMIT_TRUE = 1;
023: public static final int AUTOCOMMIT_FALSE = 2;
024: public static final int COMMIT = 3;
025: public static final int ROLLBACK = 4;
026: public static final int CHECKPOINT = 5;
027: public static final int SAVEPOINT = 6;
028: public static final int ROLLBACK_TO_SAVEPOINT = 7;
029: public static final int CHECKPOINT_SYNC = 8;
030: public static final int PREPARE_COMMIT = 9;
031: public static final int COMMIT_TRANSACTION = 10;
032: public static final int ROLLBACK_TRANSACTION = 11;
033: public static final int SHUTDOWN = 12;
034: public static final int SHUTDOWN_IMMEDIATELY = 13;
035: public static final int BEGIN = 14;
036:
037: private int type;
038: private String savepointName;
039: private String transactionName;
040:
041: public TransactionCommand(Session session, int type) {
042: super (session);
043: this .type = type;
044: }
045:
046: public void setSavepointName(String name) {
047: this .savepointName = name;
048: }
049:
050: public int update() throws SQLException {
051: switch (type) {
052: case AUTOCOMMIT_TRUE:
053: session.setAutoCommit(true);
054: break;
055: case AUTOCOMMIT_FALSE:
056: session.setAutoCommit(false);
057: break;
058: case BEGIN:
059: session.begin();
060: break;
061: case COMMIT:
062: session.commit(false);
063: break;
064: case ROLLBACK:
065: session.rollback();
066: break;
067: case CHECKPOINT:
068: session.getUser().checkAdmin();
069: session.getDatabase().getLog().checkpoint();
070: TempFileDeleter.deleteUnused();
071: break;
072: case SAVEPOINT:
073: session.addSavepoint(savepointName);
074: break;
075: case ROLLBACK_TO_SAVEPOINT:
076: session.rollbackToSavepoint(savepointName);
077: break;
078: case CHECKPOINT_SYNC:
079: session.getUser().checkAdmin();
080: session.getDatabase().sync();
081: break;
082: case PREPARE_COMMIT:
083: session.prepareCommit(transactionName);
084: break;
085: case COMMIT_TRANSACTION:
086: session.getUser().checkAdmin();
087: session.setPreparedTransaction(transactionName, true);
088: break;
089: case ROLLBACK_TRANSACTION:
090: session.getUser().checkAdmin();
091: session.setPreparedTransaction(transactionName, false);
092: break;
093: case SHUTDOWN_IMMEDIATELY:
094: session.getUser().checkAdmin();
095: session.getDatabase().setPowerOffCount(1);
096: try {
097: session.getDatabase().checkPowerOff();
098: } catch (SQLException e) {
099: // ignore
100: }
101: break;
102: case SHUTDOWN: {
103: session.getUser().checkAdmin();
104: session.commit(false);
105: // close the database, but don't update the persistent setting
106: session.getDatabase().setCloseDelay(0);
107: Database db = session.getDatabase();
108: Session[] sessions = db.getSessions();
109: for (int i = 0; i < sessions.length; i++) {
110: Session s = sessions[i];
111: synchronized (s) {
112: s.rollback();
113: }
114: if (s != session) {
115: s.close();
116: }
117: }
118: LogSystem log = db.getLog();
119: log.setDisabled(false);
120: log.checkpoint();
121: session.close();
122: break;
123: }
124: default:
125: throw Message.getInternalError("type=" + type);
126: }
127: return 0;
128: }
129:
130: public boolean isTransactional() {
131: return true;
132: }
133:
134: public boolean needRecompile() {
135: return false;
136: }
137:
138: public void setTransactionName(String string) {
139: this .transactionName = string;
140: }
141:
142: public LocalResult queryMeta() {
143: return null;
144: }
145:
146: }
|