def test_creates_an_atomic(self): assert connection.get_autocommit() == 1 assert not connection.in_atomic_block with TableLock(read=[Alphabet]): assert connection.get_autocommit() == 0 assert connection.in_atomic_block assert connection.get_autocommit() == 1 assert not connection.in_atomic_block
def __enter__(self): connection = transaction.get_connection(self.using) if not connection.in_atomic_block: # Reset state when entering an outermost atomic block. connection.commit_on_exit = True connection.needs_rollback = False if not connection.get_autocommit(): # Pretend we're already in an atomic block to bypass the code # that disables autocommit to enter a transaction, and make a # note to deal with this case in __exit__. connection.in_atomic_block = True connection.commit_on_exit = False if connection.in_atomic_block: # We're already in a transaction; create a savepoint, unless we # were told not to or we're already waiting for a rollback. The # second condition avoids creating useless savepoints and prevents # overwriting needs_rollback until the rollback is performed. if self.savepoint and not connection.needs_rollback: sid = connection.savepoint() connection.savepoint_ids.append(sid) else: connection.savepoint_ids.append(None) else: if self.immediate: connection.set_autocommit(False) connection.cursor().execute('BEGIN IMMEDIATE') else: connection.set_autocommit( False, force_begin_transaction_with_broken_autocommit=True) connection.in_atomic_block = True
def events(self, select_timeout=5, yield_timeouts=False): if not pg_connection.get_autocommit(): raise RuntimeError('Listening for events can only be done in autocommit mode') while True: if select.select([self.conn], [], [], select_timeout) == NOT_READY: if yield_timeouts: yield None else: self.conn.poll() while self.conn.notifies: yield self.conn.notifies.pop(0)
def get_transaction_start_ts(): if connection.get_autocommit(): return to_decimal(time.time()) assert local.tran_ts is not None, 'This sould not be None...' return local.tran_ts
def forwards(self, orm): # On SQLite, database transactions (which are used by # `Document.objects.sync`) requires autocommit to be turned on. South # however doesn't enable this by default. if connection.vendor == 'sqlite': autocommit = connection.get_autocommit() connection.set_autocommit(True) # Adding model 'Document' if 'desktop_document' not in connection.introspection.table_names(): db.create_table('desktop_document', ( ('description', self.gf('django.db.models.fields.TextField')(default='')), ('extra', self.gf('django.db.models.fields.TextField')(default='')), ('object_id', self.gf('django.db.models.fields.PositiveIntegerField')()), ('last_modified', self.gf('django.db.models.fields.DateTimeField')(auto_now=True, db_index=True, blank=True)), ('content_type', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['contenttypes.ContentType'])), ('version', self.gf('django.db.models.fields.SmallIntegerField')(default=1)), ('owner', self.gf('django.db.models.fields.related.ForeignKey')(related_name='doc_owner', to=orm['auth.User'])), ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('name', self.gf('django.db.models.fields.TextField')(default='')), )) db.send_create_signal('desktop', ['Document']) # Adding model 'DocumentPermission' if 'desktop_documentpermission' not in connection.introspection.table_names(): db.create_table('desktop_documentpermission', ( ('perms', self.gf('django.db.models.fields.TextField')(default='read')), ('doc', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['desktop.Document'])), ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), )) db.send_create_signal('desktop', ['DocumentPermission']) # Adding M2M table for field users on 'DocumentPermission' if 'documentpermission_users' not in connection.introspection.table_names(): db.create_table('documentpermission_users', ( ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), ('documentpermission', models.ForeignKey(orm['desktop.documentpermission'], null=False)), ('user', models.ForeignKey(orm['auth.user'], null=False)) )) db.create_unique('documentpermission_users', ['documentpermission_id', 'user_id']) # Adding M2M table for field groups on 'DocumentPermission' if 'documentpermission_groups' not in connection.introspection.table_names(): db.create_table('documentpermission_groups', ( ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), ('documentpermission', models.ForeignKey(orm['desktop.documentpermission'], null=False)), ('group', models.ForeignKey(orm['auth.group'], null=False)) )) db.create_unique('documentpermission_groups', ['documentpermission_id', 'group_id']) # Adding model 'DocumentTag' if 'desktop_documenttag' not in connection.introspection.table_names(): db.create_table('desktop_documenttag', ( ('owner', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'])), ('tag', self.gf('django.db.models.fields.SlugField')(max_length=50, db_index=True)), ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), )) db.send_create_signal('desktop', ['DocumentTag']) # Adding M2M table for field tags on 'Document' if 'desktop_document_tags' not in connection.introspection.table_names(): db.create_table('desktop_document_tags', ( ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), ('document', models.ForeignKey(orm['desktop.document'], null=False)), ('documenttag', models.ForeignKey(orm['desktop.documenttag'], null=False)) )) db.create_unique('desktop_document_tags', ['document_id', 'documenttag_id']) if not db.dry_run: Document.objects.sync() if connection.vendor == 'sqlite': connection.set_autocommit(autocommit)