SQL transactions can be executed this way:
{{{#!python
from trac.env import Environment
def myFunc():
env = Environment('/path/to/projenv')
with env.db_transaction as db:
cursor = db.cursor()
# Execute some SQL statements
}}}
When the `with` statement is exited normally, the transaction gets committed. Note that `myFunc()` can itself be called in such a `db_transaction` context. In that case, no commit will happen in myFunc, it's the outermost context which will take care of committing the transaction. If an exception gets raised, the `db_transaction` context manager will roll back the transaction.
Another context manager for performing "read-only" queries can be obtained via `env.db_query`. That one doesn't `commit` or `rollback`, but returns the connection to the pool when exiting the context, making that connection available to other threads as soon as possible.
See TracDev/DatabaseApi#Trac0.13API for more details.
Part of #9536.