1 33 package smallsql.database; 34 35 import java.io.RandomAccessFile ; 36 import java.sql.*; 37 import java.util.Map ; 38 import java.util.List ; 39 import java.util.ArrayList ; 40 import java.util.Properties ; 41 42 43 public class SSConnection implements Connection { 44 45 private Database database; 46 private boolean autoCommit = true; 47 int isolationLevel = TRANSACTION_READ_COMMITTED; int serializeCount; 49 private List commitPages = new ArrayList (); 50 51 private long transactionTime; 52 private SSDatabaseMetaData metadata; 53 private int holdability; 54 final Logger log = new Logger(); 55 56 SSConnection( Properties props ) throws SQLException{ 57 String name = props.getProperty("dbpath"); 58 boolean create = "true".equals(props.getProperty("create")); 59 database = Database.getDatabase(name, this, create); 60 metadata = new SSDatabaseMetaData(this); 61 } 62 63 64 68 Database getDatabase(boolean returnNull) throws SQLException{ 69 if(!returnNull && database == null) throw Utils.createSQLException("You are not connected with a Database."); 70 return database; 71 } 72 73 public Statement createStatement() throws SQLException { 74 return new SSStatement(this); 75 } 76 public PreparedStatement prepareStatement(String sql) throws SQLException { 77 return new SSPreparedStatement( this, sql); 78 } 79 public CallableStatement prepareCall(String sql) throws SQLException { 80 return new SSCallableStatement( this, sql); 81 } 82 83 84 public String nativeSQL(String sql){ 85 return sql; 86 } 87 88 89 public void setAutoCommit(boolean autoCommit) throws SQLException { 90 if(log.isLogging()) log.println("AutoCommit:"+autoCommit); 91 if(this.autoCommit != autoCommit){ 92 commit(); 93 this.autoCommit = autoCommit; 94 } 95 } 96 97 98 public boolean getAutoCommit(){ 99 return autoCommit; 100 } 101 102 103 106 void add(StorePage storePage) throws SQLException{ 107 testClosedConnection(); 108 commitPages.add(storePage); 109 } 110 111 112 public void commit() throws SQLException { 113 try{ 114 log.println("Commit"); 115 testClosedConnection(); 116 synchronized(commitPages){ 117 int count = commitPages.size(); 118 for(int i=0; i<count; i++){ 119 StorePage page = (StorePage)commitPages.get(i); 120 page.commit(); 121 } 122 for(int i=0; i<count; i++){ 123 StorePage page = (StorePage)commitPages.get(i); 124 page.freeLock(); 125 } 126 commitPages.clear(); 127 transactionTime = System.currentTimeMillis(); 128 } 129 }catch(Throwable e){ 130 rollback(); 131 throw Utils.createSQLException(e); 132 } 133 } 134 135 136 139 void rollbackFile(RandomAccessFile raFile) throws SQLException{ 140 testClosedConnection(); 141 for(int i=commitPages.size()-1; i>=0; i--){ 143 StorePage page = (StorePage)commitPages.get(i); 144 if(page.raFile == raFile){ 145 page.rollback(); 146 page.freeLock(); 147 } 148 } 149 } 150 151 152 void rollback(int savepoint) throws SQLException{ 153 testClosedConnection(); 154 for(int i = commitPages.size()-1; i>=savepoint; i--){ 155 StorePage page = (StorePage)commitPages.remove(i); 156 page.rollback(); 157 page.freeLock(); 158 } 159 } 160 161 162 public void rollback() throws SQLException { 163 log.println("Rollback"); 164 testClosedConnection(); 165 synchronized(commitPages){ 166 int count = commitPages.size(); 167 for(int i=0; i<count; i++){ 168 StorePage page = (StorePage)commitPages.get(i); 169 page.rollback(); 170 page.freeLock(); 171 } 172 commitPages.clear(); 173 transactionTime = System.currentTimeMillis(); 174 } 175 } 176 177 178 public void close() throws SQLException { 179 rollback(); 180 database = null; 181 commitPages = null; 182 Database.closeConnection(this); 183 } 184 185 186 final void testClosedConnection() throws SQLException{ 187 if(isClosed()) throw Utils.createSQLException("Connection was closed."); 188 } 189 190 public boolean isClosed(){ 191 return (commitPages == null); 192 } 193 194 195 public DatabaseMetaData getMetaData(){ 196 return metadata; 197 } 198 199 200 public void setReadOnly(boolean readOnly){ 201 } 203 204 205 public boolean isReadOnly(){ 206 return false; 207 } 208 209 210 public void setCatalog(String catalog) throws SQLException { 211 if(isClosed()) throw Utils.createSQLException("Connection is already closed"); 212 database = Database.getDatabase(catalog, this, false); 213 } 214 215 216 public String getCatalog(){ 217 if(database == null) 218 return ""; 219 return database.getName(); 220 } 221 222 223 public void setTransactionIsolation(int level) throws SQLException { 224 if(!metadata.supportsTransactionIsolationLevel(level)) 225 throw Utils.createSQLException("Unknown Transaction Isolation Level:"+level); 226 isolationLevel = level; 227 } 228 229 230 public int getTransactionIsolation(){ 231 return isolationLevel; 232 } 233 234 235 public SQLWarning getWarnings(){ 236 return null; 237 } 238 239 240 public void clearWarnings(){ 241 } 243 244 245 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 246 return new SSStatement( this, resultSetType, resultSetConcurrency); 247 } 248 249 250 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 251 return new SSPreparedStatement( this, sql, resultSetType, resultSetConcurrency); 252 } 253 254 255 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 256 return new SSCallableStatement( this, sql, resultSetType, resultSetConcurrency); 257 } 258 259 260 public Map getTypeMap(){ 261 return null; 262 } 263 264 265 public void setTypeMap(Map map){ 266 } 268 269 270 public void setHoldability(int holdability){ 271 this.holdability = holdability; 272 } 273 274 275 public int getHoldability(){ 276 return holdability; 277 } 278 279 280 int getSavepoint() throws SQLException{ 281 testClosedConnection(); 282 return commitPages.size(); 283 } 284 285 286 public Savepoint setSavepoint() throws SQLException { 287 return new SSSavepoint(getSavepoint(), null, transactionTime); 288 } 289 290 291 public Savepoint setSavepoint(String name) throws SQLException { 292 return new SSSavepoint(getSavepoint(), name, transactionTime); 293 } 294 295 296 public void rollback(Savepoint savepoint) throws SQLException { 297 if(savepoint instanceof SSSavepoint){ 298 if(((SSSavepoint)savepoint).transactionTime != transactionTime){ 299 throw Utils.createSQLException("Savepoint is not valid for this transaction."); 300 } 301 rollback( savepoint.getSavepointId() ); 302 return; 303 } 304 throw Utils.createSQLException("Savepoint is not valid for this driver."+savepoint); 305 } 306 307 308 public void releaseSavepoint(Savepoint savepoint) throws SQLException { 309 if(savepoint instanceof SSSavepoint){ 310 ((SSSavepoint)savepoint).transactionTime = 0; 311 return; 312 } 313 throw Utils.createSQLException("Savepoint is not valid for this driver."+savepoint); 314 } 315 316 317 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 318 return new SSStatement( this, resultSetType, resultSetConcurrency); 320 } 321 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 322 return new SSPreparedStatement( this, sql); 324 } 325 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 326 return new SSCallableStatement( this, sql, resultSetType, resultSetConcurrency); 328 } 329 330 331 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { 332 SSPreparedStatement pr = new SSPreparedStatement( this, sql); 333 pr.setNeedGeneratedKeys(autoGeneratedKeys); 334 return pr; 335 } 336 337 338 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { 339 SSPreparedStatement pr = new SSPreparedStatement( this, sql); 340 pr.setNeedGeneratedKeys(columnIndexes); 341 return pr; 342 } 343 344 345 public PreparedStatement prepareStatement(String sql, String [] columnNames) throws SQLException { 346 SSPreparedStatement pr = new SSPreparedStatement( this, sql); 347 pr.setNeedGeneratedKeys(columnNames); 348 return pr; 349 } 350 } | Popular Tags |