Edgewall Software

Changes between Version 11 and Version 12 of TracDev/DatabaseApi


Ignore:
Timestamp:
Sep 15, 2010, 7:32:50 PM (14 years ago)
Author:
Christian Boos
Comment:

present briefly the 0.12 API (@with_transaction)

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/DatabaseApi

    v11 v12  
    1111
    1212== Accessing the Database ==
     13=== API before Trac 0.12
    1314
    1415Code accessing the database in Trac go through this layer simply by using the {{{Environment}}} method {{{get_db_cnx()}}} in order to get a pooled database connection.  A database cursor can be obtained from the pooled database connection, and the code uses the cursor in conjunction with SQL statements to implement the behavior. 
     
    3031(avoid in particular doing `cursor = env.get_db_cnx().cursor()`, see r8878).
    3132
     33=== Trac 0.12 API
     34
     35{{{
     36#!python
     37from trac.env import Environment
     38def myFunc():
     39    env = Environment('/path/to/projenv')
     40    @with_transaction(env)
     41    def do_something(db):
     42        cursor = db.cursor()
     43        # Execute some SQL statements
     44
     45    return
     46}}}
     47
     48This is slightly simpler and more robust than the previous API, as not only the `commit()` is performed in any successful case (i.e. the `Execute some SQL statements` code can `return` at any
     49place), but a `rollback` will be performed should an exception be triggered in that block.
     50
     51See more details in the [../ApiChanges/0.12#with_transaction] paragraph.
     52
     53=== Configuration
    3254The {{{get_db_cnx}}} method looks at the value of the {{{database}}} configuration option in [wiki:TracIni trac.ini], which should contain a database connection URI. The default value for this option tells Trac to use an SQLite database inside the environment directory:
    3355
     
    4163=== Pooled Connections ===
    4264
    43 The {{{Environment}}} method {{{get_db_cnx()}}} returns a connection from the pool of connections.  This connection needs to be returned, and Trac is written so that the return will happen automatically by the garbage collector if the code is written to follow a simple rule.  When the garbage collector determines the pooled database connection is no longer being used, it's __del__ method will return the pooled connection to the pool for reuse.  If you have set a lexical variable in the function's body to the pooled connection, this typically occurs when the function is returning.  In the example above of {{{myFunc}}} it occurs at the return statement since {{{db}}} is a variable local to {{{myFunc}}}
     65The {{{Environment}}} method {{{get_db_cnx()}}} returns a connection from the pool of connections.  This connection needs to be returned, and Trac is written so that the return will happen automatically by the garbage collector if the code is written to follow a simple rule.  When the garbage collector determines the pooled database connection is no longer being used, its `__del__` method will return the pooled connection to the pool for reuse.  If you have set a lexical variable in the function's body to the pooled connection, this typically occurs when the function is returning.  In the example above of {{{myFunc}}} it occurs at the return statement since {{{db}}} is a variable local to {{{myFunc}}}
    4466
    4567== Rules for DB API Usage ==