<?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: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-2057261497563466722</id><updated>2012-01-25T22:35:35.817-08:00</updated><category term='nested property'/><category term='content download'/><category term='PERFORMANCE'/><category term='download'/><category term='javascript'/><category term='client'/><category term='Cache'/><category term='Database'/><category term='Validation'/><category term='DB2'/><category term='client side'/><category term='servlet'/><category term='Logging'/><category term='Update'/><category term='Oval'/><category term='Caching'/><category term='Insert'/><category term='nested properties'/><category term='export'/><title type='text'>Crazy Java</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>14</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-822392330672066727</id><published>2011-10-24T18:28:00.000-07:00</published><updated>2011-10-25T11:08:03.962-07:00</updated><title type='text'>Take a break....</title><content type='html'>&lt;title&gt;Puzzle Game&lt;/title&gt;&lt;br /&gt;&lt;center&gt;&lt;br /&gt;&lt;h2&gt;Puzzle Game&lt;br /&gt;(Arrange the number in ascending order)&lt;/h2&gt;&lt;br /&gt;Hi Friends, just take a break to play this game. &lt;br /&gt;This was developed long time back, thought of sharing with you.&lt;br /&gt;&lt;br /&gt;&lt;applet code="PuzzleGame" codebase="https://sites.google.com/site/biju74testsite/" archive="PuzzleGame.jar?attredirects=0&amp;d=1" width="400" height="200"&gt;&lt;br /&gt;Sorry Your Browser Doesn't Support Applet&lt;br /&gt;&lt;/applet&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Rules:&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Click on the numbered button near to the space to move it into the empty space&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Arrange the buttons in the sorted order as shown below&lt;br /&gt;&lt;br /&gt;&lt;table border="1"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;14&lt;/td&gt;&lt;td&gt;15&lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/center&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-822392330672066727?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/822392330672066727/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2011/10/take-break.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/822392330672066727'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/822392330672066727'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2011/10/take-break.html' title='Take a break....'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-4042901758431561706</id><published>2011-06-15T17:35:00.000-07:00</published><updated>2011-06-15T17:40:30.434-07:00</updated><title type='text'>Apache CXF 2.4.1 is released</title><content type='html'>Hi Friends,&lt;br /&gt;&lt;br /&gt;Apache CXF 2.4.1 is released. &lt;br /&gt;&lt;br /&gt;I would like to share that one of my contributions was released along with this version.&lt;br /&gt;&lt;br /&gt;Please refer to &lt;a href="http://cxf.apache.org/apache-cxf-241-release-notes.html"&gt;[Changelog - CXF-3529]&lt;/a&gt;  for my contribution.&lt;br /&gt;&lt;br /&gt;or  visit &lt;a href="https://issues.apache.org/jira/browse/CXF-3529"&gt;Direct Link&lt;/a&gt; for more details&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-4042901758431561706?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/4042901758431561706/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2011/06/apache-cxf-241-is-released.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/4042901758431561706'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/4042901758431561706'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2011/06/apache-cxf-241-is-released.html' title='Apache CXF 2.4.1 is released'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-1011213395197225166</id><published>2011-03-28T20:58:00.002-07:00</published><updated>2011-03-28T21:18:39.460-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='download'/><category scheme='http://www.blogger.com/atom/ns#' term='client side'/><category scheme='http://www.blogger.com/atom/ns#' term='content download'/><category scheme='http://www.blogger.com/atom/ns#' term='export'/><title type='text'>Exporting or Downloading Client Side (Web Page) Content</title><content type='html'>Couple of days back I came across an interesting stuff, which I thought of sharing with my friends.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Exporting or Downloading client side content into a file.&lt;/span&gt;&lt;br /&gt;Normally we use server side content for downloading. Consider you have a table of data like a Grid and you need to export to excel. Yes, most of us go with a server side component which fetch the data from database and send it to the client side by setting some headers and Content-Disposition to let client/browser know that this is not for rendering rather for download. &lt;br /&gt;On seeing such headers browser will show-up with a "&lt;b&gt;Save&lt;/b&gt;" dialog.&lt;br /&gt;&lt;br /&gt;What if you don't need you create a server side component (may be because the content is already in UI), here come the tricky part.&lt;br /&gt;&lt;br /&gt;Before going into details, have anyone tried using any other &lt;a href="http://en.wikipedia.org/wiki/URI_scheme"&gt;URI Schema&lt;/a&gt; in 'href' attribute of an HTML anchor tag?&lt;br /&gt;&lt;br /&gt;The magic or tricky part is, we have a special URI Schema named 'data'. Using this help to download the client content. For more details on 'data' URI schema, refer to &lt;a href="http://en.wikipedia.org/wiki/Data_URI_scheme"&gt;Data URI Scheme&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To know the fun part, &lt;a href="data:application/vnd.ms-excel;base64,MSwyLDMsNA=="&gt;click here&lt;/a&gt; to download a sample excel file.&lt;br /&gt;&lt;br /&gt;Hope you like this information. &lt;br /&gt;&lt;br /&gt;Before using this, refer to the above DATA URI link to see whether your browser is supported.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-1011213395197225166?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/1011213395197225166/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2011/03/exporting-or-downloading-client-side_1871.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/1011213395197225166'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/1011213395197225166'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2011/03/exporting-or-downloading-client-side_1871.html' title='Exporting or Downloading Client Side (Web Page) Content'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-924406850031409492</id><published>2011-03-15T12:58:00.000-07:00</published><updated>2011-03-15T13:21:15.565-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Oval'/><category scheme='http://www.blogger.com/atom/ns#' term='nested properties'/><category scheme='http://www.blogger.com/atom/ns#' term='Validation'/><category scheme='http://www.blogger.com/atom/ns#' term='nested property'/><title type='text'>Validation of nested properties with Oval</title><content type='html'>Yesterday one of my friend asked me how to do validation for nested property using Oval Framework. As per my knowledge oval doesn't support this. I spend couple of hours googling, finally found two articles &lt;a href="http://http//onjavahell.blogspot.com/2009/05/validation-of-nested-properties-with.html"&gt;Validation of nested properties with Oval by OnJavaHell&lt;/a&gt; and a spring version &lt;a href="http://anydoby.com/jblog/en/java/74"&gt;A better Spring validator for Oval&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;These site gave me some light on how to approach this problem. They talk about having custom validators for Oval, which support nested property validation.&lt;br /&gt;&lt;br /&gt;Here I am moving one step forward to use Oval Constraints itself for validation. Using this approach we need not use Custom Validators, instead you can make use of the Oval Validators itself. Here is what you have to do.&lt;br /&gt;&lt;br /&gt;Step 1: Create a Oval Constraint Annotation &lt;span style="font-weight:bold;"&gt;ValidateNestedProperty&lt;/span&gt;&lt;br /&gt;&lt;div style="BACKGROUND-COLOR:lightgray"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;@Documented&lt;br /&gt;@Retention(RetentionPolicy.RUNTIME)&lt;br /&gt;@Target( { ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD,&lt;br /&gt;  ElementType.TYPE })&lt;br /&gt;@Constraint(checkWith = ValidateNestedPropertyCheck.class)&lt;br /&gt;public @interface ValidateNestedProperty {&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Step 2: Create the validation class &lt;span style="font-weight:bold;"&gt;ValidateNestedPropertyCheck&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="BACKGROUND-COLOR:lightgray"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class ValidateNestedPropertyCheck extends&lt;br /&gt;  AbstractAnnotationCheck&amp;lt;ValidateNestedProperty&amp;gt; {&lt;br /&gt;&lt;br /&gt; /**&lt;br /&gt;  * &lt;br /&gt;  */&lt;br /&gt; private static final long serialVersionUID = 1L;&lt;br /&gt;&lt;br /&gt; @Override&lt;br /&gt; public boolean isSatisfied(Object validatedObject, Object valueToValidate,&lt;br /&gt;   OValContext context, Validator validator) throws OValException {&lt;br /&gt;  if (valueToValidate != null) {&lt;br /&gt;&lt;br /&gt;   if (valueToValidate instanceof Collection&amp;lt;?&amp;gt;) {&lt;br /&gt;    // valueToValidate is a collection&lt;br /&gt;    Collection&amp;lt;?&amp;gt; col = (Collection&amp;lt;?&amp;gt;) valueToValidate;&lt;br /&gt;&lt;br /&gt;    for (Object object : col) {&lt;br /&gt;     List&amp;lt;ConstraintViolation&amp;gt; violations = validator&lt;br /&gt;       .validate(object);&lt;br /&gt;     addViolations(validator, violations);&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;   if (valueToValidate.getClass().isArray()) {&lt;br /&gt;    // valueToValidate is an array&lt;br /&gt;    int length = Array.getLength(valueToValidate);&lt;br /&gt;    for (int i = 0; i &amp;lt; length; i++) {&lt;br /&gt;     Object o = Array.get(valueToValidate, i);&lt;br /&gt;     List&amp;lt;ConstraintViolation&amp;gt; violations =           &lt;br /&gt;                                                               validator.validate(o);&lt;br /&gt;     addViolations(validator, violations);&lt;br /&gt;    }&lt;br /&gt;   } else {&lt;br /&gt;    // valueToValidate is other object&lt;br /&gt;    List&amp;lt;ConstraintViolation&amp;gt; violations = validator&lt;br /&gt;      .validate(valueToValidate);&lt;br /&gt;    addViolations(validator, violations);&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  return true; //value is always true, since we do validation only to nested object.&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private void addViolations(Validator validator,&lt;br /&gt;   List&amp;lt;ConstraintViolation&amp;gt; violations) {&lt;br /&gt;  for (ConstraintViolation constraintViolation : violations) {&lt;br /&gt;   validator.reportConstraintViolation(constraintViolation);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Step 3: Use the annotation for any nested property.&lt;br /&gt;&lt;br /&gt;&lt;div style="BACKGROUND-COLOR:lightgray"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Person {&lt;br /&gt;&lt;br /&gt; @ValidateNestedProperty&lt;br /&gt; private List&amp;lt;Address&amp;gt; address;&lt;br /&gt; &lt;br /&gt; ...&lt;br /&gt; ...&lt;br /&gt;} &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Now you can use the Oval validator like validator.vaidate(person) to perform the validation.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-924406850031409492?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/924406850031409492/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2011/03/validation-of-nested-properties-with.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/924406850031409492'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/924406850031409492'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2011/03/validation-of-nested-properties-with.html' title='Validation of nested properties with Oval'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-8247743245916026471</id><published>2010-07-18T15:50:00.000-07:00</published><updated>2010-07-19T11:27:31.613-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='DB2'/><category scheme='http://www.blogger.com/atom/ns#' term='Insert'/><category scheme='http://www.blogger.com/atom/ns#' term='Database'/><category scheme='http://www.blogger.com/atom/ns#' term='Update'/><category scheme='http://www.blogger.com/atom/ns#' term='PERFORMANCE'/><title type='text'>Performance: Simplifying Insert or Update in DB2 from Java</title><content type='html'>Most of the developers have encountered a situation where they need to insert or update records to database depending on their existence.&lt;br /&gt;&lt;br /&gt;One common way is to apply select query in following format&lt;br /&gt;&lt;br /&gt;SELECT 1 FROM TABLE WHERE &amp;lt;matching conditions=""&amp;gt;&lt;br /&gt;&lt;br /&gt;If this query returns a record then we will apply update statement (assuming the record exist), if not we apply insert statement. This looks really good. But the problem is for each record we need to send two queries either SELECT+INSERT or SELECT+UPDATE.&lt;br /&gt;&lt;br /&gt;Yet another approach which I have seen is, they will either INSERT the record without checking. If in case the statement throws an constraint exception, then they will apply the UPDATE command.&lt;br /&gt;&lt;br /&gt;This is good idea if the matching conditions fields  and table constraint fields are same, it not the insert will not throw any exception :)&lt;br /&gt;&lt;br /&gt;While discussing this to one of my friend, he mentioned they first apply the UPDATE command first. If it returns '0' (Zero), then they will apply INSERT command.&lt;br /&gt;&lt;br /&gt;This looks like good idea and if all the records are for update they are in good state, but what if all the records are for inserts? The performance is still there.&lt;br /&gt;&lt;br /&gt;Today one of my friend, after reading my initial post, he was commenting that we can do the whole thing in a Stored Procedure. Which is also good idea. But my stand was, Stored Procedure is normally used to do a set of operations depending on some parameter, rather than single query execution. Will are planning to have more discussion later and will keep you updated. &lt;br /&gt;&lt;br /&gt;Recently during one of my development I came across same scenario. The database which I was using is DB2. I noticed a '&lt;a href="http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.admin.doc/doc/r0010873.htm"&gt;MERGE&lt;/a&gt;' query which does INSERT/UPDATE in a same query. Looks interesting, but the MERGE is usually used to combine two different tables. It reads the data from SOURCE table, compare with current table. If match found, depending on the given condition, then they do UPDATE else they INSERT.&lt;br /&gt;&lt;br /&gt;But what I was looking for is MERGING on same table. After couple of research finally I end-up with writing a query which does INSERT or UPDATE depending on their existence.&lt;br /&gt;&lt;br /&gt;Here for each record we issue only one command.&lt;br /&gt;&lt;br /&gt;Following is the query which I used.&lt;br /&gt;&lt;br /&gt;&lt;div style="BACKGROUND-COLOR:lightgray"&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;MERGE INTO EMPLOYEE as EMP&lt;br /&gt; USING&lt;br /&gt;  (((SELECT ID,NAME,AGE,SALARY&lt;br /&gt;  FROM CP_WEEKLY_LIFT_FACTOR&lt;br /&gt;  WHERE NAME = 'Joe' and AGE = 35)&lt;br /&gt;        UNION&lt;br /&gt;        (SELECT  -1 as ID,'Joe' as NAME,35 as AGE,10000 as SALARY   FROM SYSIBM.SYSDUMMY1)) &lt;br /&gt;        order by ID desc fetch first 1 row only  -- This is important espc for Update&lt;br /&gt;  ) as EMPDATA&lt;br /&gt; ON &lt;br /&gt;  EMP.ID = EMPDATA.ID&lt;br /&gt;  WHEN MATCHED THEN&lt;br /&gt;  UPDATE SET &lt;br /&gt;  EMP.SALARY = EMPDATA.SALARY &lt;br /&gt;  &lt;br /&gt; WHEN NOT MATCHED THEN&lt;br /&gt;  INSERT (NAME,AGE,SALARY) &lt;br /&gt;  VALUES (EMPDATA.NAME,EMPDATA.AGE,EMPDATA.SALARY)&lt;br /&gt;&lt;/pre&gt;&lt;/DIV&gt;&lt;br /&gt;&lt;br /&gt;What I am doing here is,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SELECT A RECORD DEPENDING ON SOME CONDITION&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;COMBINE THE RESULT WITH ONE MORE SELECT WITH THE VALUES TO INSERT&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SELECT THE FIRST RECORD OUT OF IT&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;IF MATCH THEN update ELSE INSERT&lt;br /&gt;&lt;br /&gt;This query is tested and working in DB2 version. I think Oracle also have something same to this.&lt;br /&gt;&lt;br /&gt;One problem I noticed when using this is, when using Java Batch Statement use the casting CAST(? AS INTEGER) for parameterized fields.&lt;br /&gt;&lt;br /&gt;Hope someone will like this and may use this in their project.&lt;br /&gt;&lt;br /&gt;&lt;/matching&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-8247743245916026471?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/8247743245916026471/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2010/07/performance-simplifying-insert-or.html#comment-form' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/8247743245916026471'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/8247743245916026471'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2010/07/performance-simplifying-insert-or.html' title='Performance: Simplifying Insert or Update in DB2 from Java'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-4603120240434833706</id><published>2010-03-22T13:41:00.000-07:00</published><updated>2010-03-22T13:58:11.885-07:00</updated><title type='text'>Mimicking Out-of-the-Box Management Using the JMX Remote API</title><content type='html'>Good Site to read: http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html#gdfvv&lt;br /&gt;&lt;br /&gt;http://blogs.sun.com/lmalventosa/entry/mimicking_the_out_of_the&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-4603120240434833706?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/4603120240434833706/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2010/03/mimicking-out-of-box-management-using.html#comment-form' title='21 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/4603120240434833706'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/4603120240434833706'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2010/03/mimicking-out-of-box-management-using.html' title='Mimicking Out-of-the-Box Management Using the JMX Remote API'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>21</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-2763576648178529087</id><published>2010-03-12T14:40:00.000-08:00</published><updated>2011-10-06T12:12:57.145-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='Logging'/><category scheme='http://www.blogger.com/atom/ns#' term='Database'/><title type='text'>Logging DB Related Activities (Connection, Statement, Prepared Statement, Callable Statement)</title><content type='html'>Recently I had a chance to review one of my friend's project, which I was surprised to see there was no proper logging done in the DB Level. Most of the logging they have was not proper. So I suggested them to use a Loggable Wrapper for most used DB Components like Connections, Statements, PreparedStatement and CallableStatement. &lt;br /&gt;&lt;br /&gt;As they started creating the wrapper, they also though of displaying the query with data (Mostly in PreparedStatement), which help them to do debugging. &lt;br /&gt;&lt;br /&gt;I know most of them really need this, so I thought of sharing this to you all.&lt;br /&gt;&lt;br /&gt;Here is what I have done,&lt;br /&gt;&lt;br /&gt;1) Create Wrappers for all DB Access Classess like Connections, Statements, PreparedStatement and CallableStatement.&lt;br /&gt;2) Wrap your connection object with the new Connection Wrapper.&lt;br /&gt;&lt;br /&gt;Rest will taken care by the wrappers.&lt;br /&gt;&lt;br /&gt;Note, I have used SQLFormatter which I have mentioned in my previous posts for displaying DB Specific message.&lt;br /&gt;&lt;br /&gt;I have also used some code snippet from &lt;a href="http://www.javaworld.com/javaworld/jw-01-2002/jw-0125-overpower.html"&gt;Overpower the PreparedStatement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="BACKGROUND-COLOR:lightgray"&gt;&lt;br /&gt;//con is the existing connection from DataSource/JDBC&lt;br /&gt;ConnectionFactory.getConnection(con,DBType.DB2);&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="BACKGROUND-COLOR:lightgray"&gt;&lt;br /&gt;public final class ConnectionFactory {&lt;br /&gt; private static final Logger log = Logger.getLogger(ConnectionFactory.class);&lt;br /&gt; &lt;br /&gt; private static final boolean useLoggable = Boolean.parseBoolean(System.getProperty("use.loggable.db","true"));&lt;br /&gt; &lt;br /&gt; static {&lt;br /&gt;  if (useLoggable) {&lt;br /&gt;   log.info("Using Loggable DB API - com.db.sql.log.*");&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private ConnectionFactory() {&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public static synchronized Connection getConnection(Connection connection, DBType dbtype) {&lt;br /&gt;  Connection cfConnection = connection;&lt;br /&gt;  if (useLoggable) {&lt;br /&gt;   cfConnection = new LoggableConnection(connection,dbtype);&lt;br /&gt;  }&lt;br /&gt;  return cfConnection;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="BACKGROUND-COLOR:lightgray"&gt;&lt;br /&gt;public class LoggableConnection implements Connection {&lt;br /&gt;&lt;br /&gt; private Connection connection;&lt;br /&gt; private DBType dbType;&lt;br /&gt;&lt;br /&gt; public LoggableConnection(Connection connection,DBType dbType) {&lt;br /&gt;  this.connection = connection;&lt;br /&gt;  this.dbType = dbType;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void clearWarnings() throws SQLException {&lt;br /&gt;  connection.clearWarnings();&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;....&lt;br /&gt;....&lt;br /&gt;....&lt;br /&gt; public Statement createStatement() throws SQLException {&lt;br /&gt;  Statement stmt = connection.createStatement();&lt;br /&gt;  return new LoggableStatement(stmt,dbType);&lt;br /&gt; }&lt;br /&gt;....&lt;br /&gt;....&lt;br /&gt;....&lt;br /&gt; public CallableStatement prepareCall(String sql) throws SQLException {&lt;br /&gt;  CallableStatement cstmt = connection.prepareCall(sql);&lt;br /&gt;  return new LoggableCallableStatement(cstmt,sql,dbType);&lt;br /&gt; }&lt;br /&gt;....&lt;br /&gt;....&lt;br /&gt;....&lt;br /&gt; public PreparedStatement prepareStatement(String sql) throws SQLException {&lt;br /&gt;  PreparedStatement pstmt = connection.prepareStatement(sql);&lt;br /&gt;  return new LoggablePreparedStatement(pstmt,sql,dbType);&lt;br /&gt; }&lt;br /&gt;....&lt;br /&gt;....&lt;br /&gt;....&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="BACKGROUND-COLOR:lightgray"&gt;&lt;br /&gt;public class LoggableStatement implements Statement {&lt;br /&gt;&lt;br /&gt; protected final Logger logger = Logger&lt;br /&gt;   .getLogger(getClass());&lt;br /&gt; &lt;br /&gt; protected DBType type = null;&lt;br /&gt;&lt;br /&gt; protected Statement stmt = null;&lt;br /&gt; protected Vector&lt;String&gt; batchQueries = null; &lt;br /&gt;&lt;br /&gt; public LoggableStatement(Statement stmt,DBType type) {&lt;br /&gt;  this.stmt = stmt;&lt;br /&gt;  batchQueries = new Vector&lt;String&gt;();&lt;br /&gt;  this.type = type;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void addBatch(String sql) throws SQLException {&lt;br /&gt;  try {&lt;br /&gt;   batchQueries.add(sql);&lt;br /&gt;   stmt.addBatch(sql);&lt;br /&gt;  } catch (SQLException sqle) {&lt;br /&gt;   SQLExceptionFormator.printException(logger, sqle, type);&lt;br /&gt;   throw sqle;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;....&lt;br /&gt;....&lt;br /&gt;....&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Library and Source code&lt;/b&gt;&lt;br /&gt;[&lt;a href='http://sourceforge.net/projects/loggabledb/files/LoggableDB/'&gt;Java 5 and 6&lt;/a&gt;]&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-2763576648178529087?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/2763576648178529087/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2010/03/logging-db-related-activities.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/2763576648178529087'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/2763576648178529087'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2010/03/logging-db-related-activities.html' title='Logging DB Related Activities (Connection, Statement, Prepared Statement, Callable Statement)'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-618658370293580393</id><published>2010-03-12T14:04:00.000-08:00</published><updated>2010-03-12T14:22:25.087-08:00</updated><title type='text'>Displaying DB (db2) Specific Error Message</title><content type='html'>Recently when I was working for a project, they spent more hours to found what root-cause. The interesting thing is that this project has good logging of all SQLException.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But the issue was since SQLException is wrapper of original exception, it was difficult for the team to find out the exact error message. Finally they took the error code and google for it and find-out.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;After that we thought of displaying DB specific errors instead of displaying just SQLException. Since the DB specific classes cannot be used in development we need to be really cautious in using such things. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Note: Use Dynamic loading for DB Specific coding, other wise you Runtime Exception can occur. We can't expect all DB (DB2, MySQL etc..) jar files to be in your classpath.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Following code snippets gives you how to develop DB Specific errors. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="BACKGROUND-COLOR: #e3f4fd"&gt;&lt;div&gt;&lt;/div&gt;&lt;em&gt;&lt;div&gt;&lt;div&gt;import java.sql.SQLException;&lt;/div&gt;&lt;div&gt;import org.apache.log4j.Logger;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;public abstract class SQLExceptionFormator {&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;public enum DBType {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;DB2&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;};&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;public abstract boolean isFormatSupported();&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;public abstract String generateDBSpecificMessage(SQLException sqle);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;public static void printException(Logger log, SQLException sqle,&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;DBType type) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;StringBuffer message = new StringBuffer(sqle.getMessage());&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;SQLExceptionFormator format = getExceptionFormator(type);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;if(format.isFormatSupported()){&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;message.append("\nDatabase Specific Message: ");&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;message.append(format.generateDBSpecificMessage(sqle));&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;log.error(message, sqle);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;private static SQLExceptionFormator getExceptionFormator(DBType type) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;SQLExceptionFormator formator = null;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;switch (type) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;case DB2:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;formator = new DB2SQLExceptionFormator();&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;break;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;return formator;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;&lt;/em&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;b&gt;&lt;i&gt;This class can be re-used for any type of DB's&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Following is the implementation class for DB2&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;em&gt;&lt;/em&gt;&lt;/div&gt;&lt;em&gt;&lt;div&gt;&lt;div&gt;import java.sql.SQLException;&lt;/div&gt;&lt;div&gt;import com.ibm.db2.jcc.DB2Diagnosable;&lt;/div&gt;&lt;div&gt;import com.ibm.db2.jcc.DB2Sqlca;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;public class DB2SQLExceptionFormator extends SQLExceptionFormator {&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;private static boolean isFormatSupported = loadClass(); &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;@Override&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;public String generateDBSpecificMessage(SQLException sqle) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;String message = null;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;if (sqle instanceof DB2Diagnosable) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;DB2Diagnosable db2e = (DB2Diagnosable) sqle;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;DB2Sqlca sqlca = db2e.getSqlca();&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;try {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;    &lt;/span&gt;message = sqlca.getMessage();&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;} catch (SQLException e) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;    &lt;/span&gt;message = e.getMessage();&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;} &lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;return message;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;@Override&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;public boolean isFormatSupported() {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;return isFormatSupported;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;private static boolean loadClass() { //Use dynamic loading, otherwise there is a possibility for Runtime Exception.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;boolean lc = false;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;try {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;Class.forName("com.ibm.db2.jcc.DB2Diagnosable");&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;lc = true;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;} catch (ClassNotFoundException e) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;return lc;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;&lt;/em&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-618658370293580393?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/618658370293580393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2010/03/displaying-db-db2-specific-error.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/618658370293580393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/618658370293580393'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2010/03/displaying-db-db2-specific-error.html' title='Displaying DB (db2) Specific Error Message'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-6685712541121782067</id><published>2010-03-12T13:58:00.000-08:00</published><updated>2010-03-12T14:03:33.874-08:00</updated><title type='text'>Closing DB resources</title><content type='html'>Most of the time we can see the Database Resources like Connection, Statements, PreparedStatement, Resultset etc.. are not closed properly.&lt;br /&gt;In certain cases there are chances where the exceptions are not caught properly.&lt;br /&gt;&lt;br /&gt;I thought of solving this issue by writing a small utility in Java 5&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;em&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;   public static void closeResources(Object... resources) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;for (Object resource : resources) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;if (resource != null) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;    &lt;/span&gt;try {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;     &lt;/span&gt;// Fix to avoid already closed exception&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;     &lt;/span&gt;if (resource instanceof Connection) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;      &lt;/span&gt;Connection con = (Connection) resource;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;      &lt;/span&gt;if (!con.isClosed()) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;       &lt;/span&gt;con.close();&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;      &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;     &lt;/span&gt;} else {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;      &lt;/span&gt;Method closeMethod = (resource.getClass()).getMethod(&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;        &lt;/span&gt;"close", null);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;      &lt;/span&gt;closeMethod.setAccessible(true);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;      &lt;/span&gt;closeMethod.invoke(resource, null);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;     &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;    &lt;/span&gt;} catch (Exception e) { // ByPassing all the exception&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;     &lt;/span&gt;logger.info(e.getMessage(), e);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;    &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;/em&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;You can use these method as with any of the DB resources.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Some examples are &lt;/div&gt;&lt;div&gt;closeResources(rs);&lt;/div&gt;&lt;div&gt;closeResources(rs,pstmt);&lt;/div&gt;&lt;div&gt;closeResources(rs,pstmt,connection)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-6685712541121782067?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/6685712541121782067/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2010/03/closing-db-resources.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/6685712541121782067'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/6685712541121782067'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2010/03/closing-db-resources.html' title='Closing DB resources'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-3537342505801030454</id><published>2009-08-02T21:27:00.000-07:00</published><updated>2012-01-25T22:35:35.853-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='Cache'/><category scheme='http://www.blogger.com/atom/ns#' term='servlet'/><category scheme='http://www.blogger.com/atom/ns#' term='Caching'/><category scheme='http://www.blogger.com/atom/ns#' term='client'/><title type='text'>Performance: Caching static data on Client Side</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Recently one of my friends came to me with a performance issue. They are doing some sort of mapping application (Building Locator) using Microsoft Virtual Map. They have one search screen where they can feed in their country name, state etc... and they make AJAX calls for fetching data which is then plotted on the map. &lt;br /&gt;First I checked their database and found it takes at-least one minute to fetch the data for each search. We can't do anything on the DB side because of other reason. &lt;br /&gt;The plus point I have noticed here is data will be static almost all the time but may vary once in a while (once in a year may be). So our idea was to bring the entire data to the client side (but not putting them to client's memory always). We thought of using globalStorage mechanism, added in HTML 5 (only few browsers support this now) or JSCache, but skipped because of the data load. &lt;br /&gt;We tried using Dynamic Java Script for caching static data on the client machine. With this approach the data are send to client side as a JavaScript file and will be stored in their IE Temporary Folder. So we don't have any memory Constrains there. Also like Cookie, the data are not passed with each request/response. One more advantage we felt here is, this feature not only store data, but do more on functionality side. To make clear let us see how we did this,&lt;br /&gt;&lt;strong&gt;&lt;em&gt;1. Writing Server Side Component (I am using Java here - You can use any language)&lt;/em&gt;&lt;/strong&gt; &lt;br /&gt;&lt;div style="background-color: #e3f4fd;"&gt;&lt;pre&gt;&lt;strong&gt;public class JSServlet extends HttpServlet&lt;/strong&gt; {&lt;/pre&gt;&lt;br /&gt;protected void doGet(HttpServletRequest request,HttpServletResponse response) &lt;br /&gt;throws ServletException, IOException { &lt;br /&gt;StringWriter buffer = new StringWriter(); &lt;br /&gt;prepareData(buffer); &lt;br /&gt;&lt;br /&gt;String encoding = request.getHeader("Accept-Encoding"); &lt;br /&gt;boolean supportsGzip = false; &lt;br /&gt;&lt;br /&gt;if (encoding != null) { &lt;br /&gt;supportsGzip = (encoding.toLowerCase().indexOf("gzip") &amp;gt; -1); &lt;br /&gt;} &lt;br /&gt;if (supportsGzip == true) { &lt;br /&gt;response.setHeader("Content-Encoding", "gzip"); &lt;br /&gt;response.setHeader("Content-Type", "text/javascript"); &lt;br /&gt;&lt;br /&gt;try { &lt;br /&gt;&lt;strong&gt;GZIPOutputStream gzos = new GZIPOutputStream(response &lt;br /&gt;.getOutputStream()); &lt;br /&gt;System.out.println(buffer.getBuffer().toString()); &lt;br /&gt;gzos.write(buffer.getBuffer().toString().getBytes()); &lt;br /&gt;gzos.close();&lt;/strong&gt; &lt;br /&gt;} catch (IOException ie) { &lt;br /&gt;} &lt;br /&gt;} else { &lt;br /&gt;// You output ordinary HTML here &lt;br /&gt;// when GZIP is not supported by the browser. &lt;br /&gt;} &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;private void prepareData(StringWriter buffer) { &lt;br /&gt;Employee emp1 = new Employee(111, "Biju", "Nair", 35); &lt;br /&gt;Employee emp2 = new Employee(121, "Chellappa", "Murugan", 32); &lt;br /&gt;//json.org &lt;br /&gt;JSONObject[] empArray = { new JSONObject(emp1), new JSONObject(emp2) }; &lt;br /&gt;&lt;br /&gt;JSONArray jsonArr = null; &lt;br /&gt;buffer.write("function displayName(empid){ "); &lt;br /&gt;buffer.write("\nvar employeeObject = {\"empList\" :"); &lt;br /&gt;try { &lt;br /&gt;jsonArr = new JSONArray(empArray); &lt;br /&gt;jsonArr.write(buffer); &lt;br /&gt;} catch (JSONException e) { &lt;br /&gt;} &lt;br /&gt;buffer.write("};"); &lt;br /&gt;buffer.write("\nfor(index=0;index&amp;lt;employeeObject.empList.length;index++){"); &lt;br /&gt;buffer.write("\n if(employeeObject.empList[index].empid == empid){"); &lt;br /&gt;buffer.write("\n alert('Name: '+( &lt;br /&gt;employeeObject.empList[index].lastName+', &lt;br /&gt;'+employeeObject.empList[index].firstName));"); &lt;br /&gt;buffer.write("\n break;"); &lt;br /&gt;buffer.write("\n }"); &lt;br /&gt;buffer.write("\n}"); &lt;br /&gt;buffer.write("\n}"); &lt;br /&gt;}&lt;/div&gt;Since we have used HTTP 1.1 GZip compression on the response, the data send from server to client is minimal. &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;2. Writing Client Side Component &lt;br /&gt;&lt;/em&gt;&lt;/strong&gt; &lt;br /&gt;&lt;br /&gt;&lt;div style="background-color: #e3f4fd;"&gt;&lt;pre&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&lt;strong&gt;&amp;lt;script type="text/javascript" src="JSServlet"&amp;gt;&lt;/strong&gt;&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;lt;form name='frm'&amp;gt;&lt;br /&gt;Enter Employee ID:&amp;lt;input type='text' name='empid'&amp;gt;&lt;br /&gt;&amp;lt;input type='Button' onClick='javascript:displayName(document.frm.empid.value)' value='Search'&amp;gt;&lt;br /&gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Look at the bolded lines above for the key points. In this case I have used static way of loading the data, but you can also do that using dynamic way using DHTML as, &lt;br /&gt;&lt;em&gt;script = document.createElement('script') &lt;br /&gt;script.src = 'JSServlet'; &lt;br /&gt;document.heads[0].appendChild(script);&lt;/em&gt; &lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;You can also restrict the data load or push to client depending on whether there has some addition to the real data.&lt;br /&gt;&lt;span style="background-color: #b6d7a8;"&gt;   if (!isModified)&lt;br /&gt;{&lt;br /&gt;response.sendError(response.HTTP_NOT_MODIFIED,"Not Modified");&lt;br /&gt;} else {&lt;br /&gt;response.addHeader("Last-Modified",new Date());&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #b6d7a8;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre style="text-align: -webkit-auto;"&gt;&lt;span style="background-color: #b6d7a8;"&gt;response.addHeader&lt;/span&gt;&lt;span style="background-color: #b6d7a8;"&gt;("Expires",&lt;/span&gt;&lt;span style="background-color: #b6d7a8;"&gt;-1); //Clear the page when browser is closed&lt;/span&gt;&lt;/pre&gt;&lt;span style="background-color: #b6d7a8;"&gt; ...&lt;br /&gt;//actual code to load data and send it to client&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;Advantages: &lt;br /&gt;&lt;ul&gt;&lt;li&gt;Make the UI perform fast.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Entire data is taken to client side, so no need for repeat server request (Ajax) for fetching data.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Data can be fetched during each page load (for updated data) or only once (for ever).  &lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="color: red;"&gt;&lt;strong&gt;Caution: Don't use this approach for secured information.&lt;/strong&gt; &lt;/span&gt;  &lt;br /&gt;I am still in the process of fine tuning the solution. Will keep you update....&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-3537342505801030454?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/3537342505801030454/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2009/08/performance-caching-static-data-on.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/3537342505801030454'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/3537342505801030454'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2009/08/performance-caching-static-data-on.html' title='Performance: Caching static data on Client Side'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-6379995089431505853</id><published>2008-12-30T02:54:00.000-08:00</published><updated>2008-12-30T02:57:41.731-08:00</updated><title type='text'>Google Guice - Dependency Injection Framework</title><content type='html'>A good Dependency Injection Framework which clicked me recently.&lt;br /&gt;&lt;br /&gt;The 'Google Guice' site provides many examples and tutorials for developers.&lt;br /&gt;&lt;br /&gt;Visit &lt;a href="http://code.google.com/p/google-guice/"&gt;Google Guice&lt;/a&gt; and download.&lt;br /&gt;&lt;br /&gt;Enjoy the new framework.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-6379995089431505853?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/6379995089431505853/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2008/12/google-guice-dependency-injection.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/6379995089431505853'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/6379995089431505853'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2008/12/google-guice-dependency-injection.html' title='Google Guice - Dependency Injection Framework'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-1357002187996327270</id><published>2008-12-30T02:30:00.001-08:00</published><updated>2008-12-30T02:53:41.641-08:00</updated><title type='text'>Creating Classes Dynamically</title><content type='html'>The first question you normally ask is why do we need to create classes dynamically. I have came across many situation where I have to modify the exisitng classes by adding some functionality.&lt;br /&gt;&lt;br /&gt;Recently when I was working with &lt;a href="http://carbonado.sourceforge.net/"&gt;Carbonado&lt;/a&gt;, I got a chance where I modified the exisitng class. Interesting part here is, Carbonado also uses the same opensource tool for creating classes dynamically.&lt;br /&gt;&lt;br /&gt;This is the reason why i thought of sharing this information.&lt;br /&gt;&lt;br /&gt;Before Carbondao I was using Berkeley DB Java Edition (another in-memory database) where we can store the data object directly into memory. Following is my data class.&lt;br /&gt;&lt;br /&gt;&lt;DIV&gt;&lt;br /&gt;&lt;PRE&gt;&lt;br /&gt;public class Employee {&lt;br /&gt; private String ID;&lt;br /&gt; private String firstName;&lt;br /&gt; private String lastName;&lt;br /&gt; private String email;&lt;br /&gt;&lt;br /&gt; public String getID() {&lt;br /&gt;  return ID;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setID(String id) {&lt;br /&gt;  ID = id;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public String getFirstName() {&lt;br /&gt;  return firstName;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setFirstName(String firstName) {&lt;br /&gt;  this.firstName = firstName;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public String getLastName() {&lt;br /&gt;  return lastName;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setLastName(String lastName) {&lt;br /&gt;  this.lastName = lastName;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public String getEmail() {&lt;br /&gt;  return email;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setEmail(String email) {&lt;br /&gt;  this.email = email;&lt;br /&gt; }&lt;br /&gt;  }&lt;br /&gt;&lt;/PRE&gt;&lt;br /&gt;&lt;/DIV&gt;&lt;br /&gt;&lt;br /&gt;Using the API's provided by Berkeley DB, I was able to insert the object into my memory.&lt;br /&gt;&lt;br /&gt;Later the requirements changed and I have to do the same using Carbonado. &lt;br /&gt;&lt;br /&gt;But Carbonado need the presitence classes to be decorated using some annotations and with some extension. I did like modifying the exisitng class, instead I planned to modify the classes on fly.&lt;br /&gt;&lt;br /&gt;For this I created a dummy class with Carbonado requirement. The dummy class looks like&lt;br /&gt;&lt;PRE&gt;&lt;br /&gt;@PrimaryKey("pkey")&lt;br /&gt;public interface CarbonadoStore extends Storable {&lt;br /&gt; String getPkey();&lt;br /&gt; void setPkey(String pkey);&lt;br /&gt;}&lt;br /&gt;&lt;/PRE&gt;&lt;br /&gt;&lt;br /&gt;Later I modified my 'Employee' class such that it implements the CarbonadoStore interface so that it meet all the requirements of carbonado and ready for presitence.&lt;br /&gt;&lt;br /&gt;The dynamic creation of class is done using &lt;a href="http://cojen.sourceforge.net/"&gt;'&lt;em&gt;&lt;strong&gt;cojen&lt;/strong&gt;&lt;/em&gt;'&lt;/a&gt; open soure component.&lt;br /&gt;&lt;br /&gt;Following is the code snippet which I used for modification.&lt;br /&gt;&lt;DIV&gt;&lt;br /&gt;&lt;PRE&gt;&lt;br /&gt; &lt;strong&gt;public void save(String key, Object object)&lt;/strong&gt;&lt;br /&gt;    {&lt;br /&gt;  Class&lt;Storable&gt; store = &lt;em&gt;createStorableInterface(object.getClass());&lt;/em&gt;  &lt;br /&gt;                Storage storage;&lt;br /&gt;  storage = repository.storageFor(store);&lt;br /&gt;  Storable message = storage.prepare();&lt;br /&gt;  copyObjectToMessage(object, message); //Copy the data of objet to message class using reflection.&lt;br /&gt;  message.insert();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;private Class&lt;Storable&gt; createStorableInterface(Class objClz)&lt;/strong&gt;&lt;br /&gt;    {&lt;br /&gt;  Class&lt;Storable&gt; store = null;&lt;br /&gt;  ClassFile cf = ClassFile.readFrom(getClass().getResourceAsStream(&lt;br /&gt;     "CarbonadoStore.class"));&lt;br /&gt;   Method[] methods = objClz.getDeclaredMethods();&lt;br /&gt;   for (Method method : methods) {&lt;br /&gt;    cf.addMethod(Modifiers.PUBLIC_ABSTRACT, method.getName(),&lt;br /&gt;      TypeDesc.forClass(method.getReturnType()),&lt;br /&gt;      getParamTypes(method.getParameterTypes()));&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   ByteArrayOutputStream out = new ByteArrayOutputStream();&lt;br /&gt;&lt;br /&gt;   cf.writeTo(out);&lt;br /&gt;   CPClassLoader cloader = new CPClassLoader(objClz.getClassLoader());&lt;br /&gt;   store = cloader.define(&lt;br /&gt;     "com.demo.CarbonadoStore",&lt;br /&gt;     out.toByteArray());&lt;br /&gt;  return store;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;private static final class CPClassLoader extends ClassLoader &lt;/strong&gt;{&lt;br /&gt;&lt;br /&gt;  CPClassLoader(ClassLoader parent) {&lt;br /&gt;   super(parent);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  CPClassLoader() {&lt;br /&gt;   super();&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  Class define(String name, byte[] b) {&lt;br /&gt;   Class clazz = defineClass(name, b, 0, b.length);&lt;br /&gt;   resolveClass(clazz);&lt;br /&gt;   return clazz;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;/PRE&gt;&lt;br /&gt;&lt;/DIV&gt;&lt;br /&gt;&lt;br /&gt;On doing this now I can reuse the same Employee class for both BerkelyDB and Carbonado operations.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-1357002187996327270?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/1357002187996327270/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2008/12/creating-classes-dynamically.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/1357002187996327270'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/1357002187996327270'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2008/12/creating-classes-dynamically.html' title='Creating Classes Dynamically'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-5823424820487866034</id><published>2008-12-30T01:22:00.000-08:00</published><updated>2008-12-30T02:28:56.505-08:00</updated><title type='text'>Sharing Data Between Sessions, WARs and Context</title><content type='html'>As per servlet specification the HTTP Session (which is normally used to share data between request) should not be shared across WAR or Context due to security reasons.&lt;br /&gt;&lt;br /&gt;There are several other way through which you can share the data betwwen Sessions, WARs and Context. These options should be used on risk. &lt;br /&gt;&lt;br /&gt;Here are few options I have worked on.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Sharing between Context using Tomcat HTTP Server&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Tomcat provide a context configuration xml, in which you can set the value of 'crosscontext' attribute to 'true'. This will share the context between WAR's. Using this approach we can access another context from a context.&lt;br /&gt;&lt;br /&gt;Following are the code you have to use for accessing the context,&lt;br /&gt;&lt;br /&gt;&lt;DIV&gt;&lt;br /&gt;&lt;br /&gt;Say you have two context namely WebApp1 and WebApp2. &lt;br&gt;&lt;br /&gt;Now set the value of cross-context as true for WebApp1 by providing context.xml&lt;br /&gt;For details visit &lt;a href="http://tomcat.apache.org/tomcat-5.5-doc/config/context.html"&gt;Tomcat Context&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In WebApp1, you can set the data to the context as context.setAttibute(&amp;quot;DATA&amp;quot;,object).&lt;br /&gt;&lt;br /&gt;In WebApp2, instead of accessing it's context object, get the Context of 'WebApp1' as,&lt;br /&gt;ServletContext fcontext = currentContext.getContext(&amp;quot;WebApp1&amp;quot;);&lt;br /&gt;and then fcontext.getAttribute(&amp;quot;DATA&amp;quot;).&lt;br /&gt;&lt;br /&gt;If the cross-context value is false, the value of &lt;em&gt;fcontext&lt;/em&gt; will be &lt;em&gt;&lt;strong&gt;null&lt;/strong&gt;&lt;/em&gt;.&lt;br /&gt;&lt;/DIV&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Using JBoss Cache to Share Data&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;JBoss Cache provide two ways of caching. One by using MBeans and second by creating the cache programatically.&lt;br /&gt;&lt;br /&gt;Following link explain the details of using JBossCache (TreeCache).&lt;br /&gt;&lt;a href="http://www.jboss.org/file-access/default/members/jbosscache/freezone/docs/1.2.0/TreeCache.html"&gt;Tree Cache - JBoss&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;For any more details, please feel free to write to me at biju74@yahoo.com&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-5823424820487866034?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/5823424820487866034/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2008/12/sharing-data-between-sessions-wars-and.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/5823424820487866034'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/5823424820487866034'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2008/12/sharing-data-between-sessions-wars-and.html' title='Sharing Data Between Sessions, WARs and Context'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2057261497563466722.post-34969625868983895</id><published>2008-12-29T08:56:00.000-08:00</published><updated>2008-12-29T09:35:49.717-08:00</updated><title type='text'>Adding Header Information to an existing HTTP Request</title><content type='html'>Servlet API doesn’t allow modifying the header information of an existing HTTP Request. The API does not allow ‘addHeader’ method.&lt;br /&gt;&lt;br /&gt;Consider a situation where you need to add a token to all existing HTTP request, which will be used by other controllers or servlets for authentication or authorization.&lt;br /&gt;&lt;br /&gt;One good example is SSO, where the encrypted user credentials can be stored as the part of HTTP Request.&lt;br /&gt;&lt;br /&gt;Following is the simple way to modify the existing HTTP Request.&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Create a Filter or Controller Class in which the existing request need to be modified.&lt;br /&gt;&lt;li&gt;Create a class which extends ‘HTTPServletRequestWrapper’ (This class is provided by Servlet API for creating custom request objects). &lt;a href="http://www.blogger.com/post-create.g?blogID=2057261497563466722#SampleClass"&gt;Sample code&lt;/a&gt; is shown below.&lt;br /&gt;&lt;li&gt;Provide addHeader method to this class.&lt;br /&gt;&lt;li&gt;Override getHeader(s) methods.&lt;br /&gt;&lt;li&gt;Create an instance of newly created class.&lt;br /&gt;&lt;li&gt;Modify the new object using addHeader method.&lt;br /&gt;&lt;li&gt;Pass the new object to the next chain instance in the Filter or Controller chain list.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Note: Make sure you are not corrupting the existing request (unless required).&lt;br /&gt;&lt;br /&gt;&lt;a name="SampleClass"&gt;&lt;br /&gt;Sample Code&lt;/a&gt;&lt;br /&gt;&lt;div&gt;&lt;em&gt;&lt;pre&gt;&lt;br /&gt;import java.util.HashMap;&lt;br /&gt;import java.util.Map;&lt;br /&gt;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletRequestWrapper;&lt;br /&gt;&lt;br /&gt;public class CustomHttpServletRequest extends HttpServletRequestWrapper {&lt;br /&gt;&lt;br /&gt; private Map&lt;string,&gt; customHeaderMap = null;&lt;br /&gt;&lt;br /&gt; public CustomHttpServletRequest(HttpServletRequest request) {&lt;br /&gt;  super(request);&lt;br /&gt;  customHeaderMap = new HashMap&lt;string,&gt;();&lt;br /&gt; }&lt;br /&gt; public void addHeader(String name,String value){&lt;br /&gt;  customHeaderMap.put(name, value);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; @Override&lt;br /&gt; public String getParameter(String name) {&lt;br /&gt;  String paramValue = super.getParameter(name); // query Strings&lt;br /&gt;  if (paramValue == null) {&lt;br /&gt;   paramValue = customHeaderMap.get(name);&lt;br /&gt;  }&lt;br /&gt;  return paramValue;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;hr&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class CustomRequestFilter implements Filter {&lt;br /&gt; public void doFilter(ServletRequest sreq, ServletResponse sresp,&lt;br /&gt;   FilterChain fchain) throws IOException, ServletException {&lt;br /&gt;  CustomHttpServletRequest request = new CustomHttpServletRequest((HttpServletRequest)sreq);&lt;br /&gt;  request.addHeader("ssoid-token", encrypt("dummy"));&lt;br /&gt;  fchain.doFilter(request, sresp); //New Request Object is passed&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2057261497563466722-34969625868983895?l=bijubnair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bijubnair.blogspot.com/feeds/34969625868983895/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bijubnair.blogspot.com/2008/12/adding-header-information-to-existing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/34969625868983895'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2057261497563466722/posts/default/34969625868983895'/><link rel='alternate' type='text/html' href='http://bijubnair.blogspot.com/2008/12/adding-header-information-to-existing.html' title='Adding Header Information to an existing HTTP Request'/><author><name>Biju Nair</name><uri>http://www.blogger.com/profile/02249522684861350835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_NdvYnLaaKSc/SVkA_BHQOQI/AAAAAAAAA2w/WYKfwOQewUY/S220/IMG_2320.jpg'/></author><thr:total>0</thr:total></entry></feed>
