About using/design with ODMG API :
Generally I want to get a domain (business) object from the persistence layer. Then I
use it in my app (webapp), modify it and when modification are validated, update it in
the persistent storage.
I use a class for persistence actions (CRUD) like this :
class BookPersistence {
static BookPersistence getInstance() {...}
Book getBook(String bookId) { ...}
void storeBook(Book b) { ...}
void deleteBook(Book b) { ...}
void updateBook(Book b) { ...}
}
And I use it like this :
Book b = new Book();
BookPersistence.getInstance().store(b);
...
And :
Book b1 = BookPersistence.getInstance().getBook("bookID");
b1.setXXX(...);
...
BookPersistence.getInstance().updateBook(b1);
Whith an update method like this :
public void updateBook(Book b) {
tx.begin();
tx.lock(myBook , Transaction.WRITE);
tx.commit;
}
It doesn work 'cause book is modified outside the transaction and OJB doesn't
"monitor" it (I think).
Same things with :
public void updateBook(Book b) {
tx.begin();
OQLQuery query = odmg.newOQLQuery();
query.create("select book from "+ Book.class.getName()+ " where code=\""+
b.getCode()+ "\"");
DList results = (DList) query.execute();
Book myBook = (Book) results.get(0);
tx.lock(myBook , Transaction.WRITE);
tx.commit;
}
But I can't open a transaction during all the modification time of my object and
commit at the end, isn't it ?
If I use this object in a webapp (modification by a form), I must wait all the
validation (and request, validation, ...) before commiting. It retains lock during
this time (even if I use optimistic locking, a long time transaction can desynchronize
data with multiple users).
And I've got to add Transaction demarcation in my business process code.
So I try to declare the object dirty in my transaction like this :
public void updateBook(Book b) {
tx.begin();
tx.lock(b, Transaction.WRITE);
((TransactionImpl)tx).makeDirty(b);
tx.commit;
}
It works but it's not a standardized process (the makeDirty() of TransactionImpl
object is not part of ODMG API and force db calls even if the object is not modified).
So what's the solution ? Am I obliged to modify the object properties inside the
transaction ? Do I use bad patterns ? Is there any trick ?
Thanx