Inline scripted transactions
Karl Wettin <[email protected]>
| Newsgroups | gmane.comp.java.prevayler |
|---|---|
| Message-ID | <[email protected]> |
I've started experimenting using beanshell script in my transactions.
public class Script<T> implements TransactionWithQuery<T> {
/** adds more text to the script */
public void append(String script);
/** serialize the instance to object factory script */
public void set(String attribute, Object instance) throws Exception;
/** serialize the instance to object factory script and returns the
name of the new attribute*/
public void set(Object instance) throws Exception;
}
This makes for code like this:
Bank bank = new Bank();
BankCustomer alice = new BankCustomer("Alice");
alice.setBank(bank);
bank.getCustomers().add(alice);
BankAccount alicesAccount = new BankAccount("123", alice);
alice.getAccounts().add(alicesAccount);
bank.getAccounts().put("123", alicesAccount);
System
.out.println(BshScriptSerializer.getAttributeOrExpression("bank",
bank));
> bank=new se.kodapan.domain.Bank();
> __tmpBankAccount1279263931208=new se.kodapan.domain.BankAccount();
> __tmpBankCustomer1279263931209=new se.kodapan.domain.BankCustomer();
> __tmpBankCustomer1279263931209.setLoans(new java.util.ArrayList());
> __tmpBankCustomer1279263931209.setAccounts(new java.util.ArrayList());
> __tmpBankCustomer1279263931209
> .getAccounts().add(__tmpBankAccount1279263931208);
> __tmpBankCustomer1279263931209.setName("Alice");
> __tmpBankCustomer1279263931209.setBank(bank);
> __tmpBankAccount1279263931208.setAccountNumber("123");
> __tmpBankAccount1279263931208
> .setCustomer(__tmpBankCustomer1279263931209);
> bank.setAccounts(new java.util.HashMap());
> bank.getAccounts(.put("123", __tmpBankAccount1279263931208);
> bank.setSecurities(new java.util.HashSet());
> bank.setCustomers(new java.util.HashSet());
> bank.getCustomers().add(__tmpBankCustomer1279263931209);
>
Script script = new Script();
script.invoke("root.put(" + set(alice) + ");");
script.invoke("root.put(" + set(bank) + ");");
prevayler.executeTransaction(script);
Script script = new Script();
script.invoke("bank=root.get(" + script.set(bank.getId() + ");");
script.invoke("bank.getCustomers().remove(" +
script.set(alice.getId()) + ");");
prevayler.executeTransaction(script);
I find this very neat and think it's safe since transactions are
"useless" after taking a snapshot. The problem would be if somone is
interested in the journal and the code has been refactored in a way
that the script in the transactions no longer is valid. Is that right?
Something else I might be missing that makes this a bad idea?
karl
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
To unsubscribe go to the end of this page: http://lists.sourceforge.net/lists/listinfo/prevayler-discussion
_______________________________________________
"Databases in Memoriam" -- http://www.prevayler.org