Archive for February, 2008

Caching fundamentals remotely by value. A cluster scope (Web hosting mysql)

Saturday, February 16th, 2008

Caching fundamentals remotely by value. A cluster scope cache therefore can t guarantee identity across a cluster. For typical web or enterprise application architectures, it s most convenient that the scope of object identity be limited to a single unit of work. In other words, it s neither necessary nor desirable to have identical objects in two concurrent threads. In other kinds of applications (including some desktop or fat-client architectures), it may be appropriate to use process scoped object identity. This is particularly true where memory is extremely limited the memory consumption of a unit of work scoped cache is proportional to the number of concurrent threads. However, the real downside to process-scoped identity is the need to synchronize access to persistent instances in the cache, which results in a high likelihood of deadlocks and reduced scalability due to lock contention. Caching and concurrency Any ORM implementation that allows multiple units of work to share the same persistent instances must provide some form of object-level locking to ensure synchronization of concurrent access. Usually this is implemented using read and write locks (held in memory) together with deadlock detection. Implementations like Hibernate that maintain a distinct set of instances for each unit of work (unit of work-scoped identity) avoid these issues to a great extent. It s our opinion that locks held in memory should be avoided, at least for web and enterprise applications where multiuser scalability is an overriding concern. In these applications, it usually isn t required to compare object identity across concurrent units of work; each user should be completely isolated from other users. There is a particularly strong case for this view when the underlying relational database implements a multiversion concurrency model (Oracle or PostgreSQL, for example). It s somewhat undesirable for the object/relational persistence cache to redefine the transactional semantics or concurrency model of the underlying database. Let s consider the options again. A transaction/unit of work-scoped cache is preferred if you also use unit of work-scoped object identity and if it s the best strategy for highly concurrent multiuser systems. This first-level cache is mandatory, because it also guarantees identical objects. However, this isn t the only cache you can use. For some data, a second-level cache scoped to the process (or cluster) that returns data by value can be a useful. This scenario therefore has two cache layers; you ll later see that Hibernate uses this approach.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

CHAPTER 13 Optimizing fetching and caching accessed by

Friday, February 15th, 2008

CHAPTER 13 Optimizing fetching and caching accessed by concurrently running threads, obviously with implications on transaction isolation. Cluster scope cache Shared between multiple processes on the same machine or between multiple machines in a cluster. Here, network communication is an important point worth consideration. A process scope cache may store the persistent instances themselves in the cache, or it may store just their persistent state in a disassembled format. Every unit of work that accesses the shared cache then reassembles a persistent instance from the cached data. A cluster scope cache requires some kind of remote process communication to maintain consistency. Caching information must be replicated to all nodes in the cluster. For many (not all) applications, cluster scope caching is of dubious value, because reading and updating the cache may be only marginally faster than going straight to the database. Persistence layers may provide multiple levels of caching. For example, a cache miss (a cache lookup for an item that isn t contained in the cache) at the transaction scope may be followed by a lookup at the process scope. A database request is the last resort. The type of cache used by a persistence layer affects the scope of object identity (the relationship between Java object identity and database identity). Caching and object identity Consider a transaction-scoped cache. It seems natural that this cache is also used as the identity scope of objects. This means the cache implements identity handling: Two lookups for objects using the same database identifier return the same actual Java instance. A transaction scope cache is therefore ideal if a persistence mechanism also provides unit of work-scoped object identity. Persistence mechanisms with a process scope cache may choose to implement process-scoped identity. In this case, object identity is equivalent to database identity for the whole process. Two lookups using the same database identifier in two concurrently running units of work result in the same Java instance. Alternatively, objects retrieved from the process scope cache may be returned by value. In this case, each unit of work retrieves its own copy of the state (think about raw data), and resulting persistent instances aren t identical. The scope of the cache and the scope of object identity are no longer the same. A cluster scope cache always needs remote communication, and in the case of POJO-oriented persistence solutions like Hibernate, objects are always passed
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Managed web hosting - Caching fundamentals Hibernate caching system is the way

Thursday, February 14th, 2008

Caching fundamentals Hibernate caching system is the way it is. We then introduce the Hibernate caching system and show you how to enable, tune, and manage the first- and second- level Hibernate cache. We recommend that you carefully study the fundamentals laid out in this section before you start using the cache. Without the basics, you may quickly run into hard to debug concurrency problems and risk the integrity of your data. Caching is all about performance optimization, so naturally it isn t part of the Java Persistence or EJB 3.0 specification. Every vendor provides different solutions for optimization, in particular any second-level caching. All strategies and options we present in this section work for a native Hibernate application or an application that depends on Java Persistence interfaces and uses Hibernate as a persistence provider. A cache keeps a representation of current database state close to the application, either in memory or on disk of the application server machine. The cache is a local copy of the data. The cache sits between your application and the database. The cache may be used to avoid a database hit whenever The application performs a lookup by identifier (primary key). The persistence layer resolves an association or collection lazily. It s also possible to cache the results of queries. As you ll see in the chapter 15, the performance gain of caching query results is minimal in many cases, so this functionality is used much less often. Before we look at how Hibernate s cache works, let s walk through the different caching options and see how they re related to identity and concurrency. 13.3.1 Caching strategies and scopes Caching is such a fundamental concept in object/relational persistence that you can t understand the performance, scalability, or transactional semantics of an ORM implementation without first knowing what kind of caching strategy (or strategies) it uses. There are three main types of cache: Transaction scope cache Attached to the current unit of work, which may be a database transaction or even a conversation. It s valid and used only as long as the unit of work runs. Every unit of work has its own cache. Data in this cache isn t accessed concurrently. Process scope cache Shared between many (possibly concurrent) units of work or transactions. This means that data in the process scope cache is
Check Tomcat Web Hosting services for best quality webspace to host your web application.

CHAPTER 13 (Hp web site) Optimizing fetching and caching Too

Wednesday, February 13th, 2008

CHAPTER 13 Optimizing fetching and caching Too many SQL statements may be executed. Set fetch=”join” on many-to-one and one-to-one association mappings. In rare cases, if you re absolutely sure, enable fetch=”join” to disable lazy loading for particular collections. Keep in mind that more than one eagerly fetched collection per persistent class creates a product. Evaluate whether your use case can benefit from prefetching of collections, with batches or subselects. Use batch sizes between 3 and 15. After setting a new fetching strategy, rerun the use case and check the generated SQL again. Note the SQL statements and go to the next use case. After optimizing all use cases, check every one again and see whether any global optimization had side effects for others. With some experience, you ll easily be able to avoid any negative effects and get it right the first time. This optimization technique is practical for more than the default fetching strategies; you may also use it to tune HQL and Criteria queries, which can define the fetch plan and the fetching strategy dynamically. You often can replace a global fetch setting with a new dynamic query or a change of an existing query we ll have much more to say about these options in the next chapter. In the next section, we introduce the Hibernate caching system. Caching data on the application tier is a complementary optimization that you can utilize in any sophisticated multiuser application. 13.3 Caching fundamentals A major justification for our claim that applications using an object/relational persistence layer are expected to outperform applications built using direct JDBC is the potential for caching. Although we ll argue passionately that most applications should be designed so that it s possible to achieve acceptable performance without the use of a cache, there is no doubt that for some kinds of applications, especially read-mostly applications or applications that keep significant metadata in the database, caching can have an enormous impact on performance. Furthermore, scaling a highly concurrent application to thousands of online transactions usually requires some caching to reduce the load on the database server(s). We start our exploration of caching with some background information. This includes an explanation of the different caching and identity scopes and the impact of caching on transaction isolation. This information and these rules can be applied to caching in general and are valid for more than just Hibernate applications. This discussion gives you the background to understand why the
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Selecting a fetch strategy Optimization step by step

Tuesday, February 12th, 2008

Selecting a fetch strategy Optimization step by step First, enable the Hibernate SQL log. You should also be prepared to read, understand, and evaluate SQL queries and their performance characteristics for your specific database schema: Will a single outer-join operation be faster than two selects? Are all the indexes used properly, and what is the cache hit-ratio inside the database? Get your DBA to help you with that performance evaluation; only he has the knowledge to decide what SQL execution plan is the best. (If you want to become an expert in this area, we recommend the book SQL Tuning by Dan Tow, [Tow, 2003].) The two configuration properties hibernate.format_sql and hibernate. use_sql_comments make it a lot easier to read and categorize SQL statements in your log files. Enable both during optimization. Next, execute use case by use case of your application and note how many and what SQL statements are executed by Hibernate. A use case can be a single screen in your web application or a sequence of user dialogs. This step also involves collecting the object retrieval methods you use in each use case: walking the object links, retrieval by identifier, HQL, and Criteria queries. Your goal is to bring down the number (and complexity) of SQL statements for each use case by tuning the default fetch plan and fetching strategy in metadata. It s time to define your fetch plan. Everything is lazy loaded by default. Consider switching to lazy=”false” (or FetchType.EAGER) on many-to-one, one-to-one, and (sometimes) collection mappings. The global fetch plan defines the objects that are always eagerly loaded. Optimize your queries and enable eager fetching if you need eagerly loaded objects not globally, but in a particular procedure a use case only. Once the fetch plan is defined and the amount of data required by a particular use case is known, optimize how this data is retrieved. You may encounter two common issues: The SQL statements use join operations that are too complex and slow. First optimize the SQL execution plan with your DBA. If this doesn t solve the problem, remove fetch=”join” on collection mappings (or don t set it in the first place). Optimize all your many-to-one and one-to-one associations by considering if they really need a fetch=”join” strategy or if the associated object should be loaded with a secondary select. Also try to tune with the global hibernate.max_fetch_depth configuration option, but keep in mind that this is best left at a value between 1 and 5.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

