Commit 3cdcc0f2 authored by Chris McDonough's avatar Chris McDonough

Add a 'connection-class' key to the database definition which you may use to...

Add a 'connection-class' key to the database definition which you may use to override the stock ZODB Connection class for a given database via the config file.
parent ae03ca94
......@@ -15,6 +15,8 @@
"""Datatypes for the Zope schema for use with ZConfig."""
import os
from ZODB.config import ZODBDatabase
from ZODB.config import FileStorage
# generic datatypes
......@@ -78,6 +80,27 @@ def mount_point(value):
"begin with a slash" % value)
return value
# A datatype that converts a Python dotted-path-name to an object
def importable_name(name):
try:
components = name.split('.')
start = components[0]
g = globals()
package = __import__(start, g, g)
modulenames = [start]
for component in components[1:]:
modulenames.append(component)
try:
package = getattr(package, component)
except AttributeError:
n = '.'.join(modulenames)
package = __import__(n, g, g, component)
return package
except ImportError:
raise ValueError, (
'The object named by "%s" could not be imported' % name )
# Datatype for the root configuration object
# (adds the softwarehome and zopehome fields; default values for some
# computed paths)
......@@ -119,17 +142,25 @@ def root_config(section):
return section
class ZopeDatabase(ZODBDatabase):
""" A ZODB database datatype that can handle an extended set of
attributes """
def open(self):
DB = ZODBDatabase.open(self)
# set the connection class
DB.klass = self.config.connection_class
return DB
def getDefaultDatabaseFactory(context):
# default to a filestorage named 'Data.fs' in clienthome
from ZODB.config import FileStorage
from ZODB.config import ZODBDatabase
class dummy:
def __init__(self, name):
self.name = name
def getSectionName(self):
return self.name
from ZODB.Connection import Connection
path = os.path.join(context.clienthome, 'Data.fs')
fs_ns = dummy('default filestorage at %s' % path)
fs_ns.path = path
......@@ -144,4 +175,6 @@ def getDefaultDatabaseFactory(context):
db_ns.version_pool_size=3
db_ns.version_cache_size = 100
db_ns.mount_points = ['/']
return ZODBDatabase(db_ns)
db_ns.connection_class = Connection
return ZopeDatabase(db_ns)
......@@ -95,6 +95,23 @@ class StartupTestCase(unittest.TestCase):
""")
self.assert_(isinstance(conf.dns_resolver, resolver.caching_resolver))
def test_zodb_db(self):
conf = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
<zodb_db main>
<filestorage>
path <<INSTANCE_HOME>>/var/Data.fs
</filestorage>
connection-class Products.TemporaryFolder.LowConflictConnection.LowConflictConnection
mount-point /
cache-size 5000
pool-size 7
version-pool-size 3
version-cache-size 100
</zodb_db>
""")
self.assertEqual(conf.databases[0].config.connection_class.__name__,
'LowConflictConnection')
def test_suite():
return unittest.makeSuite(StartupTestCase)
......
......@@ -121,8 +121,9 @@
</sectiontype>
<sectiontype name="zodb_db" datatype="ZODB.config.ZODBDatabase"
<sectiontype name="zodb_db" datatype=".ZopeDatabase"
implements="ZODB.database" extends="zodb">
<description>
We need to specialize the database configuration section for Zope
only by including a (required) mount-point argument, which
......@@ -131,6 +132,15 @@
</description>
<multikey name="mount-point" required="yes" attribute="mount_points"
datatype=".mount_point"/>
<description>
We want to allow people to be able to change the connection
class a database uses on a per-database basis to support
different connection policies.
</description>
<key name="connection-class" datatype=".importable_name"
default="ZODB.Connection.Connection"/>
</sectiontype>
<!-- end of type definitions -->
......@@ -273,7 +283,7 @@
</description>
</key>
<multisection type="ZODB.database" name="+" attribute="databases">
<multisection type="zodb_db" name="+" attribute="databases">
<description>
Zope ZODB databases must have a name, and they are required to be
referenced via the "zodb_db" database type because it is
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment