| Path: | recipes.txt |
| Last Update: | Wed May 26 02:35:36 GMT Daylight Time 2004 |
You probably want a singleton or global database handle. Something like:
module Highcrest
module Persistence
def dbh()
$dbh ||= DBI.connect 'DBI:OCI8:', 'scott', 'tiger'
end
end
end
Associate the connection with the current thread. Use a thread-local variable or associate the handle with another accessible object such as Apache::Request.
module Apache
class Request
def dbh()
@dbh ||= DBI.connect 'DBI:OCI8:', 'scott', 'tiger'
end
def cleanup()
@dbh.disconnect if @dbh
end
end
end
module Highcrest
module Persistence
def dbh()
Apache::Request.dbh
end
end
end
Unless you use auto-commit, use a request completion event to trigger commits, and the request error-handler to trigger rollback.
You probably also want to establish a connection pool, rather than reconnecting in every request that uses the database.
FIXME
To include an object that is automatically persisted along with this one, and which is automatically deleted if this one is deleted:
FIXME
Assuming that we send the old values to the client-tier, and it returns back both old values and updated values in the middle tier:
Something like:
object = Object.read old_key_value
populate object, all_old_values
object.reset_optimistic_cache!
populate object, all_new_values
object.replace!
For this to work, you would have to make the reset_optimistic_cache! method public. More likely you would mix in a public populate_old_values method that called it automatically.