Changes between Version 11 and Version 12 of TracDev/DatabaseApi
- Timestamp:
- Sep 15, 2010, 7:32:50 PM (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracDev/DatabaseApi
v11 v12 11 11 12 12 == Accessing the Database == 13 === API before Trac 0.12 13 14 14 15 Code 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. … … 30 31 (avoid in particular doing `cursor = env.get_db_cnx().cursor()`, see r8878). 31 32 33 === Trac 0.12 API 34 35 {{{ 36 #!python 37 from trac.env import Environment 38 def 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 48 This 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 49 place), but a `rollback` will be performed should an exception be triggered in that block. 50 51 See more details in the [../ApiChanges/0.12#with_transaction] paragraph. 52 53 === Configuration 32 54 The {{{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: 33 55 … … 41 63 === Pooled Connections === 42 64 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}}}65 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, 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}}} 44 66 45 67 == Rules for DB API Usage ==