JUG Master DB Module
The master uses a MySQL database to keep track of the state of
everything. InnoDB tables are used for high concurrency and robust
transactional support. This module is used to run queries and make
updates to the database.
For documentation on how to use the cursor object, see
Python Database API 2.0.
The database schema describes the tables and
columns in the database.
For multi-threaded applications, such as JugMasterRPC, the DB
module manages a pool of connection objects. Each thread should
lock/unlock a connection by calling AllocateConnectionForThread()
and ReleaseConnection(). Otherwise, when a thread finishes,
the automatically allocated connection will remain open and consume
a slot in the connection pool, which may fill up and block
creation of additional database connections.
Functions
- Query(SQL="")
- Return a cursor object suitable for reading from the database.
If SQL is provided, call cursor.execute(SQL).
- Update(SQL="")
- Return a cursor object suitable for writing to the database.
If SQL is provided, call cursor.execute(SQL).
- Update_AutoRetryOnDeadlock(SQL)
- Execute given SQL and retry until the operation succeeds
whenever a deadlock condition happens, which is a case that
should be expected whenever using InnoDB tables. Do not
use this in the middle of a multi-step transaction. In that case,
you need to catch DB.DeadlockError yourself and retry the whole
transaction.
- Quote(str)
- Return a quoted SQL string value, with any special characters escaped.
- AllocateConnectionForThread()
- In a multi-threaded application with threads that are dynamically
allocated and deallocated, each thread that uses the database
should call this before accessing the database and then
ReleaseConnection() to release its lock on the connection and
allow another thread to use it.
- ReleaseConnection(c)
- Release this thread's claim to a connection returned by
AllocateConnectionForThread().
Example 1
import JugMaster.DB as DB
qry = DB.Query("SELECT * FROM table")
row = qry.fetchone()
Example 2
import JugMaster.DB as DB
qry = DB.Update("UPDATE table SET x = 3 WHERE y = 1")
Example 3
import JugMaster.DB as DB
DB.Update("BEGIN")
q = DB.Query("SELECT * FROM table FOR UPDATE")
...
DB.Update("COMMIT")