Basically this patch adds a second connection pool for the write server. The new
configuration option is "database_write", with the same syntax as the normal
"database" option. If database_write is omitted then the normal pool will be
used.
It works by returning a DatabasePair instead of the actual database connection.
Much existing code will have to be changed to support this change, which will be
done in the following patch. The goal of this is to implement easy
synchronization between two Trac instances by having one master database which
pushes changes to the slave databases.
Signed-off-by: Axel Gembe <[EMAIL PROTECTED]>
---
trac/db/api.py | 42 ++++++++++++++++++++++++++++++++++++------
trac/env.py | 2 +-
2 files changed, 37 insertions(+), 7 deletions(-)
diff --git a/trac/db/api.py b/trac/db/api.py
index e194293..acbb9e7 100644
--- a/trac/db/api.py
+++ b/trac/db/api.py
@@ -49,6 +49,13 @@ class IDatabaseConnector(Interface):
"""Return the DDL statements necessary to create the specified table,
including indices."""
+class DatabasePair(object):
+ """
+ Holds two database connections, one for reading and one for writing.
+ """
+ def __init__(self, db, dbwr):
+ self.read = db
+ self.write = dbwr
class DatabaseManager(Component):
@@ -59,31 +66,54 @@ class DatabaseManager(Component):
[wiki:TracEnvironment#DatabaseConnectionStrings string] for this
project""")
+ write_connection_uri = Option('trac', 'database_write', '',
+ """Database write connection
+ [wiki:TracEnvironment#DatabaseConnectionStrings string] for this
+ project""")
+
timeout = IntOption('trac', 'timeout', '20',
"""Timeout value for database connection, in seconds.
Use '0' to specify ''no timeout''. ''(Since 0.11)''""")
def __init__(self):
self._cnx_pool = None
+ self._cnx_pool_write = None
def init_db(self):
- connector, args = self._get_connector()
+ connector, args = self._get_connector(self.connection_uri)
+ if self.write_connection_uri and self.write_connection_uri != '':
+ connector, args = self._get_connector(self.write_connection_uri)
connector.init_db(**args)
- def get_connection(self):
+ def _create_pool(self):
if not self._cnx_pool:
- connector, args = self._get_connector()
+ connector, args = self._get_connector(self.connection_uri)
self._cnx_pool = ConnectionPool(5, connector, **args)
- return self._cnx_pool.get_cnx(self.timeout or None)
+ if self.write_connection_uri and self.write_connection_uri != '' and
not self._cnx_pool_write:
+ connector, args = self._get_connector(self.write_connection_uri)
+ ### FIXME: Connection pool does not handle the max number although
it might be needed
+ self._cnx_pool_write = ConnectionPool(1, connector, **args)
+
+ def get_connection(self):
+ self._create_pool()
+ conn = self._cnx_pool.get_cnx(self.timeout or None)
+ if not self.write_connection_uri or self.write_connection_uri == '':
+ return DatabasePair(conn, conn);
+ else:
+ return DatabasePair(conn,
self._cnx_pool_write.get_cnx(self.timeout or None))
def shutdown(self, tid=None):
if self._cnx_pool:
self._cnx_pool.shutdown(tid)
if not tid:
self._cnx_pool = None
+ if self._cnx_pool_write:
+ self._cnx_pool_write.shutdown(tid)
+ if not tid:
+ self._cnx_pool_write = None
- def _get_connector(self): ### FIXME: Make it public?
- scheme, args = _parse_db_str(self.connection_uri)
+ def _get_connector(self, db_str): ### FIXME: Make it public?
+ scheme, args = _parse_db_str(db_str)
candidates = {}
for connector in self.connectors:
for scheme_, priority in connector.get_supported_schemes():
diff --git a/trac/env.py b/trac/env.py
index d082269..11a4314 100644
--- a/trac/env.py
+++ b/trac/env.py
@@ -253,7 +253,7 @@ class Environment(Component, ComponentManager):
fd.close()
def get_db_cnx(self):
- """Return a database connection from the connection pool."""
+ """Return a database connection pair from the connection pool."""
return DatabaseManager(self).get_connection()
def shutdown(self, tid=None):
--
1.5.6
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Trac
Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/trac-dev?hl=en
-~----------~----~----~----~------~----~------~--~---