<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2325340321512556304</id><updated>2024-10-18T05:51:49.700-07:00</updated><title type='text'>Performance Optimization of Hibernate Application</title><subtitle type='html'>It describes Hibernate Performance improvement with hibernate first and second level cache and fetching strategy. It also describes how to improve the performance with the proper use of available techniques.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://advanced-hibernate.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2325340321512556304/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://advanced-hibernate.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>NITISH</name><uri>http://www.blogger.com/profile/00692446908573432779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2325340321512556304.post-8534546677642765263</id><published>2010-11-28T07:10:00.000-08:00</published><updated>2011-03-14T22:24:00.636-07:00</updated><title type='text'>Performance Monetoring and Optimization of Hibernate Application</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h1&gt;Performance Optimization of Application&lt;br /&gt;
&lt;/h1&gt;&lt;br /&gt;
Now a days, enterprise systems are getting developed with various &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_0&quot;&gt;ORM&lt;/span&gt; tools like Hibernate, but if we use these tools incorrectly, it may degrade the system. We will see how to improve the performance of the system developed with one of the most popular &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_1&quot;&gt;ORM&lt;/span&gt; tool -Hibernate.&lt;br /&gt;
Following Diagram shows by what all ways hibernate application performance can be improved.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUDkXw6nqJdfw6YacTOi2IzQRYL06EQT_XHFGdZtG046rybHUWG-mKXQ24r9k0HQwLgiTaxjQjaeMxpF9H1Er3M0U0tlF8RfIGqxSR8Eu48mrI3UnOn9m9j381WrfNH-HEcmcdVQAvP1U/s1600/HibernatePerformance.jpg&quot; onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; id=&quot;BLOGGER_PHOTO_ID_5549008093170133090&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUDkXw6nqJdfw6YacTOi2IzQRYL06EQT_XHFGdZtG046rybHUWG-mKXQ24r9k0HQwLgiTaxjQjaeMxpF9H1Er3M0U0tlF8RfIGqxSR8Eu48mrI3UnOn9m9j381WrfNH-HEcmcdVQAvP1U/s400/HibernatePerformance.jpg&quot; style=&quot;cursor: pointer; display: block; height: 282px; margin: 0px auto 10px; text-align: center; width: 400px;&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;h2&gt;Using Hibernate Caching&lt;/h2&gt;&lt;br /&gt;
A cache is the way to reduce traffic between your application and the database by conserving data already loaded from the database with the principle that database gets accessed only when retrieving data that is not currently available in the cache.&lt;br /&gt;
&lt;br /&gt;
There are three types of cache available with Hibernate&lt;br /&gt;
&lt;blockquote&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;First Level Cache&lt;/li&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;Second Level Cache&lt;/li&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;Query Cache&lt;/li&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/blockquote&gt;We will see it one by one in the below section.&lt;br /&gt;
&lt;h2&gt;First Level Cache&lt;br /&gt;
&lt;/h2&gt;Cache associated with the Hibernate Session is first level cache, that means, whatever operations you do in single hibernate session gets executed in one query. &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_2&quot;&gt;Wel&lt;/span&gt;, its not true, actually it means, whatever database operations you perform in single hibernate session, Hibernate tries to optimize the generated &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_3&quot;&gt;SQL&lt;/span&gt; for you and hence reducing the database traffic. When you load the same data from multiple points in single session, hibernate actually fires only one query to retrieve data and for subsequent calls, you get the data from Session cache. Same happens if you are updating something multiple times in one session, there will be only one update query to database at the end of transaction containing all the modifications.&lt;br /&gt;
Also single session of hibernate represents one transaction to the database so you need not to commit operations explicitly(remove your extra useless lines of code :) ).&lt;br /&gt;
&lt;h2&gt;Second-Level Cache&lt;/h2&gt;&lt;br /&gt;
Second-level cache keeps loaded objects at the Session Factory level  across transaction boundaries. The objects in the second-level cache are available to the whole application, not just to the user running the query. This way, each time a query returns an object that is already loaded in the cache,  transactions potentially gets avoided&lt;br /&gt;
The application may need to empty (invalidate) the cache from time to time if the database is updated or modified in some way, because it has no way of knowing whether the cache is up to date, again that can be configured with different strategies of second level caching.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Read-only&lt;/li&gt;
&lt;li&gt;Read/write&lt;/li&gt;
&lt;li&gt;Non-strict read/write&lt;/li&gt;
&lt;li&gt;Transactional&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Read-Only Cache&lt;/h3&gt;Useful for data that is read frequently but never updated.&lt;br /&gt;
Simplest and best-performing cache strategy.&lt;br /&gt;
&lt;h3&gt;Read/write Cache&lt;/h3&gt;Appropriate if your data needs to be updated.&lt;br /&gt;
They carry more overhead than read-only caches&lt;br /&gt;
&lt;h3&gt;Non-strict Read/write Cache&lt;/h3&gt;Does not guarantee that two transactions won&#39;t simultaneously modify the same data.&lt;br /&gt;
Therefore, it may be most appropriate for data that is read often but only occasionally modified&lt;br /&gt;
&lt;h3&gt;Transactional Cache&lt;/h3&gt;A fully transactional cache that may be used only in a &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_4&quot;&gt;JTA&lt;/span&gt; environment&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Query Cache&lt;/h2&gt;Query result sets may also be cached. This is only useful for queries that are run frequently with the same parameters. Use it when you need to cache actual query results, rather than just persistent objects.&lt;br /&gt;
&lt;h2&gt;Fetching Strategy&lt;/h2&gt;&lt;h3&gt;Join Fetching&lt;/h3&gt;Hibernate retrieves the associated instance or collection in the same SELECT, using an OUTER JOIN.&lt;br /&gt;
&lt;h3&gt;Select Select Fetching&lt;/h3&gt;A second SELECT is used to retrieve the associated entity or collection. Unless you explicitly disable lazy fetching by specifying lazy=&quot;false&quot;, this second select will only be executed when you actually access the association.&lt;br /&gt;
&lt;h3&gt;Sub-select Fetching&lt;/h3&gt;A second SELECT is used to retrieve the associated collections for all entities retrieved in a previous query or fetch. Unless you explicitly disable lazy fetching by specifying lazy=&quot;false&quot;, this second select will only be executed when you actually access the association.&lt;br /&gt;
&lt;h3&gt;Batch Bach&lt;/h3&gt;An optimization strategy for select fetching ‐ Hibernate retrieves a batch of entity instances or collections in a single SELECT, by specifying a list of primary keys or foreign keys&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;Performance Monitoring&lt;/h1&gt;To improve application performance, you will need to first monitor it.&lt;br /&gt;
Hibernate provides a full range of figures about its internal operations. Statistics in Hibernate are available per &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_5&quot;&gt;SessionFactory&lt;/span&gt;. Don&#39;t forget to use these &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_6&quot;&gt;APIs&lt;/span&gt; before making any changes to your application for improving its performance to know the bottleneck.&lt;br /&gt;
&lt;h3&gt;Summary&lt;br /&gt;
&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Check if really hibernate needed for particular  or it can be better done with &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_7&quot;&gt;SQL&lt;/span&gt;.&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Decide appropriate fetch strategy for your tasks&lt;/li&gt;
&lt;li&gt;Overuse of cache can be dangerous, use appropriate cache strategy where needed.&lt;br /&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;&lt;script src=&quot;http://www.assoc-amazon.com/s/link-enhancer?tag=performanc09d-20&amp;amp;o=1&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;&lt;iframe src=&quot;http://rcm.amazon.com/e/cm?t=performanc09d-20&amp;o=1&amp;p=8&amp;l=bpl&amp;asins=1932394885&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;m=amazon&amp;lc1=0000FF&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr&quot; style=&quot;align:left;padding-top:5px;width:131px;height:245px;padding-right:10px;&quot;align=&quot;left&quot; scrolling=&quot;no&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;&lt;iframe src=&quot;http://rcm.amazon.com/e/cm?t=performanc09d-20&amp;o=1&amp;p=8&amp;l=bpl&amp;asins=193239415X&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;m=amazon&amp;lc1=0000FF&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr&quot; style=&quot;align:left;padding-top:5px;width:131px;height:245px;padding-right:10px;&quot;align=&quot;left&quot; scrolling=&quot;no&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;&lt;iframe src=&quot;http://rcm.amazon.com/e/cm?t=performanc09d-20&amp;o=1&amp;p=8&amp;l=bpl&amp;asins=1933988649&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;m=amazon&amp;lc1=0000FF&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr&quot; style=&quot;align:left;padding-top:5px;width:131px;height:245px;padding-right:10px;&quot;align=&quot;left&quot; scrolling=&quot;no&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2325340321512556304/posts/default/8534546677642765263'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2325340321512556304/posts/default/8534546677642765263'/><link rel='alternate' type='text/html' href='http://advanced-hibernate.blogspot.com/2010/11/performance-optimization-and.html' title='Performance Monetoring and Optimization of Hibernate Application'/><author><name>NITISH</name><uri>http://www.blogger.com/profile/00692446908573432779</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUDkXw6nqJdfw6YacTOi2IzQRYL06EQT_XHFGdZtG046rybHUWG-mKXQ24r9k0HQwLgiTaxjQjaeMxpF9H1Er3M0U0tlF8RfIGqxSR8Eu48mrI3UnOn9m9j381WrfNH-HEcmcdVQAvP1U/s72-c/HibernatePerformance.jpg" height="72" width="72"/></entry></feed>