Archive for October, 2006

112 Primary interfaces and classes Object getObjectById(Object id)

Wednesday, October 25th, 2006

Extent 113 manager, to actually read instances from the database. Only when an Iterator is obtained and its next() method invoked does the data retrieval process commence. Therefore it is quite legitimate to obtain a particular extent at the beginning of an application, and use it to obtain Iterators as required, and in different transactions, until the application is closed. Of course, if you take this approach you should be doubly sure to close each Iterator as soon as you ve finished with it in order to conserve system resources. These Iterators are closed through the close() methods of the Extent. Extent of an interface In some implementations, it may be possible to obtain the extent of an interface. This may require the interface to have been enhanced. Iterators obtained from such an extent would yield all of the persistent JDO instances that implement the chosen interface. In theory, the extent of the SellableItem interface could be used to list items that can be placed on an order. However, this is not quite as useful as it might seem. Since interfaces have only methods and not true attributes, there can be no concept of the persistent fields of an interface. The JDO Query Language (JDOQL) requires that persistent fields be supported within query filters; arbitrary method invocations are not supported. Thus it may be impossible to execute meaningful JDOQL queries against such extents in a portable manner. For more information about JDOQL, see Chapter 8. Support for the extent of an interface should be considered non-standard unless it is explicitly included in a future version of JDO. We have already used extents in several examples when iterating through all persistent instances of a candidate class. However, the primary purpose of an extent is to provide a candidate collection of instances to a query. The query will then apply its filtering semantics to the extent. The result of query execution is a collection that is a subset of the extent containing only those instances that match the specified filter criteria. How do queries actually use extents? Although, logically, a query applies a filter to an extent, it is not expected to do this merely by iterating from start to finish. In most cases, information from the extent will merely be used to create an equivalent query in the data store s native query language. Implementations will compete amongst each other in an effort to provide the most efficient query resolution possible, by maximizing the capabilities of the underlying data store in this regard. We discuss JDOQL and the Query interface in detail in Chapter 8.

Hint: This post is supported by Gama hrvatski web hosting services

112 Primary interfaces and classes Object getObjectById(Object id)

Wednesday, October 25th, 2006

112 Primary interfaces and classes Object getObjectById(Object id) This method retrieves a single persistent instance from the data store based on the identified Object ID instance. It is possible for an application to determine an instance s identity from a persistence manager of a particular implementation, and to subsequently attempt to get the corresponding instance using the getObjectById() method of another persistence manager that is part of a different implementation. In such cases, the Object ID is guaranteed to be portable only if the JDO instance has application identity. For datastore identity the actual class acting as Object ID is an implementation choice, and portability across implementations is not even likely, let alone guaranteed. 6.4.6 Query factory methods A persistence manager acts as the factory for JDO Query objects. This is provided through nine overloaded variants of the method newQuery(). Each of these will be discussed in Chapter 8. 6.5 Extent The extent of a class (the so-called candidate class) represents the complete set of all persistent instances of that class. In pure object oriented terms, an extent actually contains all classes that are instances of the candidate class, although JDO allows the construction of an extent to optionally include or exclude subclasses of the candidate class. Extents without subclasses There are a number of JDO experts, myself included, who believe that the concept of an extent of a candidate class without subclasses does not make sense in terms of object orientation. We recognize that extents without subclasses do facilitate performance improvements with some Implementations. However, it is our opinion that the subclasses = false setting should be considered as a hint to the implementation that it may optimize execution by ignoring any subclasses, rather than a mandatory requirement to specifically exclude any persistent subclasses which may exist. I present this argument in more detail in Chapter 13. An extent is merely a holder for the class descriptor, subclasses flag, and a collection of open Iterator objects. In this regard the JDO specification does not expect the obtaining of an extent, by calling getExtent() on a persistence

Hint: This post is supported by Gama hrvatski web hosting services

110 Primary interfaces and classes in either type

Tuesday, October 24th, 2006

