question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

why does query invoke a transaction

See original GitHub issue

mysqlclient==1.4.2.post1 SQLAlchemy==1.3.5

def test4():
    Session = db.session  # db.session = sessionmaker(bind=self.engine, **session_options)

    print("start")
    print(db.status())
    conn = db.engine.connect()
    print(db.status())
    print("after check out one connection")

    session = Session(bind=conn)
    new_user = User()
    new_user.id = 3
    print("begin the transaction")

    session.query(User).filter_by(id=1).first()


    # session.rollback() # uncomment this line. everything works fine
    print("invalidate the connection. mock the connection not available")
    conn.invalidate()

    print(db.status())
    session.query(User).filter_by(id=1).first()

    print(db.status())

this will generate exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1177, in _execute_context
    conn = self._revalidate_connection()
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 463, in _revalidate_connection
    "Can't reconnect until invalid "
sqlalchemy.exc.InvalidRequestError: Can't reconnect until invalid transaction is rolled back

After this, this connection is no longer usable anymore.

My question is : Why does query invoke a transaction? Normally speaking, if there is no transaction, conn.invalidate() won’t cause any problems. It just close the connection and return it back to pools, right? For example, if add session.rollback() right after query, everything works just fine.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
chenyuan53618commented, Jan 17, 2020

Thx, @zzzeek. I just became your patron. Really worth all detailed answers

0reactions
zzzeekcommented, Jan 30, 2020

closing this for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chaincode Invoke and Query. Overview | by KC Tam - Medium
Chaincode function invoke is a transaction reflecting a real business transaction. A quick example is that Alice sends Bob 100 shares.
Read more >
Understanding SQL Server query execution and transactions
Since SQL Server default transaction mode is Autocommit Transactions, each individual statement is a transaction.
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 >
Transactions - Snowflake Documentation
The term “query statement” refers to SELECT and CALL statements. Although a CALL statement (which calls a stored procedure) is a single statement,...
Read more >
How to use transactions in Microsoft Access VBA - Codekabinett
The implementation is fairly simple. Before you execute the first action query, you instruct the DBEngine to start a transaction using the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found