Thursday, December 13, 2007

DataServiceTransaction & LCDS

Just a quick blog note on something that I've now wasted a good day on twice, only because I never blogged it last time. The challenge is say you need to make changes to data in a database, maybe across multiple tables, and using DataServices just doesn't make sense. For example, cloning x number of records. It's easy enough to create your Java remote objects and call them from Flex, but then in an FDS/LCDS implementation how do you push those changes out to Flex clients?

There's some documentation on this, scattered vaguely across the web, and there's a good chance that perhaps this has already been blogged - but I just couldn't find it. The trick is that there are 2 sides to this story, one, you must perform your operations on the database directly, and then two, you must INFORM FDS that changes have occurred. So say I want to create a new Author record, this is what that might look like:

public void createAuthor(){  
DataServiceTransaction dtx = DataServiceTransaction.begin(false);
//create record and save:
Author auth = new Author();
auth.setFirstName("Ernest");
auth.setLastName("Hemingway");
AuthorDAO dao = new AuthorDAO();
dao.create(auth);
//let LCDS know:
dtx.createItem("author", auth);
dtx.commit();
}
So I just do my regular db insert, then I use a DataServiceTransaction to let FDS know that I've created an Item. Alternatively, I could use this approach:
public void createFDSItem(){
DataServiceTransaction dtx = DataServiceTransaction.begin(false);
Author auth = new Author();
auth.setFirstName("victor");
auth.setLastName("javarubba");
AuthorDAO dao = new AuthorDAO();
dao.create(auth);
dtx.refreshFill("author", null);
dtx.commit();
}
This approach might be a bit of overkill and might be better suited when doing mass updates across many records. I did a little bit of testing and it seemed that for the most part calling CreateItem() was faster that the refreshFill() in pushing the new record to the Flex Client.

What cost me so much grief is that I was falsely led down the garden path to believe that calling dtx.createItem("author", auth) would not only inform FDS but also actually create the item. This is not the case. There! Officially blogged!