CHAPTER 13 Optimizing fetching and caching Global and (Web design templates)

Monday, February 11th, 2008

CHAPTER 13 Optimizing fetching and caching Global and dynamic fetching strategies help you to solve the n+1 selects and Cartesian product problems. Hibernate offers another option to initialize a proxy or a collection that is sometimes useful. Forcing proxy and collection initialization A proxy or collection wrapper is automatically initialized whenever any of its methods are invoked (except for the identifier property getter, which may return the identifier value without fetching the underlying persistent object). Prefetching and eager join fetching are possible solutions to retrieve all the data you d need. You sometimes want to work with a network of objects in detached state. You retrieve all objects and collections that should be detached and then close the persistence context. In this scenario, it s sometimes useful to explicitly initialize an object before closing the persistence context, without resorting to a change in the global fetching strategy or a different query (which we consider the solution you should always prefer). You can use the static method Hibernate.initialize() for manual initialization of a proxy: Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Item item = (Item) session.get(Item.class, new Long(1234)); Hibernate.initialize( item.getSeller() ); tx.commit(); session.close(); processDetached( item.getSeller() ); … Hibernate.initialize() may be passed a collection wrapper or a proxy. Note that if you pass a collection wrapper to initialize(), it doesn t initialize the target entity objects that are referenced by this collection. In the previous example, Hibernate.initalize( item.getBids() ) wouldn t load all the Bid objects inside that collection. It initializes the collection with proxies of Bid objects! Explicit initialization with this static helper method is rarely necessary; you should always prefer a dynamic fetch with HQL or Criteria. Now that you know all the options, problems, and possibilities, let s walk through a typical application optimization procedure.
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Selecting a fetch strategy Figure 13.6 A product

Sunday, February 10th, 2008

