I have an issue setting transaction isolation level. I want the most strict serializable, while the default is read committed.
I am using:
Django==1.10.6
psycopg2==2.5.1
Running on heroku.
Based on the documentation:
https://docs.djangoproject.com/en/1.10/ref/databases/#isolation-level
I have the following settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'OPTIONS': {
'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
}
}
if on_heroku:
DATABASES['default'] = dj_database_url.config()
Here's the view code:
#require_http_methods(["POST"])
#transaction.atomic
#login_required()
def api_test_add_one(request):
cursor = connection.cursor()
cursor.execute('SHOW default_transaction_isolation')
logger.info("aa: " + str(cursor.fetchone()))
return HttpResponse("{}", content_type="application/json")
The output is:
aa: (u'read committed',)
I have run different tests simultaneously accessing the same endpoint, incrementing an integer in DB and confirmed that transactions were not isolated.
The OPTIONS belongs inside the database's settings, e.g.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgres',
...
'OPTIONS': {
'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
},
},
}
In your case, you replace DATABASES['default'], so your OPTIONS set there would be lost:
if on_heroku:
DATABASES['default'] = dj_database_url.config()
Instead, you can set OPTIONS after setting DATABASES['default'].
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
}
if on_heroku:
DATABASES['default'] = dj_database_url.config()
DATABASES['default']['OPTIONS'] = {
'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
}
Related
I have multiple databases defined. This is for test profile, and I want to be able to specify which database to be picked for testing. eg: "python manage.py test -db=mysql"
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'mysql': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysql_test',
}
}
I went through the django documentation, but i cant find a clear cut way of doing it. One way of getting around this is setting up environment variables, and define both databases as default. Then use the db based on the database type.
please let me know if there is a much better way of doing this.
thanks
Amal
You can use the TEST attribute of DATABASES:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'TEST': {
'NAME' : 'mysql'
},
},
'mysql': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysql_test',
}
}
but is there a reason you don't want to use the default test database django creates?
I am getting the following error:
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE VALUE.
My settings.py looks like:
DATABASES = {
'default': {
},
'postgres': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'app',
'USER':'user',
'HOST': 'localhost',
},
'cassandra': {
'ENGINE': 'django_cassandra_engine',
'NAME': 'app',
'HOST': '127.0.0.1',
'OPTIONS': {
'replication': {
'strategy_class': 'SimpleStrategy',
'replication_factor': 1
},
'connection': {
'consistency': ConsistencyLevel.LOCAL_ONE,
'port': 9042,
'retry_connect': True
# + All connection options for cassandra.cluster.Cluster()
},
'session': {
'default_timeout': 10,
'default_fetch_size': 10000
# + All options for cassandra.cluster.Session()
}
}
}
}
runserver, syncdb are working fine. The error pops up when I access the index page.
Is there something I am doing wrong?
Thanks in advance :)
/Saha
The DATABASES setting must configure a default database; any number of additional databases may also be specified.
You cannot have empty default setting.
This is from the Django docs. Django settings documentations
What I'am trying to do is to use 2 databases in my django app. One is to be accessed from a remote server. Django settings has something like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'snackvoxadmin'
},
'users': {
.....
}
}
The database user has a url like similar to this one: postgres://a78adj1he81....
You can decompose your database url and configure it like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
And the pattern for a database url is :
postgres://user:password#host:post/database
https://docs.djangoproject.com/en/1.8/ref/settings/#databases
Or you can use the package dj-database-url to directly use the database url.
E.g. from readme :
import dj_database_url
DATABASES = {'default': dj_database_url.parse('postgres://...')}
That URL presumably consists of a username, a password, and a host name/IP address. You could split them up yourself or use the dj-database-url library.
I face a problem that I design a information system base on django-admin.
But I have several independent system to build.every system's Features are all the same.
So I want to put a texbox to select to switch my database before I login in admin.How can I do that?
for example ,when I want to login in admin,I want to freely switch from db1 to db2 or reverse.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db1.sqlite3'),
},
'newData': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db2.sqlite3'),
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
How can I change the name of the database properly ?