118 Transaction management 7.2 Transaction interface A JDO
Transaction strategies 119 7.3 Transaction strategies JDO supports two specific transaction strategies. Pessimistic transactions are a required feature of the specification, and are therefore supported by all compliant implementations. Optimistic transactions are an optional feature of the specification. They will be supported by many, but not all, implementations. Support for this feature does not depend on native support for optimistic transactions in the underlying data store vendors may choose to simulate this feature where such native support is lacking. 7.3.1 Pessimistic (data store) transactions Pessimistic transactions are the default in JDO. They are suitable when the transaction is very short-lived, typically because there is no user interaction or other blocking activity between the transaction s start and end. When data is read or changed during a pessimistic transaction, other transactions are excluded from accessing that data until the first transaction has been completed. Pessimistic JDO transactions are usually implemented through native pessimistic transactions in the underlying data store. We have already seen examples of pessimistic transactions in previous chapters any transaction begun with the optimistic property of the transaction set to false is a pessimistic transaction. In the example below, a pessimistic transaction is used to undertake an element of work. If any exceptions occur while the work is being performed, the transaction is rolled back if it is still active. Transaction t = pm.currentTransaction(); t.setOptimistic(false); // I m being explicit in case it was // previously set true t.begin() try { // do some work here which does not involve significant // delays, therefore // this is a short-lived transaction // eg. Obtain a BusinessPartner and immediately update it } catch (Exception e) { // rollback the transaction if it is still active if (t.isActive()) t.rollback(); } finally { try { // commit the transaction if it is still active if (t.isActive()) t.commit(); }
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services