I have only just started to use ZODB and have been able to use it with
ease so far.

 

I want to be able to have access to multiple ZODB databases at the same
time that are completely independent.

What I cannot see how to do is to keep the changes to the objects from
each database separate - that is making the transaction commits only
occur for one database at a time even if changes have occurred to
multiple databases between commits.

 

Can the transactions be made specific to a particular database or
connection?

 

Below is example code that accesses two independent databases.

It opens the databases, increments the value for an object in each
database, but I only want to commit the changes to one of databases.

The code here commits the changes for both databases.

 

How do I stop that happening?

I am assuming that I need to do something different than just
"transaction.commit()" but what do I need to do?

 

Thanks

Matt

 

##### START CODE

 

 

from ZODB import FileStorage, DB

import transaction

 

from persistent import Persistent

 

 

def get_item_root(file_name):

    # open the database

    storage = FileStorage.FileStorage(file_name)

    db = DB(storage)

    conn = db.open()

    dbroot = conn.root()

    return dbroot

 

def get_item(dbroot, name):

    try:

        item = dbroot[name]

    except KeyError:

        item = None

    return item

 

def add_item(dbroot, item):

    if item.name not in dbroot: 

        dbroot[item.name] = item

    

def save_item(dbroot, item):

    transaction.commit()

 

class Test(Persistent):

    def __init__(self, name):

        self.name = name

        self.value = 0

 

 

db_1 = get_item_root("db_1")

db_2 = get_item_root("db_2")

 

test_1 = get_item(db_1, "test_1")

if test_1 is None:

    test_1 = Test("test_1")

    add_item(db_1, test_1)

 

test_2 = get_item(db_2, "test_2")

if test_2 is None:

    test_2 = Test("test_2")

    add_item(db_2, test_2)

 

print "BEFORE: test_1: [%s] [%s]" % (test_1.name, test_1.value)

print "BEFORE: test_2: [%s] [%s]" % (test_2.name, test_2.value)

 

# increment values

test_1.value = test_1.value + 1

test_2.value = test_2.value + 1

print "AFTER: test_1: [%s] [%s]" % (test_1.name, test_1.value)

print "AFTER: test_2: [%s] [%s]" % (test_2.name, test_2.value)

 

save_item(db_1, test_1)

 

 

##### END CODE #####

 


This e-mail and any attachment may contain confidential and/or privileged 
information. If you have received this e-mail and/or attachment in error, 
please notify the sender immediately and delete the e-mail and any attachment 
from your system. If you are not the intended recipient you must not copy, 
distribute, disclose or use the contents of the e-mail or any attachment. 
All e-mail sent to or from this address may be accessed by someone other than 
the recipient for system management and security reasons or for other lawful 
purposes. 

Xype Limited is registered in England and Wales under company number 04516192. 
The company's registered office is Unit 1 Brabazon Office Park, Golf Course 
Lane, Filton, Bristol, BS34 7PZ
_______________________________________________
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zodb-dev

Reply via email to