Selecting a fetch strategy Figure 13.6 A product is the result of two outer joins with many rows. items in the database, and each item has 20 bids and 5 images you ll see a result- set with possibly 100,000 rows! The size of this result may well be several megabytes. Considerable processing time and memory are required on the database server to create this resultset. All the data must be transferred across the network. Hibernate immediately removes all the duplicates when it marshals the resultset into persistent objects and collections redundant information is skipped. Three queries are certainly faster! You get three queries if you map the parallel collections with fetch=”subselect”; this is the recommended optimization for parallel collections. However, for every rule there is an exception. As long as the collections are small, a product may be an acceptable fetching strategy. Note that parallel single-valued associations that are eagerly fetched with outer-join SELECTs don t create a product, by nature. Finally, although Hibernate lets you create Cartesian products with fetch=”join” on two (or even more) parallel collections, it throws an exception if you try to enable fetch=”join” on parallel collections. The result- set of a product can t be converted into bag collections, because Hibernate can t know which rows contain duplicates that are valid (bags allow duplicates) and which aren t. If you use bag collections (they are the default @OneToMany collection in Java Persistence), don t enable a fetching strategy that results in products. Use subselects or immediate secondary-select fetching for parallel eager fetching of bag collections.
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Web design programs - CHAPTER 13 Optimizing fetching and caching annotation (which

Saturday, February 9th, 2008

CHAPTER 13 Optimizing fetching and caching annotation (which is the default in Java Persistence). Eager join fetching of single- ended associations, unlike eager outer-join fetching of collections, doesn t create a Cartesian product problem. The Cartesian product problem The opposite of the n+1 selects problem are SELECT statements that fetch too much data. This Cartesian product problem always appears if you try to fetch several parallel collections. Let s assume you ve made the decision to apply a global fetch=”join” setting to the bids collection of an Item (despite our recommendation to use global prefetching and a dynamic join-fetching strategy only when necessary). The Item class has other collections: for example, the images. Let s also assume that you decide that all images for each item have to be loaded all the time, eagerly with a fetch=”join” strategy: If you map two parallel collections (their owning entity is the same) with an eager outer-join fetching strategy, and load all Item objects, Hibernate executes an SQL SELECT that creates a product of the two collections: select item.*, bid.*, image.* from ITEM item left outer join BID bid on item.ITEM_ID = bid.ITEM_ID left outer join ITEM_IMAGE image on item.ITEM_ID = image.ITEM_ID Look at the resultset of that query, shown in figure 13.6. This resultset contains lots of redundant data. Item 1 has three bids and two images, item 2 has one bid and one image, and item 3 has no bids and no images. The size of the product depends on the size of the collections you re retrieving: 3 times 2, 1 times 1, plus 1, total 8 result rows. Now imagine that you have 1,000
We recommend high quality webhost to host and run your jsp application: christian web host services.

Selecting a fetch strategy This seems to be

Friday, February 8th, 2008

Selecting a fetch strategy This seems to be an optimization you shouldn t make. Can you really say that whenever an item is needed, all its bids are needed as well ? Fetching strategies in mapping metadata work on a global level. We don t consider fetch=”join” a common optimization for collection mappings; you rarely need a fully initialized collection all the time. In addition to resulting in higher memory consumption, every OUTER JOINed collection is a step toward a more serious Cartesian product problem, which we ll explore in more detail soon. In practice, you ll most likely enable a batch or subselect strategy in your mapping metadata for the bids collection. If a particular procedure, such as this, requires all the bids for each Item in-memory, you modify the initial HQL or Criteria query and apply a dynamic fetching strategy: List allItems = session.createQuery(”from Item i left join fetch i.bids”) .list(); List allItems = session.createCriteria(Item.class) .setFetchMode(”bids”, FetchMode.JOIN) .list(); // Iterate through the collections… Both queries result in a single SELECT that retrieves the bids for all Item instances with an OUTER JOIN (as it would if you have mapped the collection with join=”fetch”). This is likely the first time you ve seen how to define a fetching strategy that isn t global. The global fetch plan and fetching strategy settings you put in your mapping metadata are just that: global defaults that always apply. Any optimization process also needs more fine-grained rules, fetching strategies and fetch plans that are applicable for only a particular procedure or use case. We ll have much more to say about fetching with HQL and Criteria in the next chapter. All you need to know now is that these options exist. The n+1 selects problem appears in more situations than just when you work with lazy collections. Uninitialized proxies expose the same behavior: You may need many SELECTs to initialize all the objects you re working with in a particular procedure. The optimization guidelines we ve shown are the same, but there is one exception: The fetch=”join”setting on or associations is a common optimization, as is a @ManyToOne(fetch = FetchType.EAGER)
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

CHAPTER 13 Optimizing fetching and caching (Web hosting ratings) What you

Thursday, February 7th, 2008

CHAPTER 13 Optimizing fetching and caching What you access is the bids collection of each Item. This collection isn t initialized so far, the Bid objects for each item have to be loaded with an additional query. This whole code snippet therefore produces n+1 selects. You always want to avoid n+1 selects. A first solution could be a change of your global mapping metadata for the collection, enabling prefetching in batches: Instead of n+1 selects, you now see n/10+1 selects to retrieve the required collections into memory. This optimization seems reasonable for an auction application: Only load the bids for an item when they re needed, on demand. But if one collection of bids must be loaded for a particular item, assume that other item objects in the persistence context also need their bids collections initialized. Do this in batches, because it s somewhat likely that not all item objects need their bids. With a subselect-based prefetch, you can reduce the number of selects to exactly two: The first query in the procedure now executes a single SQL SELECT to retrieve all Item instances. Hibernate remembers this statement and applies it again when you hit the first uninitialized collection. All collections are initialized with the second query. The reasoning for this optimization is slightly different: Only load the bids for an item when they re needed, on demand. But if one collection of bids must be loaded, for a particular item, assume that all other item objects in the persistence context also need their bids collection initialized. Finally, you can effectively turn off lazy loading of the bids collection and switch to an eager fetching strategy that results in only a single SQL SELECT:
We recommend high quality webhost to host and run your jsp application: christian web host services.