1 5 package org.h2.command.dml; 6 7 import java.io.FileNotFoundException ; 8 import java.io.FileOutputStream ; 9 import java.sql.SQLException ; 10 import java.util.zip.ZipEntry ; 11 import java.util.zip.ZipFile ; 12 import java.util.zip.ZipOutputStream ; 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 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 savepointName; 41 private String transactionName; 42 43 public TransactionCommand(Session session, int type) { 44 super(session); 45 this.type = type; 46 } 47 48 public void setSavepointName(String name) { 49 this.savepointName = name; 50 } 51 52 public int update() throws SQLException { 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 e) { 97 } 99 break; 100 case SHUTDOWN: { 101 session.getUser().checkAdmin(); 102 session.commit(); 103 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 default: 127 throw Message.getInternalError("type=" + type); 128 } 129 return 0; 130 } 131 132 private void backupTo(String fileName) throws SQLException { 133 } 143 144 public boolean isTransactional() { 145 return true; 146 } 147 148 public boolean needRecompile() { 149 return false; 150 } 151 152 public void setTransactionName(String string) { 153 this.transactionName = string; 154 } 155 156 } 157 | Popular Tags |