mò Sã¸Ec@s©dZdkZdklZdkZdeifd„ƒYZdeifd„ƒYZd„Zd„Z d „Z d „Z d „Z e d joeid dƒndS(sJ $Id: testcrossdatabasereferences.py 40739 2005-12-12 17:25:26Z tim_one $ N(sdoctesttMyClasscBstZRS(N(t__name__t __module__(((tE/data/zmath/zope/lib/python/ZODB/tests/testcrossdatabasereferences.pyRstMyClass_w_getnewargscBstZd„ZRS(NcCsfS(N((tself((Rt__getnewargs__s(RRR(((RRscCsdS(sõ It's important to use consistent connections. References to to separate connections to the ssme database or multi-database won't work. For example, it's tempting to open a second database using the database open function, but this doesn't work: >>> import ZODB.tests.util, transaction, persistent >>> databases = {} >>> db1 = ZODB.tests.util.DB(databases=databases, database_name='1') >>> db2 = ZODB.tests.util.DB(databases=databases, database_name='2') >>> tm = transaction.TransactionManager() >>> conn1 = db1.open(transaction_manager=tm) >>> p1 = MyClass() >>> conn1.root()['p'] = p1 >>> tm.commit() >>> conn2 = db2.open(transaction_manager=tm) >>> p2 = MyClass() >>> conn2.root()['p'] = p2 >>> p2.p1 = p1 >>> tm.commit() # doctest: +NORMALIZE_WHITESPACE Traceback (most recent call last): ... InvalidObjectReference: Attempt to store a reference to an object from a separate connection to the same database or multidatabase >>> tm.abort() Even without multi-databases, a common mistake is to mix objects in different connections to the same database. >>> conn2 = db1.open(transaction_manager=tm) >>> p2 = MyClass() >>> conn2.root()['p'] = p2 >>> p2.p1 = p1 >>> tm.commit() # doctest: +NORMALIZE_WHITESPACE Traceback (most recent call last): ... InvalidObjectReference: Attempt to store a reference to an object from a separate connection to the same database or multidatabase >>> tm.abort() N((((Rt$test_must_use_consistent_connectionss2cCsdS(s’ If a connection participates in a multidatabase, then it's connections must remain so that references between it's cached objects remain sane. >>> import ZODB.tests.util, transaction, persistent >>> databases = {} >>> db1 = ZODB.tests.util.DB(databases=databases, database_name='1') >>> db2 = ZODB.tests.util.DB(databases=databases, database_name='2') >>> tm = transaction.TransactionManager() >>> conn1 = db1.open(transaction_manager=tm) >>> conn2 = conn1.get_connection('2') >>> z = MyClass() >>> conn2.root()['z'] = z >>> tm.commit() >>> x = MyClass() >>> x.z = z >>> conn1.root()['x'] = x >>> y = MyClass() >>> y.z = z >>> conn1.root()['y'] = y >>> tm.commit() >>> conn1.root()['x'].z is conn1.root()['y'].z True So, we have 2 objects in conn1 that point to the same object in conn2. Now, we'll deactivate one, close and repopen the connection, and see if we get the same objects: >>> x._p_deactivate() >>> conn1.close() >>> conn1 = db1.open(transaction_manager=tm) >>> conn1.root()['x'].z is conn1.root()['y'].z True >>> db1.close() >>> db2.close() N((((Rt3test_connection_management_doesnt_get_caching_wrongQs)cCsdS(s] >>> import ZODB.tests.util, transaction, persistent >>> databases = {} >>> db1 = ZODB.tests.util.DB(databases=databases, database_name='1') >>> db2 = ZODB.tests.util.DB(databases=databases, database_name='2') >>> tm = transaction.TransactionManager() >>> conn1 = db1.open(transaction_manager=tm) >>> conn2 = conn1.get_connection('2') >>> z = MyClass() >>> conn1.root()['z'] = z >>> conn1.add(z) >>> s = tm.savepoint() >>> conn2.root()['z'] = z >>> tm.commit() >>> z._p_jar.db().database_name '1' >>> db1.close() >>> db2.close() N((((Rt#test_explicit_adding_with_savepoint|scCs&|idiƒ|idiƒdS(Ntdb1tdb2(ttesttglobstclose(R ((Rt tearDownDbs•scCsXtitiddtdtƒdtƒtiddtdtƒdtƒtiƒfƒS(Ns ../cross-database-references.txtR RttearDown( tunittestt TestSuitetdoctestt DocFileSuitetdictRRRt DocTestSuite(((Rt test_suite™s t__main__t defaultTestR(t__doc__Rt zope.testingRt persistentt PersistentRRRRR RRRtmain( RRRRRR RRRR((Rt?s    4 +