Insertion in transaction presisted after rollback? #881
-
Hello all, While investigating an issue in Databases I couldn't understand a problem regarding rolling back transactions when two connections are present. For example: import asyncio
import logging
import asyncpg
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
DB_DSN = "postgresql://0.0.0.0:5432/databases_test"
async def truncate(db, expect=None):
logger.info("Truncating")
async with db.acquire() as conn:
logger.info("%s", conn)
await conn.execute("TRUNCATE foo")
async def insert(db):
logger.info("Inserting")
async with db.acquire() as conn:
logger.info("%s", conn)
async with conn.transaction():
logger.info("Inside transaction context: inserting")
await db.execute("INSERT INTO foo VALUES (2)")
raise Exception
# this somehow survives?!
async def select(db, expect=None):
logger.info("Selecting")
async with db.acquire() as conn:
logger.info("%s", conn)
result = await conn.execute("SELECT * FROM foo")
assert result == expect, result
async def main():
db = await asyncpg.create_pool(DB_DSN)
await truncate(db)
try:
await insert(db)
except Exception:
pass
await select(db)
if __name__ == "__main__":
asyncio.run(main()) After running this script I expected the table I couldn't find any issues mentioning this behaviour, hopefully someone can explain why this is? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This should be |
Beta Was this translation helpful? Give feedback.
This should be
conn.execute
, notdb.execute
. You started a transaction on a different connection.