Changeset 304
- Timestamp:
- 09/21/06 06:57:07
- Files:
-
- trunk/storage/__init__.py (modified) (1 diff)
- trunk/storage/db.py (modified) (1 diff)
- trunk/storage/geniusql.py (modified) (4 diffs)
- trunk/storage/storeado.py (modified) (1 diff)
- trunk/storage/storesqlite.py (modified) (1 diff)
- trunk/test/zoo_fixture.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/storage/__init__.py
r303 r304 95 95 raise NotImplementedError("%s has no rename_property method." 96 96 % self.__class__) 97 98 # Transactions # 99 100 start = None 101 rollback = None 102 commit = None 103 97 104 98 105 trunk/storage/db.py
r303 r304 716 716 717 717 return "\n".join(code) 718 719 718 719 def start(self): 720 """Start a transaction. Not needed if self.implicit is True.""" 721 self.db.start() 722 723 def rollback(self): 724 """Roll back the current transaction.""" 725 self.db.rollback() 726 727 def commit(self): 728 """Commit the current transaction.""" 729 self.db.commit() 730 trunk/storage/geniusql.py
r303 r304 1424 1424 1425 1425 def fetch(self, query, conn=None): 1426 """ fetch(query, conn=None) -> rowdata, columns.1426 """Return rowdata, columns (name, type) for the given query. 1427 1427 1428 1428 query should be a SQL query in string format … … 1457 1457 1458 1458 transaction_key = threading._get_ident 1459 1460 def get_transaction(self): 1461 """Return the connection for the current transaction, or a new one. 1462 1463 Uses the current thread id as a transaction key. 1459 implicit = False 1460 1461 def get_transaction(self, new=False): 1462 """Return the (possibly new) connection for the current transaction. 1463 1464 If self.implicit is True, a new connection will be associated with 1465 self.transaction_key(); repeated calls to this method will return 1466 the same connection. The default transaction key is the current 1467 thread ID. 1468 1469 If self.implicit is False, this method returns self.connection(), 1470 unless a transaction is already in progress. 1464 1471 """ 1465 1472 key = self.transaction_key() … … 1469 1476 raise conn 1470 1477 else: 1471 conn = self.transactions[key] = self.connection() 1472 self.start() 1478 conn = self.connection() 1479 if self.implicit or new: 1480 self.transactions[key] = conn 1481 if not new: 1482 self.start() 1473 1483 return conn 1474 1484 1475 1485 def start(self): 1476 """Start a transaction. """1477 self.execute("START TRANSACTION;", self.get_transaction( ))1486 """Start a transaction. Not needed if self.implicit is True.""" 1487 self.execute("START TRANSACTION;", self.get_transaction(new=True)) 1478 1488 1479 1489 def rollback(self): 1480 """Roll back the current transaction.""" 1481 self.execute("ROLLBACK;", self.get_transaction()) 1490 """Roll back the current transaction (error if not in transaction).""" 1482 1491 key = self.transaction_key() 1492 self.execute("ROLLBACK;", self.transactions[key]) 1483 1493 del self.transactions[key] 1484 1494 1485 1495 def commit(self): 1486 """Commit the current transaction.""" 1487 self.execute("COMMIT;", self.get_transaction()) 1496 """Commit the current transaction, if any.""" 1488 1497 key = self.transaction_key() 1489 del self.transactions[key] 1498 if key in self.transactions: 1499 self.execute("COMMIT;", self.transactions[key]) 1500 del self.transactions[key] 1490 1501 1491 1502 # Change this to 'error' if you don't want autocommit on schema ops. … … 1493 1504 1494 1505 def lock(self, msg=None): 1495 """Deny transactions (required for some operations).1506 """Deny transactions during schema operations (DDL statements). 1496 1507 1497 1508 Any code which calls this should also call 'unlock' in a try/finally: trunk/storage/storeado.py
r303 r304 513 513 def start(self): 514 514 """Start a transaction.""" 515 self.execute("BEGIN TRANSACTION;", self.get_transaction( ))515 self.execute("BEGIN TRANSACTION;", self.get_transaction(new=True)) 516 516 517 517 def execute(self, query, conn=None): trunk/storage/storesqlite.py
r303 r304 606 606 """Start a transaction.""" 607 607 self.acquire() 608 self.execute("BEGIN;", self.get_transaction()) 608 self.execute("BEGIN;", self.get_transaction(new=True)) 609 609 610 610 611 def rollback(self): 611 612 """Roll back the current transaction.""" 612 db.Database.rollback(self) 613 self.release() 613 key = self.transaction_key() 614 if key in self.transactions: 615 self.execute("ROLLBACK;", self.transactions[key]) 616 del self.transactions[key] 617 self.release() 614 618 615 619 def commit(self): 616 620 """Commit the current transaction.""" 617 db.Database.commit(self) 618 self.release() 621 key = self.transaction_key() 622 if key in self.transactions: 623 self.execute("COMMIT;", self.transactions[key]) 624 del self.transactions[key] 625 self.release() 619 626 620 627 def execute(self, query, conn=None): trunk/test/zoo_fixture.py
r303 r304 654 654 655 655 # Test transaction rollback 656 box = arena.new_sandbox() 657 SDZ = box.unit(Zoo, Name='San Diego Zoo') 658 SDZ.Name = 'The One and Only San Diego Zoo' 659 SDZ.Founded = datetime.date(2039, 9, 13) 660 box.rollback() 661 SDZ = box.unit(Zoo, Name='San Diego Zoo') 662 self.assertEqual(SDZ.Name, 'San Diego Zoo') 663 self.assertEqual(SDZ.Founded, datetime.date(1835, 9, 13)) 664 box.flush_all() 656 if arena.storage(Zoo).rollback: 657 box = arena.new_sandbox() 658 SDZ = box.unit(Zoo, Name='San Diego Zoo') 659 SDZ.Name = 'The One and Only San Diego Zoo' 660 SDZ.Founded = datetime.date(2039, 9, 13) 661 box.rollback() 662 SDZ = box.unit(Zoo, Name='San Diego Zoo') 663 self.assertEqual(SDZ.Name, 'San Diego Zoo') 664 self.assertEqual(SDZ.Founded, datetime.date(1835, 9, 13)) 665 box.flush_all() 665 666 666 667 def test_Multithreading(self):
