File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\dml\TransactionCommand.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.dml;
8
9 import org.h2.command.CommandInterface;
10 import org.h2.command.Prepared;
11 import org.h2.engine.Database;
12 import org.h2.engine.Session;
13 import org.h2.message.DbException;
14 import org.h2.result.ResultInterface;
15
16 /**
17 * Represents a transactional statement.
18 */
19 public class TransactionCommand extends Prepared {
20
21 private final int type;
22 private String savepointName;
23 private String transactionName;
24
25 public TransactionCommand(Session session, int type) {
26 super(session);
27 this.type = type;
28 }
29
30 public void setSavepointName(String name) {
31 this.savepointName = name;
32 }
33
34 public int 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 session.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 session.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_IMMEDIATELY:
77 session.getUser().checkAdmin();
78 session.getDatabase().shutdownImmediately();
79 break;
80 case CommandInterface.SHUTDOWN:
81 case CommandInterface.SHUTDOWN_COMPACT:
82 case CommandInterface.SHUTDOWN_DEFRAG: {
83 session.getUser().checkAdmin();
84 session.commit(false);
85 if (type == CommandInterface.SHUTDOWN_COMPACT || type == CommandInterface.SHUTDOWN_DEFRAG) {
86 session.getDatabase().setCompactMode(type);
87 }
88 // close the database, but don't update the persistent setting
89 session.getDatabase().setCloseDelay(0);
90 Database db = session.getDatabase();
91 // throttle, to allow testing concurrent
92 // execution of shutdown and query
93 session.throttle();
94 for (Session s : db.getSessions(false)) {
95 if (db.isMultiThreaded()) {
96 synchronized (s) {
97 s.rollback();
98 }
99 } else {
100 // if not multi-threaded, the session could already own
101 // the lock, which would result in a deadlock
102 // the other session can not concurrently do anything
103 // because the current session has locked the database
104 s.rollback();
105 }
106 if (s != session) {
107 s.close();
108 }
109 }
110 session.close();
111 break;
112 }
113 default:
114 DbException.throwInternalError("type=" + type);
115 }
116 return 0;
117 }
118
119 public boolean isTransactional() {
120 return true;
121 }
122
123 public boolean needRecompile() {
124 return false;
125 }
126
127 public void setTransactionName(String string) {
128 this.transactionName = string;
129 }
130
131 public ResultInterface queryMeta() {
132 return null;
133 }
134
135 public int getType() {
136 return type;
137 }
138
139 public boolean isCacheable() {
140 return true;
141 }
142
143 }
144