PersistenceManager 111 invocation of its methods throws a JDOFatalUserException, apart from the isClosed() method that returns true. When a persistence manager is closed, the implementation may choose what to do with it. In a non-managed environment the object is likely to be discarded (and subsequently garbage-collected), whilst in a managed environment the object is likely to be returned to a pool of free persistence managers. setUserObject(Object o) Object getUserObject() These methods allow an application to associate any single but arbitrary object with a persistence manager. This may be used by an application framework for infrastructure or administrative purposes. PersistenceManagerFactory getPersistenceManagerFactory() This method returns a reference to the factory from which the persistence manager was originally obtained. Transaction currentTransaction() This method returns a reference to the persistence manager s transaction object (an instance of javax.jdo.Transaction). Each persistence manager has only one such object, through which the application can demarcate transactions serially. If separate transactions are required in parallel, separate persistence managers will have to be used. void setMultithreaded(boolean multithreaded) boolean getMultithreaded() These methods allow manipulation of the Multithreaded option within a per sistence manager. If supported by the implementation, setting Multithreaded to true enables the application to invoke its methods from multiple threads simultaneously, at the expense of added thread synchronization overheads within the persistence manager. void setIgnoreCache(boolean ignoreCache) boolean getIgnoreCache() These methods allow the manipulation of the IgnoreCache option. If IgnoreCache is set to true then the implementation is permitted (but not required) to optimize query execution by ignoring optimistically locked instances that have been changed in the current transaction. 6.4.5 Obtaining instances Extent getExtent(class cl, boolean subclasses) Given the class descriptor for a persistence-capable class, this method returns the extent of all persistent instances of that class. If the subclasses parameter is true, the extent includes all persistent instances of that class and its subclasses.
Note: If you are looking for inexpensive but high quality provider to host and run your jsp application check Astra jsp hosting services

110 Primary interfaces and classes in either type

Tuesday, October 24th, 2006

110 Primary interfaces and classes in either type of transaction, the instance transitions to Persistent-Dirty. Applications will, however, make transient instances transactional. The makeNontransactional() method is intended for applications to reverse that process. 6.4.3 Working with JDO identities PersistenceManager provides two methods for determining the identity of an instance. They are identical to the equivalent static methods of the JDOHelper class. Where they are used, the methods must be invoked on the particular PersistenceManager instance that is responsible for managing the persistence-capable instance. Since many PersistenceManagers may be active in a complex application (particularly likely when multiple data stores are accessed), it is more usual for the JDOHelper methods to be used. Class getObjectIdClass(Object pc) This method returns the class descriptor for the class that forms the JDO identity of the specified persistence-capable instance. Where application identity applies, this will be the class identified in the persistence descriptor. Where datastore identity applies, this will be the class chosen for that purpose by the implementation. Object getObjectId(Object pc) This method returns the JDO identity of the instance. Object getTransactionalObjectId(Object pc) This method is the same as getObjectId above, except that where application identity applies it will return the current identity, taking into account any alter ations made to primary key fields during the current transaction. Object newObjectIdinstance(Class cls, String representation) It is a requirement of Object ID classes that they support a toString() method outputting a string representation of the key, and a string constructor that can accept such a representation and equivalently initialize an appropriate Object ID instance. The newObjectIdinstance method accepts the class descriptor for a persistence-capable class, and a string representation of an Object ID. The method identifies the appropriate Object ID class for the indicated persistence-capable class, and returns a new instance initialized from the string representation. 6.4.4 Administrative functions The following functions facilitate an application s administration of persistence managers. void close() void isClosed() When an application has finished using a PersistenceManager instance it should call the close() method. After close() has been called, any further
Note: If you are looking for inexpensive but high quality provider to host and run your jsp application check Astra jsp hosting services

108 Primary interfaces and classes passed as a

Tuesday, October 24th, 2006

PersistenceManager 109 JDO vendors may choose to add value to their implementations by specifying deletion constraints, which cause deletes to cascade to dependent objects. However, there is no guarantee that the set of objects deleted in this manner would be the same as the set of objects that would have been persisted through reachability. Furthermore, portable applications should not rely on such delete cascade functionality, which is beyond the JDO specification and may not be supported by alternative implementations. 6.4.2.3 makeTransient void makeTransient(Object pc) void makeTransientAll(Collection c) void makeTransientAll(Object[] o) Make a persistent instance transient again. Note that this does not affect the underlying data store entity in any way. Making the instance transient does not delete the data; it merely disassociates the instance from the data store. Any subsequent changes to the instance will not be synchronized with the data store. Please note that when an instance is passed as a parameter to makeTransient(), only that instance transitions to the transient state. If it holds references to other persistent instances, they do not become transient. In this way makeTransient(), like deletePersistent(), is not the opposite of makePersistent(). 6.4.2.4 makeTransactional void makeTransactional(Object pc) void makeTransactionalAll(Collection c) void makeTransactionalAll(Object[] o) Make a non-transactional instance transactional, so that its persistent and transactional field values become subject to transaction rollback. 6.4.2.5 makeNontransactional void makeNontransactional(Object pc) void makeNontransactionalAll(Collection c) void makeNontransactionalAll(Object[] o) Make a transactional instance non-transactional. The state Persistent-Nontransactional applies to persistent instances whose field values are not necessarily in sync with the data store. This applies to persistent instances whose field values have been read, but not yet altered, in an optimistic transaction. A Persistent instance can explicitly be made Persistent-Nontransactional through the persistence manager s makeNontransactional() method, although it is unlikely that an application would choose to do so. As soon as field values are read in a data store (pessimistic) transaction, or as soon as field values are updated
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services

