1 5 package org.h2.command; 6 7 import java.sql.SQLException ; 8 9 import org.h2.engine.Constants; 10 import org.h2.engine.Database; 11 import org.h2.engine.Session; 12 import org.h2.message.Message; 13 import org.h2.message.Trace; 14 import org.h2.result.LocalResult; 15 import org.h2.result.ResultInterface; 16 import org.h2.util.ObjectArray; 17 18 21 public abstract class Command implements CommandInterface { 22 protected Session session; 23 protected long startTime; 24 protected Trace trace; 25 private volatile boolean cancel; 26 27 public abstract boolean isTransactional(); 28 public abstract boolean isQuery(); 29 public abstract ObjectArray getParameters(); 30 public abstract boolean isReadOnly(); 31 32 public Command(Parser parser) { 33 this.session = parser.getSession(); 34 trace = session.getDatabase().getTrace(Trace.COMMAND); 35 } 36 37 public int update() throws SQLException { 38 throw Message.getSQLException(Message.METHOD_NOT_ALLOWED_FOR_QUERY); 39 } 40 41 public LocalResult query(int maxrows) throws SQLException { 42 throw Message.getSQLException(Message.METHOD_ONLY_ALLOWED_FOR_QUERY); 43 } 44 45 60 public ResultInterface executeQuery(int maxrows, boolean scrollable) throws SQLException { 61 return executeQueryLocal(maxrows); 62 } 63 64 public LocalResult executeQueryLocal(int maxrows) throws SQLException { 65 startTime = System.currentTimeMillis(); 66 Database database = session.getDatabase(); 67 Object sync = Constants.MULTI_THREADED_KERNEL ? (Object )session : (Object )database; 68 synchronized (sync) { 69 try { 70 database.checkPowerOff(); 71 session.setCurrentCommand(this); 72 LocalResult result = query(maxrows); 73 return result; 74 } catch(Throwable e) { 75 SQLException s = Message.convert(e); 76 database.exceptionThrown(s); 77 throw s; 78 } finally { 79 stop(); 80 } 81 } 82 } 83 84 protected void start() { 85 startTime = System.currentTimeMillis(); 86 } 87 88 public void checkCancelled() throws SQLException { 89 if (cancel) { 90 throw Message.getSQLException(Message.STATEMENT_WAS_CANCELLED); 91 } 92 session.throttle(); 93 } 94 95 private void stop() throws SQLException { 96 session.setCurrentCommand(null); 97 if (!isTransactional()) { 98 session.commit(); 100 } else if (session.getAutoCommit()) { 101 session.commit(); 102 } 103 if (trace.info()) { 104 long time = System.currentTimeMillis() - startTime; 105 if (time > Constants.LONG_QUERY_LIMIT_MS) { 106 trace.info("long query: " + time); 107 } 108 } 109 } 110 111 public int executeUpdate() throws SQLException { 112 startTime = System.currentTimeMillis(); 113 Database database = session.getDatabase(); 114 Object sync = Constants.MULTI_THREADED_KERNEL ? (Object )session : (Object )database; 115 synchronized (sync) { 116 int rollback = session.getLogId(); 117 session.setCurrentCommand(this); 118 try { 119 database.checkPowerOff(); 120 int result = update(); 121 return result; 122 } catch (SQLException e) { 123 database.exceptionThrown(e); 124 database.checkPowerOff(); 125 session.rollbackTo(rollback); 126 throw e; 127 } finally { 128 stop(); 129 } 130 } 131 } 132 133 public void close() { 134 } 136 137 public void cancel() { 138 this.cancel = true; 139 } 140 141 } 142 | Popular Tags |