當前位置: 首頁>>代碼示例>>Python>>正文


Python DatabaseManager.get_connection方法代碼示例

本文整理匯總了Python中trac.db.api.DatabaseManager.get_connection方法的典型用法代碼示例。如果您正苦於以下問題:Python DatabaseManager.get_connection方法的具體用法?Python DatabaseManager.get_connection怎麽用?Python DatabaseManager.get_connection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在trac.db.api.DatabaseManager的用法示例。


在下文中一共展示了DatabaseManager.get_connection方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: trac_db_transaction

# 需要導入模塊: from trac.db.api import DatabaseManager [as 別名]
# 或者: from trac.db.api.DatabaseManager import get_connection [as 別名]
def trac_db_transaction(env):
    """
    Context manager to handle database connection and cursor from trac environment's
    read / write connection. This handles automatic commits, rollbacks and connection
    closing.

    Internally, this uses Trac's connection pool via the Trac DatabaseManager class.
    This is because it would appear that to multiproject uses, Trac's with_transaction()
    is not sufficient.

    For examples of use, see db_transaction()

    :param Environment env: The Trac Environment in use.
    """
    conn = None
    cursor = None

    try:
        dm = DatabaseManager(env)
        conn = dm.get_connection()
    except Exception:
        env.log.exception("Failed to get database connection from trac database manager")
        raise

    try:
        cursor = conn.cursor()
        yield cursor
        conn.commit()
    except Exception:
        env.log.error("Failed to perform transaction to database")
        conn.rollback()
        raise
    finally:
        cursor.close()
        conn.close()
開發者ID:alvabai,項目名稱:trac-multiproject,代碼行數:37,代碼來源:db.py

示例2: trac_db_query

# 需要導入模塊: from trac.db.api import DatabaseManager [as 別名]
# 或者: from trac.db.api.DatabaseManager import get_connection [as 別名]
def trac_db_query(env):
    """
    Context manager to handle database connection and cursor from trac environment's
    read only connection. This does not attempt to roll back or commit, the connection
    is meant only for accessing the data. For examples on use, see db_query().

    Internally, this uses Trac's connection pool via the Trac DatabaseManager class

    :param Environment env: The Trac environment
    """
    conn = None
    cursor = None

    try:
        dm = DatabaseManager(env)
        conn = dm.get_connection()
    except Exception:
        env.log.exception("Failed to get database connection from trac database manager")
        raise

    try:
        # NOTE: Trac's connection does not support alternative cursor types!
        cursor = conn.cursor()
        yield cursor
    except Exception:
        env.log.error("Failed to query database")
        raise
    finally:
        cursor.close()
        conn.close()
開發者ID:alvabai,項目名稱:trac-multiproject,代碼行數:32,代碼來源:db.py

示例3: _utf8_size

# 需要導入模塊: from trac.db.api import DatabaseManager [as 別名]
# 或者: from trac.db.api.DatabaseManager import get_connection [as 別名]
 def _utf8_size(self, cnx):
     if cnx is None:
         connector, args = DatabaseManager(self.env).get_connector()
         cnx = connector.get_connection(**args)
         charset = cnx.charset
         cnx.close()
     else:
         charset = cnx.charset
     return 4 if charset == 'utf8mb4' else 3
開發者ID:exocad,項目名稱:exotrac,代碼行數:11,代碼來源:mysql_backend.py


注:本文中的trac.db.api.DatabaseManager.get_connection方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。