Incompleted transactions with PostgreSQL
See original GitHub issueHow to deal with incompleted transactions in PostgreSQL? It seems we can’t make queries after single transaction fail. Instead we get: psycopg2.InternalError: current transaction is aborted, commands ignored until end of transaction block
Here’s minimal code sample to reproduce the issue:
import peewee
db = peewee.PostgresqlDatabase(
database='test',
host='localhost',
port=None,
user='test',
password=None)
class User(peewee.Model):
username = peewee.CharField(max_length=50, unique=True)
class Meta:
database = db
User.create_table()
User.create(username='test')
So, I get single user with username “test”. If I try to create another one, I’ll get an error:
>>> User.create(username='test')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/test/.virtualenvs/test/lib/python3.3/site-packages/peewee.py", line 2193, in create
inst.save(force_insert=True)
File "/Users/test/.virtualenvs/test/lib/python3.3/site-packages/peewee.py", line 2278, in save
new_pk = insert.execute()
File "/Users/test/.virtualenvs/test/lib/python3.3/site-packages/peewee.py", line 1623, in execute
return self.database.last_insert_id(self._execute(), self.model_class)
File "/Users/test/.virtualenvs/test/lib/python3.3/site-packages/peewee.py", line 1314, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "/Users/test/.virtualenvs/test/lib/python3.3/site-packages/peewee.py", line 1721, in execute_sql
res = cursor.execute(sql, params or ())
psycopg2.IntegrityError: duplicate key value violates unique constraint "user_username"
DETAIL: Key (username)=(test) already exists.
And that’s predictable and fine! (By the way, how should we catch this kind of exception?)
But when I try to make another request, it fails:
>>> User.get(username='test')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/test/.virtualenvs/test/lib/python3.3/site-packages/peewee.py", line 2203, in get
return sq.get()
File "/Users/test/.virtualenvs/test/lib/python3.3/site-packages/peewee.py", line 1521, in get
return clone.execute().next()
File "/Users/test/.virtualenvs/test/lib/python3.3/site-packages/peewee.py", line 1556, in execute
self._qr = ResultWrapper(self.model_class, self._execute(), query_meta)
File "/Users/test/.virtualenvs/test/lib/python3.3/site-packages/peewee.py", line 1314, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "/Users/test/.virtualenvs/test/lib/python3.3/site-packages/peewee.py", line 1721, in execute_sql
res = cursor.execute(sql, params or ())
psycopg2.InternalError: current transaction is aborted, commands ignored until end of transaction block
And I need to drop database connection and create a new one to make it work again.
Issue Analytics
- State:
- Created 10 years ago
- Comments:19 (7 by maintainers)
Top Results From Across the Web
incomplete transaction keeps table locked? - PostgreSQL
Hi. I'm running PostgreSQL 7.0.2 on Linux 2.2.14, i686. Are there any circumstances when exiting a PHP web script
Read more >Statistical probability of an incomplete transaction committed ...
It seems like well-written database software like Postgres are designed to ensure that a transaction will never commit incomplete data.
Read more >How to check for pending operations in a PostgreSQL ...
If the transaction id returned by the two selects is equal, then there is an open transaction. If not then there wasn't, (but...
Read more >Incomplete transaction may hold large number of locks and ...
When a transaction is not completed either because a query times out or because the batch is cancelled in the middle of a...
Read more >Transaction Isolation in PostgreSQL - pgDash
What happens when one (unfinished) transaction inserts rows in a table and the other (also unfinished) transaction tries to read all rows in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Added fbd6c20aa3fa75b1a92b74030f201d0e43304e28 which adds a new parameter to the
Database
classautorollback
:As a new user of peewee (which is great - thanks for creating & maintaining it!), I’m very much in favor of changing the default; the current behavior was confusing to me when I switched my underlying database from sqlite to postgres and started getting errors about open transactions that had not existed with sqlite. Of course, I don’t have tons of code to maintain that uses peewee that would be negatively affected by this change.
Regardless, I’d argue that - semantically speaking -
autocommit
’s current behavior is broken.