two databases from one application
two databases from one application
- Subject: two databases from one application
- From: Milan M <email@hidden>
- Date: Wed, 10 May 2006 12:20:18 -0400 (EDT)
Hello all,
WO application I am working on was pretty simple up to
this point: one apache webserver and one database
server serving data to it. The things turned more
complicated now: In addition to the first database, we
now want to add access to another database. Both of
these databases have identical schema, and the tables
in question are identical. Think of the second
database as a backup.
At one point in the applicaiton, I want to take an
enterprise object generated in defaultEditingContext
and to save it to the other database. I have read the
manuals, and decided to:
1. Generate another editing context (newContext)
2. Set it to connect to this new database
3. Insert object into it from defaultEditingContext
4. Delete object from defaultEditingContext since I
don't need it
5. Save newContext, thus committing this record to the
new database.
Here's the code that fails:
/* ===== BEGIN ===== */
EOEditingContext newContext = new EOEditingContext();
/* Now connect to the new database with
connectWithModelNamed(...) */
EOModel modelNew =
EOModelGroup.defaultGroup().modelNamed("DefaultModel");
NSDictionary dictNew =
modelNew.connectionDictionary();
NSMutableDictionary dictMut =
dictNew.mutableClone();
dictMut.takeValueForKey(System.getProperty("myNewDatabaseConnection"),
"URL");
NSDictionary newDict =
dictMut.immutableClone();
EOUtilities.connectWithModelNamed(newContext,
"DefaultModel", newDict);
/* transfer from default EditingContext into new one,
delete from default */
EOEnterpriseObject objectE =
EOUtilities.localInstanceOfObject(
session().defaultEditingContext() ,
myEnterpriseObject);
session().defaultEditingContext().deleteObject(myEnterpriseObject);
newContext.insertObject(objectE);
newContext.saveChanges(); // at this point, I should
find this record in the new database, correct?
/* When done with the above, connect back to the old
database (reuse variables above) */
dictMut.takeValueForKey(System.getProperty("myOldDatabaseConnection"),
"URL");
NSDictionary newDict2 = dictMut.immutableClone();
EOUtilities.connectWithModelNamed(session().defaultEditingContext(),
"DefaultModel", newDict2);
/* ===== END ===== */
Note: "System.getProperty" calls on two strings,
"myNewDatabaseConnection" and
"myOldDatabaseConnection". These are defined in
Properties.java and they look like this:
myNewDatabaseConnection=jdbc:oracle:thin:@111.111.111.111:1521:newDB
myOldDatabaseConnection=jdbc:oracle:thin:@111.111.111.110:1521:oldDB
Error message I get when I try the above:
java.lang.IllegalArgumentException: An
EOEnterpriseObject may only be in (or observed by) a
single EOEditingContext.
It shows up on this line:
newContext.insertObject(objectE);
Can someone tell me what's wrong with the code? I
thought that EOUtilities.localInstanceOfObject returns
just that -- a local copy of an object. After I had
received this message for the first time, I reasoned
that maybe I should delete it from the originating
editingContext before I try to insert. That's where I
am right now, and I still get the same error.
Maybe I have to connect to the new database in some
other manner?
Better yet, it someone worked with simultaneous access
to several databases and has some tutorials or code
examples, please post them? In perl, using DBI/DBD,
this would be a fairly simple thing to do: create a
channel to another database, dump record into it. I
appreciate that EnterpriseObjects are very powerful
and have a steep learning curve, but frankly I'd
rather be solving business requirements problems than
technical details.
Thanks in advance guys and girls.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden