KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > dml > TransactionCommand


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.dml;
6
7 import java.io.FileNotFoundException JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.sql.SQLException JavaDoc;
10 import java.util.zip.ZipEntry JavaDoc;
11 import java.util.zip.ZipFile JavaDoc;
12 import java.util.zip.ZipOutputStream JavaDoc;
13
14 import org.h2.command.Prepared;
15 import org.h2.engine.Database;
16 import org.h2.engine.Session;
17 import org.h2.message.Message;
18 import org.h2.store.DiskFile;
19
20
21 /**
22  * @author Thomas
23  */

24 public class TransactionCommand extends Prepared {
25     public static final int AUTOCOMMIT_TRUE = 1;
26     public static final int AUTOCOMMIT_FALSE = 2;
27     public static final int COMMIT = 3;
28     public static final int ROLLBACK = 4;
29     public static final int CHECKPOINT = 5;
30     public static final int SAVEPOINT = 6;
31     public static final int ROLLBACK_TO_SAVEPOINT = 7;
32     public static final int CHECKPOINT_SYNC = 8;
33     public static final int PREPARE_COMMIT = 9;
34     public static final int COMMIT_TRANSACTION = 10;
35     public static final int ROLLBACK_TRANSACTION = 11;
36     public static final int SHUTDOWN = 12;
37     public static final int SHUTDOWN_IMMEDIATELY = 13;
38
39     private int type;
40     private String JavaDoc savepointName;
41     private String JavaDoc transactionName;
42
43     public TransactionCommand(Session session, int type) {
44         super(session);
45         this.type = type;
46     }
47
48     public void setSavepointName(String JavaDoc name) {
49         this.savepointName = name;
50     }
51
52     public int update() throws SQLException JavaDoc {
53         switch (type) {
54         case AUTOCOMMIT_TRUE:
55             session.setAutoCommit(true);
56             break;
57         case AUTOCOMMIT_FALSE:
58             session.setAutoCommit(false);
59             break;
60         case COMMIT:
61             session.commit();
62             break;
63         case ROLLBACK:
64             session.rollback();
65             break;
66         case CHECKPOINT:
67             session.getUser().checkAdmin();
68             session.getDatabase().getLog().checkpoint();
69             break;
70         case SAVEPOINT:
71             session.addSavepoint(savepointName);
72             break;
73         case ROLLBACK_TO_SAVEPOINT:
74             session.rollbackToSavepoint(savepointName);
75             break;
76         case CHECKPOINT_SYNC:
77             session.getUser().checkAdmin();
78             session.getDatabase().sync();
79             break;
80         case PREPARE_COMMIT:
81             session.prepareCommit(transactionName);
82             break;
83         case COMMIT_TRANSACTION:
84             session.getUser().checkAdmin();
85             session.setPreparedTransaction(transactionName, true);
86             break;
87         case ROLLBACK_TRANSACTION:
88             session.getUser().checkAdmin();
89             session.setPreparedTransaction(transactionName, false);
90             break;
91         case SHUTDOWN_IMMEDIATELY:
92             session.getUser().checkAdmin();
93             session.getDatabase().setPowerOffCount(1);
94             try {
95                 session.getDatabase().checkPowerOff();
96             } catch(SQLException JavaDoc e) {
97                 // ignore
98
}
99             break;
100         case SHUTDOWN: {
101             session.getUser().checkAdmin();
102             session.commit();
103             // close the database, but don't update the persistent setting
104
session.getDatabase().setCloseDelay(0);
105             Database db = session.getDatabase();
106             Session[] sessions = db.getSessions();
107             for(int i=0; i<sessions.length; i++) {
108                 Session s = sessions[i];
109                 synchronized(s) {
110                     s.rollback();
111                 }
112                 if(s != session) {
113                     s.close();
114                 }
115             }
116             db.getLog().checkpoint();
117             session.close();
118             break;
119         }
120 // case BACKUP: {
121
// session.getUser().checkAdmin();
122
// session.commit();
123
// backupTo("backup.zip");
124
// break;
125
// }
126
default:
127             throw Message.getInternalError("type=" + type);
128         }
129         return 0;
130     }
131
132     private void backupTo(String JavaDoc fileName) throws SQLException JavaDoc {
133 // int todoAddSpecialSQLStatement;
134
// FileOutputStream fileout = new FileOutputStream("test.zip");
135
// ZipOutputStream out = new ZipOutputStream(fileout);
136
// out.putNextEntry(new ZipEntry("test.data.db"));
137
// DiskFile file = session.getDatabase().getDataFile();
138
// session.getDatabase().getLog().incStopDeleteFiles(true);
139
// TODO Auto-generated method stub
140
// session.getDatabase().getLog().setStopDeleteFiles(false);
141
//
142
}
143
144     public boolean isTransactional() {
145         return true;
146     }
147
148     public boolean needRecompile() {
149         return false;
150     }
151
152     public void setTransactionName(String JavaDoc string) {
153         this.transactionName = string;
154     }
155
156 }
157
Popular Tags