Andrus, Michael,
Thanks for your answers, they were very helpful.
On Apr 21, 2009, at 2:00 PM, Michael Gentry wrote:
On Tue, Apr 21, 2009 at 4:40 PM, Ylan Segal <[email protected]>
wrote:
Ok. That makes sense. I can see that for a web application you
would want to
have each user manipulate data on it's own, without affecting what
other
users are doing. As far as junit test cases go, I can just create a
new
DataContext without much consequence. Right?
Yes. A DataContext (or ObjectContext) is fairly cheap to create, too.
Create them as it makes sense. Some people like to create a new DC
for every "transaction" they wish to do. Some like to have a
session-based DC (in a web application). Some use a mixture or add in
nested DCs. It really depends on what you are trying to accomplish.
It is also safe in Cayenne to reuse a DC across request/response loops
in a web application. Or even within the same request/response cycle.
Cayenne does not disconnect objects from their database channel after
a commit is done (unlike Hibernate). You typically do not care about
the database channel with Cayenne, either -- the DC manages that.
In a web app where each session has a DataContext, do I need to
worry about
data caching in different DataContexts? I don't know if that is
clear, so
let me explain. Suppose we are talking about a pet store. User A
and B are
both separately
in their browser shopping for a cat. There is only 1 left in
inventory. Both
user A and B see that there is one left. User A makes up his mind
first and
buys the cat. The changes are committed. In the DB the inventory
correctly
has the number of cats at 0. If User B's DataContext caches the
data, it
might think that the inventory is still 1 and also allow User B to
buy the
cat. Should I worry about this or is it all taken care of by Cayenne?
This actually brings up many issues. One of the issues is data
freshness (and you can force a refresh if if you want). However,
there is always some latency involved between two requests, so even
that could result in a missed read. I think what you are really
asking about here is optimistic locking:
http://cwiki.apache.org/CAY/optimistic-locking-explained.html
Let's say user A and B both read from the "inventory" table a value of
1 for "number_left". You are really wanting Cayenne to generate, as
part of a transaction -- dataContext.commitChanges() -- an update
similar to this:
UPDATE inventory SET number_left = 0 WHERE primary_key = 587375 and
number_left = 1;
When user A commits, all is well. When user B commits, an exception
will be thrown by Cayenne because there WHERE clause fails (the
"number_left" value is now at 0 due to user A's commit).
Thanks for the answers. In the meantime I have been trying out the
modeler
and it seems to do a great job of creating the db schema,
relationship
mapping and Java code generation. So far I am very impressed!
--
Ylan.
--
Ylan Segal
[email protected]