tag:blogger.com,1999:blog-17423576342031794552024-02-02T05:45:09.414-08:00Behold The ApocalypseThis blog primarily is an attempt to write down all the eye opening moments I continually have while in pursuit of learning technology. There are times when it’s a moment of revelation - uncovering or in ancient Greek “Apocalypse”.
Unknownnoreply@blogger.comBlogger10125tag:blogger.com,1999:blog-1742357634203179455.post-65859944642294310502013-02-07T23:31:00.000-08:002013-02-07T23:31:22.143-08:00Hibernate fetching optimizations – Batch prefetching<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
There are many optimizations for data fetching in Hibernate.<o:p></o:p></div>
<div class="MsoNormal">
One of the not so common method used is <b>batch prefetching</b>.<o:p></o:p></div>
<div class="MsoNormal">
By default Hibernate utilizes proxy objects as placeholders
for associations and collections.<o:p></o:p></div>
<div class="MsoNormal">
For collections Hibernate uses its own collection wrapper
implementations which acts as smart collections but the entities loaded in the
collections are by default also proxies.<o:p></o:p></div>
<div class="MsoNormal">
These proxy objects just have their id set and they only load
their all properties by send the query to database only on property access
other than their id.<o:p></o:p></div>
<div class="MsoNormal">
Let’s say we have a Department entity and Employee entity
with one to many relations. Each Department has many employees and for sake of
simplicity each employee belongs to only one department<o:p></o:p></div>
<div class="MsoNormal">
We now load all departments like this<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<pre class="brush: java">
List allDeps= session.createQuery("from Department").list();
</pre>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Imagine if we have 10 departments, then this will result in
a list of size 10. Further assume that in each of the department we have 10
employees each and we need to access the salary of each employee in this list. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<pre class="brush: java">
for (Department dep : allDeps){
printSalaryForEachEmployees(dep);
}
</pre>
<pre class="brush: java">
printSalaryForEachEmployees(Department dep){
for (Employee emp : dep.getEmployees()){
System.out.println(emp.getSalary);
}
</pre>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The print method will iterate through each of the employee
in each department and call the getSalary() method. This call will initialize
the Employee proxy and each call will send a SQL select to database.<o:p></o:p></div>
<div class="MsoNormal">
All in all, this one use-case will send 1+ (10 x 10) =101 selects to database which is horrible number.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This is also worst case for infamous problem of <b>n+1</b> selects. In our use
case it becomes (n x n+1).<o:p></o:p></div>
<div class="MsoNormal">
There are many ways to optimize this.<o:p></o:p></div>
<div class="MsoNormal">
Today we are looking into one of the methods given by
Hibernate called <b>batch prefetching</b>.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
It works like this.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Hibernate can prefetch the employee by initializing its
proxies beforehand. This is how it is mentioned in the configuration.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<pre class="brush: java">
<class name="Employee"
table="Employee"
batch-size="10">
...
</class>
</pre>
<div class="MsoNormal">
This tells Hibernate that if it is using proxies for
Employees( which by default Hibernate does), then on the the first
initialization of single proxy, automatically initialize<b> upto </b>10 proxies even before their property access. If there are more
than 10 proxies then on the access of 11<sup>th</sup> proxy preload another 10
proxies until the there are no proxies left.<o:p></o:p></div>
<div class="MsoNormal">
Understandably this kind of optimization is referred to as
blind-guess optimization by Hibernate as you don’t know beforehand how many
proxies are there.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
This optimization can also be applied for collections:<o:p></o:p></div>
<pre class="brush: java">
<class name="Department" table="Department">
...
<set name="employees"
inverse="true"
batch-size="10">
<key column="EMPLOYEE_ID"/>
<one-to-many class="Employee"/>
</set>
</class>
</pre>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
So for our use case since we have 10 departments in the
list, the moment we initialize collection of employees for one of the department
object it now initializes 10 more employees’ collections of 10 Departments, all
by using a single select something this:<o:p></o:p></div>
<pre class="brush:java">
select e.* from Employee e
where e.DEPARTMENT_ID in (?, ?, ?,?,?,?,?,?,?,?)
</pre>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1742357634203179455.post-89868002879686379852013-01-29T03:28:00.000-08:002013-01-29T03:33:28.477-08:00RESTful url - few tips<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
Many beginners associate REST url’s with url having some
identifier to identify the resource even if the identifier is present in the
url parameter.</div>
<div class="MsoNormal">
For e.g.,<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>http://www.myshop.com/shopping/products?productid=1</b><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here the url seems to be identifying a resource (product)
with an identifier (productid) 1<o:p></o:p></div>
<div class="MsoNormal">
However technically this is not a RESTful url.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
REST ways of looking at resources says that the web is a web
of information resources on which you can take standard actions. HTTP is a good example of REST with the resources
identified by url’s and the standard actions on those resources are GET, POST,
PUT, and DELETE. The most predominant operations done in HTTP is to get the
resource representation using GET and make updates to it using POST.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
A resource oriented RESTful url must serve as an unique identifier without
the additional parameters, meaning the url itself should have the identifier in
itself, something like this<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>http://www.myshop.com/shopping/products/1</b><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Another important distinction is that the url must not
include any verb rather only nouns because the verbs (actions that could be
performed) are already defined as standard actions. For e.g., have a look at
this url.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>http://www.myshop.com/shopping/getproducts/1</b><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Some frameworks might make use of this kind of url to map a
method “getproducts” in the controller and pass the identifier 1. However this
cannot be taken as a true resource identifier as it includes a part “getproducts”
which has nothing to do to identify a resource. So in essence a RESTful url
should be a resource oriented url which uniquely identifies a resource.<o:p></o:p></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1742357634203179455.post-67349063720059657282013-01-28T09:57:00.004-08:002013-01-28T23:56:31.812-08:00Spring MVC Framework - SessionAttributes annotation<div dir="ltr" style="text-align: left;" trbidi="on">
<!--[if gte mso 9]><xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
</o:OfficeDocumentSettings>
</xml><![endif]--><br />
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val="Cambria Math"/>
<m:brkBin m:val="before"/>
<m:brkBinSub m:val="--"/>
<m:smallFrac m:val="off"/>
<m:dispDef/>
<m:lMargin m:val="0"/>
<m:rMargin m:val="0"/>
<m:defJc m:val="centerGroup"/>
<m:wrapIndent m:val="1440"/>
<m:intLim m:val="subSup"/>
<m:naryLim m:val="undOvr"/>
</m:mathPr></w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
DefSemiHidden="true" DefQFormat="false" DefPriority="99"
LatentStyleCount="267">
<w:LsdException Locked="false" Priority="0" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Normal"/>
<w:LsdException Locked="false" Priority="9" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="heading 1"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/>
<w:LsdException Locked="false" Priority="39" Name="toc 1"/>
<w:LsdException Locked="false" Priority="39" Name="toc 2"/>
<w:LsdException Locked="false" Priority="39" Name="toc 3"/>
<w:LsdException Locked="false" Priority="39" Name="toc 4"/>
<w:LsdException Locked="false" Priority="39" Name="toc 5"/>
<w:LsdException Locked="false" Priority="39" Name="toc 6"/>
<w:LsdException Locked="false" Priority="39" Name="toc 7"/>
<w:LsdException Locked="false" Priority="39" Name="toc 8"/>
<w:LsdException Locked="false" Priority="39" Name="toc 9"/>
<w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/>
<w:LsdException Locked="false" Priority="10" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Title"/>
<w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/>
<w:LsdException Locked="false" Priority="11" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/>
<w:LsdException Locked="false" Priority="22" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Strong"/>
<w:LsdException Locked="false" Priority="20" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/>
<w:LsdException Locked="false" Priority="59" SemiHidden="false"
UnhideWhenUsed="false" Name="Table Grid"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/>
<w:LsdException Locked="false" Priority="1" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 1"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 1"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 1"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/>
<w:LsdException Locked="false" Priority="34" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/>
<w:LsdException Locked="false" Priority="29" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Quote"/>
<w:LsdException Locked="false" Priority="30" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 1"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 1"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 2"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 2"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 2"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 2"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 2"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 3"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 3"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 3"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 3"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 3"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 4"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 4"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 4"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 4"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 4"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 5"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 5"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 5"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 5"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 5"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 6"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 6"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 6"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 6"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 6"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/>
<w:LsdException Locked="false" Priority="19" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/>
<w:LsdException Locked="false" Priority="21" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/>
<w:LsdException Locked="false" Priority="31" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/>
<w:LsdException Locked="false" Priority="32" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/>
<w:LsdException Locked="false" Priority="33" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Book Title"/>
<w:LsdException Locked="false" Priority="37" Name="Bibliography"/>
<w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/>
</w:LatentStyles>
</xml><![endif]--><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
</style>
<![endif]-->
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">Spring MVC is a highly configurable web framework. It can be configured
using xml or now more popular annotations. Spring MVC is very powerful , however
the number of different ways in which a single thing can be done can be sometimes
overwhelming. I have found it extremely useful to have Spring framework source code in my IDE and go through it whenever I need to know the internals. Of course technical blogs and discussions on StackOverFlow site has always been very helpful. Today I am going to discuss about an important annotation called
SessionAttributes.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">This annotation is declared at type level on the Controller class.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">You specify SessionAttributes annotation like this:</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";">@Controller</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";">@RequestMapping("/editPet.do")</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";">@SessionAttributes("pet")</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";">public
class EditPetForm {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>// ...</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";">}</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">As you can see that you mention just the attribute name. This is a signal
to the framework that whatever object is stored against the attribute named “pet”
in the Model during the processing of the handler, it will be available as long
as the methods are being called in the same controller.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">Now the question is who creates / stores the object corresponding to this
attribute.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">You may remember that a controller may contain a method annotated with
@ModelAttribute which means that the method will be called before any handler method
is called. Also that method stores the return value in the Model against the model
attribute name specified by @ModelAttribute.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">Also a handler method may contain an argument annotated by
@ModelAttribute which means that the framework will inject that attribute from
Model if present or create new. So when you add a @ModelAttribute("pet")
controller method argument you're telling Spring MVC to look for it in the
model or create a new instance if not found. But <span style="mso-spacerun: yes;"> </span>if you also add @SessionAttributes, at
controller class level framewotk will not attempt to create a new instance and
will expect the object to be either in the model or in the session.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">So now armed with this knowledge let’s recreate the steps</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">1. Our controller class is annotated with @SessionAttribute name “pet”</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">2. A method in controller class is annotated with @ModelAttribute named “pet”
and it returns a Pet object usually from database. We do not want to load it
again when next time this method is called.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">3. Because of point 1, the model attribute “pet” is available not only in
the view but it is also stored session so that when again the handler method
annotated with @ModelAttribute(“pet”) is called, it is injected by framework</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">4. Also we would like such conversational attributes to be removed from
session when the conversation ends. To accomplish this we can declare an
argument of type SessionStatus in the handler method and framework will inject
it into the handler method. We can then call setComplete() method on
SessionStatus to indicate that the attributes need to be cleared.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">So then what is the difference between storing an attribute in
HttpSession and SessionAttribute.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">An object stored in http session will be there till the life time of http
session. Now imagine that in the above mapping example for /editPet.do, the “pet
” attribute is a Pet object loaded from database through a method annotated
with @ModelAttribute <span style="mso-spacerun: yes;"> </span>in the controller
class. We probably need the Pet object for the time as long as we are doing
some processing logic for the editing. All such methods doing this processing
logic should ideally be in the same controller.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 5;">
<span style="color: black; font-family: "Segoe UI","sans-serif"; font-size: 9.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman";">So @SessionAttribute along with SessionStatus provides a way to make the “pet” attribute available
while the methods of ONLY this controller are being called. That also means
that session attributes using @SessionAttributes are not available across
controllers while attributes stored in HttpSession are always available. So in nutshell we can say that attributes specified by @SessionAttributes are meant for storing <b>conversation related objects </b>into http session for conversation time period.</span></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1742357634203179455.post-87826846237948209992012-10-17T02:08:00.001-07:002012-10-17T04:35:00.652-07:00Hibernate automatic versioning - Part 1Hibernate can provide automatic versioning for the entities without you doing ANYTHING. By WITHOUT ANYTHING I really mean WITHOUT ANYTHING ! You just have your entity classes and you wnat hibernate to provide versioning so that an update on a already changes object would not happen. This sounds exciting but in actual practise its not that very used. We'll see how and why.<br />
<br />
If you simply want hibernate to provide automatic versioning (without addition of version column or timestamp in the correspoding table and entity) then hibernate can do this but the caveat is that object should be retrieved and modified in the <b>same</b> session or persistence context. This essentially means that the session must not be closed (it can be flushed though to send the changes to database).<br />
<br />
When using this feature, Hibernate checks the current database state of the entity in question and if the earlier state ( when it was retrieved or when the last flush operation was called upon session) is different from the current database state, it concludes that the entity has been modified and throws StaleObjectException.<br />
<br />
To enable this feature on an entity you will have to add attribute optimistic-lock="all" in the entity mapping if you are using xml based configuration or Hibernate specific annotation like below if you are using annotation approach<br />
<br />
<br />
<pre class="brush: java">@Entity
@org.hibernate.annotations.Entity(
.................
optimisticLock = OptimisticLockType.ALL,
........................)
public class TestEntity{ ... }
</pre>
Hibernate team does not recommend using this automatic versioning without version or timestamp column as this is bit slow and complex. Also another bigger disadvantage is that it does not work for detached objects. It works only as long as the session is not closed. For this reason too, this is not effective in long running conversations as well.<br />
<br />
In the next part I will explain about other better way of adding optimistic version control to your entities which uses version or timestamp column to keep track of the entity state.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1742357634203179455.post-78022542713981946182012-10-17T01:23:00.000-07:002013-02-07T23:35:25.249-08:00Resource loading in JSF 2 - Part 1<div dir="ltr" style="text-align: left;" trbidi="on">
UIComponents are central to the functioning and usability of JSF. What you see on the user agent ( for e.g, web browsers) is the markup generated by UIComponent classes and whatever is submitted through browsers in web request is decoded by the UIComponent instances for that particular page. However for any component having slight complexity, how the markup looks like and behaves is dependent on other resources like images, javascript and stylesheet etc. So if a component author provides a component, the framework should have a mechanism of including these resources (preferably within the jar bundling the component) so that components can look and behave as desired. This way components can be self contained.<br />
<br />
Prior to JSF 2 such a mechanism was missing. JSF 2 provides this important missing piece to enable a component to load resources uniformly from class path resources.<br />
<br />
Typically in a web application, how static resource requests are served depends on the mapping in web.xml. Most commonly the web.xml does not contain any specific mappings for extensions of static resources like images, scripts ( for e.g., *.jpg,*.css etc). In that case a Servlet provided by container usually serves up the request. For e.g., in Tomcat such requests are served up by <br />
<br />
How JSF recognizes that a request is for a resource depends upon the existence of string "/javax.faces.resource" in the request url. If this string is present then the JSF main Servlet "FacesServlet" considers this as a resource request and forwards it to default ResourceHandler.</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1742357634203179455.post-69397756976023122272012-10-11T23:41:00.000-07:002012-10-11T23:41:52.341-07:00<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
<b>Hibernate Collections implementations<o:p></o:p></b></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
So your Hibernate entity has a reference to collection of other entities mapped using Set interface (or for that matter any other collection interface). You load an entity from database and in your debugger inspect the type of the collection Hibernate is using ( assuming it is configured as eagerly loaded. Lazy loading is quite another story for another day). You expect it to be usual java implementation HashSet but you see something as PersistentSet. You are bewildered and scratching your head jump to the Hibernate manuals or different forums. Go no further ! Here is what is happening under the hood !</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Whenever hibernate is used for querying an object, an entity
is returned which is managed by Hibernate as long as the session in which it
was queried is open. Behind the scenes Hibernate queries the database and
builds the entity object. The entity might contain associations in form of
Single object association or Collections. In case of collections, hibernate
creates its own implementation of collection. For e.g., for a List type it
creates a wrapper list Persistent List. All the java collections like Set,
HashMap etc. have their corresponding implementaions in Hibernate.<o:p></o:p></div>
<div class="MsoNormal">
The base class for all these hibernate implementations is
AbstractPersistentCollection as you can see below in the type hierarchy.<o:p></o:p></div>
<div class="MsoNormal">
You will never have to directly deal with these Hibernate classes however it's good to understand the inner working of collection mapping.</div>
<div class="MsoNormal">
<!--[if gte vml 1]><v:shapetype
id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t"
path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
<v:stroke joinstyle="miter"/>
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0"/>
<v:f eqn="sum @0 1 0"/>
<v:f eqn="sum 0 0 @1"/>
<v:f eqn="prod @2 1 2"/>
<v:f eqn="prod @3 21600 pixelWidth"/>
<v:f eqn="prod @3 21600 pixelHeight"/>
<v:f eqn="sum @0 0 1"/>
<v:f eqn="prod @6 1 2"/>
<v:f eqn="prod @7 21600 pixelWidth"/>
<v:f eqn="sum @8 21600 0"/>
<v:f eqn="prod @7 21600 pixelHeight"/>
<v:f eqn="sum @10 21600 0"/>
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
<o:lock v:ext="edit" aspectratio="t"/>
</v:shapetype><v:shape id="Picture_x0020_1" o:spid="_x0000_i1025" type="#_x0000_t75"
style='width:379.2pt;height:280.8pt;visibility:visible;mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\SHAILE~1\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png"
o:title=""/>
</v:shape><![endif]--><!--[if !vml]--><!--[endif]--><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtFIccSGJsNmSUt5gm4p_A7xyL1nhwv6TdnVAoWXvowvc0NlRMFOc791v9X7E3L_dMUhW6Uz7diBA93uZ2QRA-foMa0GeFdmvH3SfRGpnlznsGpbnxRKk1nrA1wn2SSNXJ2kFuvhnYSXHJ/s1600/heirarchy.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="236" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtFIccSGJsNmSUt5gm4p_A7xyL1nhwv6TdnVAoWXvowvc0NlRMFOc791v9X7E3L_dMUhW6Uz7diBA93uZ2QRA-foMa0GeFdmvH3SfRGpnlznsGpbnxRKk1nrA1wn2SSNXJ2kFuvhnYSXHJ/s320/heirarchy.png" width="320" /></a></div>
<div class="MsoNormal">
<o:p><br /></o:p></div>
<div class="MsoNormal">
All these implementations are wrapper over the original
collection in the entity (decorator pattern you see !). There are many things for which Hibernate uses
these implementations classes. One of the things Hibernate uses these implementations
is to mark the collections as dirty. Whenever you add/remove an object in these
collections, it marks the collection dirty and Hibernate can queue the update
query later when it inspects the collection. AbstractPersistentCollection
maintains a Boolean variable “dirty” to indicate this and it is updated
whenever there is any change to the collection via its interface.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1742357634203179455.post-9837089849083615912012-09-04T02:54:00.002-07:002012-09-04T05:07:19.924-07:00My experiences with Hibernate - Part 1<div dir="ltr" style="text-align: left;" trbidi="on">
Hibernate is arguably the most popular ORM tool which is not only widely used and well documented but also motivator for other ORM standards like JPA. There is one side of camp which hates ORM in general (championed by Ted Neward in early 2006 and propagated by his article "<a class="TitleLinkStyle" href="http://blogs.tedneward.com/2006/06/26/The+Vietnam+Of+Computer+Science.aspx" rel="bookmark" style="background-color: #fcfcfc; color: black; font-family: Arial, Tahoma, Verdana; font-size: 11pt; font-weight: bold; text-decoration: none;">The Vietnam of Computer Science</a>"). I read that article long way back in 2007 and thought in horror"My God ! Why should I bother about ORM in general. I am happy with my JDBC and cluttering my code with mapping code". But as things turned out, ORM tools slowly gained ground and now a days majority of Enterprise applications use some sort of ORM to speed up the development work. So I begin to have a taste of Hibernate during work and personal projects. Hibernate does a great job when you begin to appreciate the impedance mismatch betweeen object model and relational model it's trying to solve ( though that's impossible to completely solve. To know why read the article mentioned above). Also its well documented and an exceptionally well written book "Hibernate in Action" (now in second edition with title "Java Persistence with Hibernate"). The mailing list is helpful and there are lots of sample examples.<br />
However while advocating Hibernate to anyone I had a feeling inside and I could not give word to that until one day when I stumbled upon a phrase by ever popular Joel. He mentioned about "<a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html" target="_blank">Leaky Abstractions</a>" and when I read that I was convinced that is what ORM and in general Hibernate is. In summary what "Leaky Abstraction" refers to is an abstraction which promised to act like an abstraction to its users but behind the scenes requires the users to understand about its details ( which is violation of the principle of abstraction).<br />
I am not taking away anything from Hibernate and maintain that it a well written tool full of features. But just a word of caution. If you think you just need to know what to use in Hibernate for use case, you will shoot yourself in your foot. You have to keep an eye on what hibernate is doing behind the scene and how to use it myriads of configuration settings. If you do not you will loose several hours in debugging it. I would suggest if you foresee yourself using Hibernate or JPA for a considerable time in your projects, then spend some time in soaking in some basic and advanced concepts from the book "Java persistence with Hibernate". You won't regret this.<br />
<br />
I coming weeks I would be listing some of the concepts in Hibernate which are bit confusing and at times misleading. These posts would not be necessarily long ones and some may even be bullet points but they will be helpful to understand more about the inner workings of Hibernate.</div>
Unknownnoreply@blogger.com4tag:blogger.com,1999:blog-1742357634203179455.post-18391561061535520322012-09-04T02:03:00.000-07:002012-10-17T01:24:12.229-07:00JTA Internals - Part 1<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Georgia, Times New Roman, serif;">Transaction management is one of the most important part of design of an application </span><span style="font-family: Georgia, 'Times New Roman', serif;">which requires adherence to ACID properties.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">Java EE provides JTA specification to manage distributed transactions. However there are many popular framworks like Spring framework which provide integration to JTA as well as old school JDBC (</span><span style="background-color: white;"><span style="font-family: Georgia, Times New Roman, serif;">native database transactions through the JDBC Connection API - more commonly known as resource local transaction</span></span><span style="font-family: Georgia, 'Times New Roman', serif;">). On the top of it if you are using an ORM (like Hibernate) then there is another layer on top of that. Couple that with concepts of transactional second level caches in ORM's like Hibernate in JTA environment. So just knowing about the ACID properties of transaction is not enough to understand the infrastructure. Also as always if you need to debug any transaction related issue you need to have knowledge of what goes on behind the scenes.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">For those who are not satisfied with just using an API but want to uncover the internals I will be writing up a bit of detailed information in the coming weeks. This is the first part of the series.</span>
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span><br />
<span style="font-family: Georgia, Times New Roman, serif;">JTA (Java Transaction API) is a part of Java EE specification and is defined as </span><br />
<blockquote>
<span style="font-family: Georgia, Times New Roman, serif;">standard Java interfaces between a transaction manager and the parties involved in a distributed transaction system: the resource manager, the application server, and the transactional applications.</span></blockquote>
<span style="font-family: Georgia, Times New Roman, serif;">Let's see each of the parties here in a bit more details.
But before that it would be really good to explain a bit about some key interfaces supplied by JTA (there are only very few interfaces to name).
</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<a href="http://docs.oracle.com/javaee/5/api/javax/transaction/UserTransaction.html" target="_blank"><span style="font-family: Georgia, Times New Roman, serif;"><b>UserTransaction</b></span></a><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">This is the interface used by <b>your application code</b> to manage the transaction boundaries and commit and rollback.
In application server environment, the implementation of this interface is bound usually to jndi and the code can use lookup or dependency injection to get hold of this.
</span><br />
<pre class="brush: java">UserTransaction ut = getUserTransaction();
ut.begin();
......
ut.commit();
} catch (Exception e) {
ut.rollback();
}
</pre>
</div>
<span style="font-family: Georgia, Times New Roman, serif;"><a href="http://docs.oracle.com/javaee/5/api/javax/transaction/TransactionManager.html"><b>TransactionManager</b></a></span><br />
<br />
<span style="font-family: Georgia, Times New Roman, serif;">This is the interface used by the <b>application server code</b> which provides the infrastructure related to transaction management. Your application can be configured to use the application server transaction facility using <b><i>declarative approach</i></b> - meaning you tell your application server through configuration that your application code requires transactions at such and such place. To begin and end the transaction, the application server uses code which is similar to the above but this code is hidden in application server code managing the transaction infrastructure so your classes don't have to write this.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;">Usually the implementation for UserTransaction and TransactionManager Interfaces are done in the same implementation class as there are just few methods extra in TransactionManager interface (though this may vary from implementations)</span><br />
<span style="font-family: Georgia, Times New Roman, serif;">Also this implementaion is bound to jndi from where it is looked up by the application server.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><a href="http://docs.oracle.com/javaee/5/api/javax/transaction/Transaction.html" target="_blank">Transaction</a></b></span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">This is the interface representing the transaction associated with a current thread.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;">It is crucial to understand a bit of details of how this is used although you wont be directly using this interface in your code. Also it will help you in understanding what happens under the covers when your application uses other framework layers such as Spring and Hibernate.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">So in either case ( programmatic or declarative transaction management), when a transaction begins ( either by your application code calling UserTransaction begin method or TransactionManager </span><span style="font-family: Georgia, 'Times New Roman', serif;">begin method is called by application server code), a Transaction object is created and bound to the current thread using Threadlocal variable approach. If there is an active Transaction object bound to the current thread then a transaction is said to be in progress. Any transaction aware code can check the existence of this transaction to see if there is a transaction in progress. For e.g., when a connection is requested from a transaction aware implementation of DataSource , it checks the existence of a transaction and enlists the resource in transaction using Transaction interface enlist method.</span><br />
<span style="font-family: Georgia, 'Times New Roman', serif;"><br /></span><span style="font-family: Georgia, 'Times New Roman', serif;">This check is also essential for many operations in transaction and more specifically when deciding for transaction propagation "required". For required propagation, if there is a transaction already in progress in the current thread then no new transaction is created.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1742357634203179455.post-79260640239468129312010-07-08T01:26:00.000-07:002010-07-08T01:34:53.945-07:00Utility methods from FrameworksEver project invariably needs several utility logic for various tasks( a very simple example would be utilities for String/array/ manipulations, XML reading/writing etc.).<br />Being a follower of "don't reinvent the wheel", I have found it many times useful to go through the utility classes used internally by various frameworks like Struts, Hibernate,Spring Framework,JSF etc for things which I can use in my project.<br />A nice habit is to have the source code of these frameworks in my IDE and occasionally go through each of the utility files.I have to admit many times I have got better code for a particulat task than the one I could write on my own.More often than not, it was other way round too. But overall very helpful in the end and it enhanced my learning as well.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1742357634203179455.post-11894101807535433672010-07-06T22:47:00.000-07:002010-07-07T05:23:38.891-07:00Detailed JSF Life cycleThere are plethora of articles on net which explain the JSF Life cycle. If you know the JSF life cycle superficially then I would suggest you to look at <span style="font-style:italic;"><a href="http://www.softwareengineeringsolutions.com/thoughts/frameworks/JSF-lifecycle.htm">this</a></span> article. Personally I prefer to understand about a framework by going through documentation and source code. Especially in case of JSF I found it very useful as the lifecycle is bit complex and it gives you a lot of points where you can plug in your custom application logic, so if you want to understand and master the internals of the framework then get the source code of the JSF implementation (myfaces, mojarra etc) you are using and go through <a href="http://www.softwareengineeringsolutions.com/thoughts/frameworks/JSF-lifecycle.htm">this</a> article. Thanks to Damodar Chetty for <a href="http://www.softwareengineeringsolutions.com/thoughts/frameworks/JSF-lifecycle.htm">this article</a>.<br />Another <a href="http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html">excellent article</a> is by ever amazing BaluC on JSF lifecycle.Unknownnoreply@blogger.com0