5

I just switched to Django 1.6 (with Posgres 9.1) and there is a few questions that I couldn't answer myself:

  1. (Answered) Is there a statement that prints/shows me if I am currently inside one or more and in which transaction.atomic blocks?

  2. (Answered) I neither have TransactionMiddleware enabled nor set ATOMIC_REQUESTS to True. So per default my code is not wrapped inside a transaction.atomic block, correct?

  3. Are sql statements that are executed via cursor committed properly when executed inside a transaction.atomic block? Is there another/better way to commit them?

    with transaction.atomic():
        cursor = connection.cursor()
        cursor.execute(sql)
    
  4. Do I need to wrap cursor.executemany() in a transaction.atomic block or does the execution already happen atomically?

  5. How can I see the current autocommit status? When is the status set, at the beginning of a connection, a transaction, as global database setting?

kev
  • 8,928
  • 14
  • 61
  • 103

1 Answers1

3

To answer one of your questions:

You can find out if you are currently in an atomic block using the connection object returned from:

from django.db.transaction import get_connection or any of the connection objects inside of django.db.connections

which has an in_atomic_block property.


I feel like its going to be easiest just to look through the django source

dm03514
  • 54,664
  • 18
  • 108
  • 145
  • `django.db.transaction.get_connection()` is "a private API" according to its docstring. I think you should use `from django.db import connections, DEFAULT_DB_ALIAS; connections[DEFAULT_DB_ALIAS].in_atomic_block` instead. – Rockallite Oct 07 '15 at 17:17