25.01.2015 Views

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Object Persistence<br />

Note:<br />

In a multiprocess environment, an application with improper concurrency controls<br />

can use %Save in a way that results in unexpected behavior. For more information,<br />

see the appendix “Object Concurrency Options.”<br />

12.2.1.1 Saving <strong>Objects</strong> and Transactions<br />

The %Save method automatically saves all the objects in its SaveSet as a single transaction.<br />

If any of these objects fail to save, then the entire transaction is rolled back.<br />

If you wish to save two or more unrelated objects as a single transaction, then you must<br />

enclose the calls to %Save within an explicit transaction: that is, you must start the transaction<br />

using the TSTART command and end it with the TCOMMIT command.<br />

For example:<br />

// start a transaction<br />

TSTART<br />

// save first object<br />

Set sc = obj1.%Save()<br />

// save second object (if first was save)<br />

If ($$$ISOK(sc)) {<br />

Set sc = obj2.%Save()<br />

}<br />

// if both saves are ok, commit the transaction<br />

If ($$$ISOK(sc)) {<br />

TCOMMIT<br />

}<br />

There are two things to note about this example:<br />

1. The %Save method knows if it being called within an enclosing transaction (because<br />

the system variable, $TLEVEL, will be greater than 0).<br />

2. If any of the %Save methods within the transaction fails, the entire transaction is rolled<br />

back (the TROLLBACK command is invoked). This means that an application must test<br />

every call to %Save within a explicit transaction and if one fails, skip calling %Save on<br />

the other objects and skip invoking the final TCOMMIT command.<br />

If you are saving objects from Java, and wish to save multiple objects within the same transaction<br />

(as in the above example) you can use the transactionStart and transactionCommit<br />

methods of the Java Database class.<br />

12.2.1.2 READONLY <strong>Objects</strong><br />

It is possible to define persistent objects that can be opened but not saved or deleted. To do<br />

this set the READONLY parameter for the class to 1:<br />

102 <strong>Using</strong> <strong>Caché</strong> <strong>Objects</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!