108 Primary interfaces and classes passed as a

Tuesday, October 24th, 2006

108 Primary interfaces and classes passed as a parameter to makePersistent(), each referenced OrderLine will be made provisionally persistent. However, it is still possible to alter the order- lines collection before the transaction is committed. At commit time, only those OrderLine instances which are referenced by the Order will be made persistent. Any OrderLine instances which were referenced at the time of makePersistent(), but are no longer referenced at the time of commit(), will remain transient. When to use makePersistent() Readers with a background in relational databases will be used to inserting new entities into the data store. This is essentially what makePersistent() does, although with a little more flair (e.g. persistence by reachability). However, if you had to call makePersistent() every time you instantiated a new object that was to be stored, you would quickly find JDO to be less than transparent! The truth is that applications only rarely need to invoke makePersistent(). Most of the time, newly created (and therefore transient) instances are referenced by existing persistent instances, either via singleton references or membership of a collection. In such cases, the new instance will be transparently made persistent when the transaction is committed. Two occasions when you will nevertheless call makePersistent() are: . when you are persisting an instance which is not immediately being referenced by another persistent instance; . when you need to obtain the Object ID for your new Instance before the transaction has been committed. 6.4.2.2 deletePersistent void deletePersistent(Object pc) void deletePersistentAll(Collection c) void deletePersistentAll(Object[] o) These methods delete persistent instances from the database. They must be called in the context of an active transaction (albeit an optimistic transaction). The instance transitions to Persistent-Deleted or Persistent-New-Deleted as appropriate. After the transaction has been successfully committed it transitions to Transient. The Java object thus remains, but no longer represents the persistent data store entity, which has been deleted. Please note that deletePersistent() is not the exact opposite of makePersistent(). Persistence by reachability permits the makePersistent() call to persist a closure of instances. However, JDO merely specifies that deletePersistent() will delete the particular instance (or instances if a collection is given).
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services

106 Primary interfaces and classes arrays of instances,

Tuesday, October 24th, 2006

PersistenceManager 107 6.4.1.3 retrieve void retrieve(Object pc) void retrieveAll(Collection c) void retrieveAll(Object[] o) The retrieve methods cause the instance s as yet unread fields to be read from the data store. If your application has a reference to a Collection of JDO instances (perhaps reference called orders to a HashSet containing Order objects) and you are about to iterate through the Collection, you might consider calling retrieveAll(orders). This will cause the implementation to read all the field values of each referenced Order instance. This minimizes subsequent data store activity as the application accesses each Order. Avoid doing so if your Collection is known to contain an enormous number of instances, or if you do not intend to work through the whole of its contents. 6.4.2 Instance lifecycle The following methods are used by an application to alter the state of a JDO instance. 6.4.2.1 makePersistent void makePersistent(Object pc) void makePersistentAll(Collection c) void makePersistentAll(Object[] o) This is a request to create a new entity in the data store. Where application identity is used and the primary key already exists in the data store, the instance cannot be persisted. Depending on the implementation, an exception will be thrown either when makePersistent() is called or when the transaction is committed. With datastore identity the entity will be persisted even if another persistent entity already exists with identical persistent field values, as the JDO identity for each object (created at persist time by the implementation) will be different. At the time that a transient instance is made persistent, the implementation marks any other transient instances that are referenced by persistent fields of the newly persistent instance as being provisionally persistent. This occurs recursively, until the closure (complete set) of transient instances referenced by such persistent fields is provisionally persistent. At commit time, those provisionally persistent instances which are still part of this closure are made persistent. This is called persistence by reachability . By way of an example, consider a transient Order instance that holds a collection of references to (transient) OrderLine instances. When the Order is
Note: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting services

106 Primary interfaces and classes arrays of instances,

