7.5 Multi-Threaded Applications


Many of the examples in the book so far have been of the simplest type of JDO application. The application runs, gets a PersistenceManager instance from a PersistenceManagerFactory , begins a transaction, does something, and ends the transaction. This is a single-threaded, single PersistenceManager application.

JDO supports much more sophisticated applications than this. An application can essentially get as many PersistenceManager instances at a time as available resources allow. Each PersistenceManager instance represents a connection to the datastore, a transaction context, and a cache of persistent objects. From the application perspective, each PersistenceManager instance is independent of the other, and from the datastore perspective, each PersistenceManager instance is essentially a separate user . An application can be multi-threaded and concurrently access the datastore using different PersistenceManager instances.

7.5.1 Multi-threaded programming

When using multiple threads, a developer must give thought to how threads interact with a PersistenceManager instance. A PersistenceManager instance is essentially a shared resource; therefore, care needs to be taken when multiple threads concurrently access it.

In fact, not only is the PersistenceManager instance itself a shared resource, but because the PersistenceManager acts as a factory for Query , Extent , and Transaction instances, any of those instances are likewise shared resources. Also, any persistent objects associated with the PersistenceManager instance are shared resources.

If multiple threads are going to concurrently access a PersistenceManager instance or any associated instances ( Query , Extent , and so on), then the Multitheaded property of the PersistenceManager instance needs to be true. This ensures that the JDO implementation synchronizes internally and that concurrent access doesn't cause internal data structures to be corrupted. However, this synchronization may add additional overhead.

Beyond this, the application must ensure that multiple threads interact with the PersistenceManager instance correctly. Although multiple threads can access persistent objects at the same time, it is still the application's responsibility to synchronize this access. This is no different from normal multi-threaded programming in which multiple threads are concurrently accessing the same Java objects. Also, care must be taken when using various methods on PersistenceManager that affect the lifecycle states of persistent objects. For example, an application must ensure that persistent objects being evicted or refreshed by one thread are not being used by another.

When completing a transaction, the lifecycle states of all persistent objects associated with the PersistenceManager instance change (they all transition to being "hollow"). An application must ensure that only one thread at a time calls commit or rollback and that other threads are not accessing at this time any persistent objects associated with the PersistenceManager instance.

7.5.2 Simplified multi-threaded programming

Although an application may be multi-threaded, if there is only a single thread accessing a PersistenceManager instance or its associated instances at a given point, then as far as the JDO runtime is concerned , this is equivalent to the application being single-threaded. Because each PersistenceManager instance is independent of any other, multiple threads can concurrently access different PersistenceManager instances without conflict, and the Multithreaded PersistenceManager property does not need to be true.



Core Java Data Objects
Core Java Data Objects
ISBN: 0131407317
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net