Re: two databases from one application
Re: two databases from one application
- Subject: Re: two databases from one application
- From: Chuck Hill <email@hidden>
- Date: Wed, 10 May 2006 10:01:04 -0700
On May 10, 2006, at 9:20 AM, Milan M wrote:
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.
I'd consider that to be a risky / difficult thing to do with EOF.
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);
You don't need that line. localInstanceOfObject will already have
included it in that EC. However, as it is not in the second
database, you are likely to get an exception when the fault is
fired. I think that you will need to manually create a new EO in
the second EC and then manually copy the data over, including doing
this for all related objects.
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);
That is because you already have a localInstance of the object you
are trying to insert.
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.
Maybe this?
http://sequoia.continuent.org/HomePage
Chuck
--
Coming in 2006 - an introduction to web applications using WebObjects
and Xcode http://www.global-village.net/wointro
Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems. http://www.global-village.net/products/practical_webobjects
_______________________________________________
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