Tuesday, October 24th, 2006

106 Primary interfaces and classes arrays of instances, and potentially another applies the manipulation to all applicable instances in the cache. The precise meanings of these overloaded methods will be described only for the eviction methods. 6.4.1 Cache management The following methods allow an application to influence the management of a persistence manager s cache. 6.4.1.1 Evict void evict(Object pc) Evict the identified persistence-capable instances from the cache. void evictAll(Collection c) Evict all members of the identified collection of persistence-capable instances from the cache. void evictAll(Object[] o) Evict all members of the identified array of persistence-capable instances from the cache. void evictAll() Evict all cached persistence-capable instances from the cache. Eviction is a hint to the persistence manager that an instance should be removed from its cache. Under usual circumstances this happens automatically during transaction completion, and it is not necessary for an application to programmatically evict instances. The persistence manager will ignore requests to evict instances that are in an inappropriate state, e.g. those that are dirty. 6.4.1.2 refresh void refresh(Object pc) void refreshAll(Collection c) void refreshAll(Object[] o) void refreshAll() The instance s persistent field values are reset to their values as at the start of the transaction, and the instance transitions back to Persistent-Clean if it was Persistent-Dirty. Persistent-Nontransactional (e.g. optimistically locked) instances have their fields refreshed but remain in the same state. See Chapter 7, for more information about optimistic transaction strategies.
Note: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting services

104 Primary interfaces and classes The most important

Tuesday, October 24th, 2006

PersistenceManager 105 6.3.1.1 Example optional feature support Below is an example application that bootstraps a JDO implementation and then lists the optional features it supports. OptionalSupport.java package com.ogilviepartners.jdobook.app; import javax.jdo.*; import com.ogilviepartners.jdo.JDOBootstrap; import java.util.Collection; import java.util.Iterator; public class OptionalSupport { public static void main(String[] args) { // instantiate the PersistenceManagerFactory JDOBootstrap bootstrap = new JDOBootstrap(); bootstrap.listJDOProperties(); bootstrap.listVendorProperties(); PersistenceManagerFactory pmf = bootstrap.getPersistenceManagerFactory(); // determine and print supported optional features System.out.println(”Supported Optional Features:”); Collection c = pmf.supportedOptions(); Iterator i = c.iterator(); while (i.hasNext()) { System.out.println(i.next()); } } } 6.4 PersistenceManager The PersistenceManager interface is the application developer s primary means of affecting the state of persistence-capable instances. In Chapter 4, I presented examples illustrating the use of a persistence manager s state management methods (makePersistent, deletePersistent, makeTransient, evict, refresh, etc.). We will now examine the PersistenceManager interface in more detail, but without unnecessarily reproducing Chapter 4 s examples. The PersistenceManager interface presents overloaded variants of most cache management and instance lifecycle methods. One of these manipulates a single instance, whilst others perform the same manipulation to collections or
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

104 Primary interfaces and classes The most important

Tuesday, October 24th, 2006

104 Primary interfaces and classes The most important non-configuration methods are detailed below. PersistenceManager getPersistenceManager() PersistenceManager getPersistenceManager (String user, String password) These methods return an appropriately configured PersistenceManager instance. The configuration is determined by the values of the various properties of the PersistenceManagerFactory itself. Property values may have been set by explicit calls to corresponding set methods prior to the first invocation of either getPersistenceManager method, or by a properties object passed to a getPersistenceManagerFactory method in the JDOHelper when the factory instance was originally obtained. The method that takes user and password as parameters will return a persistence manager with an appropriately configured connection to the data store. Note that specifying user details in this way may subvert both the pooling of connections by the connection factory and the pooling of persistence managers by the persistence manager factory. Properties getProperties() The return value from this method is a properties object describing the configuration of the factory. 6.3.1 Optional feature support Since the JDO specification contains a large number of optional features, a means is required by which a particular implementation s support for optional features can be determined. This is achieved through the factory s supportedOptions()method. Collection supportedOptions() The return value of this method is a collection comprised of string objects. JDO defines a set of strings that may be present in the returned collection to indicate support for the corresponding optional feature. For example, if the implementation supports application identity, the string “javax.jdo.option.ApplicationIdentity” will be present in the collection. If the implementation supports datastore identity, the String “javax.jdo.option.DatastoreIdentity” will be present in the collection. (Recall that to be JDO compliant, an implementation must support at least one of these.) The full list of supported option strings is provided in Appendix B.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services