Wednesday, July 27, 2016

Web Storage API : Local Storage


I am going to explain web storage API specially Local Storage today. Its just awesome.

There are three types of storage :

1) Session Storage : available for your websites only and valid till browser is open or reloads or restores/

2) Local Storage : available for your website only and valid while browser is closed and opened again.

3) There is global storage also which is accessed by all of the websites in your browser.

Testing for support vs availability :
Browsers which support localStorage will have a property with the window object named localStorage. But, for various reasons, just asserting that property exists may throw exceptions. so you might want to check it in a try catch so that it does not break the flow.

Like :
function storageAvailable(type) {
 try {
  var storage = window[type],
   x = '__storage_test__';
  storage.setItem(x, x);
  storage.removeItem(x);
  return true;
 }
 catch(e) {
  return false;
 }
}

Then you do a check and do your work.
if (storageAvailable('localStorage')) {
   var storage = window['localStorage'];
      storage.clear(); storage.setItems("test", "test");
}
else {
 // Too bad, no localStorage for us
}

And other things which you should keep in mind while working with local storage :
1) You can only store strings in the local storage. If you need to store objects use JSON.Stringify.
2) clear your local storage by clear() method.
3) When you put data into local storage by usingstorage.setItems("test", "test"), you should do it in a try catch as it can also give exception if not enough memory.
4) You can run the web storage test to find out how much memory you have with your browser.
Run Test for Storage
5) When you clear the history , it calls storage.clear(), something you can do in production environment.

Sunday, July 24, 2016

GTT Global Temporary table : Java Interview

What are Global temporary tables and its uses :

 I worked on Global temporary table functionality recently and wanted to share a few experience.

Temporary tables are tables which live for a session only.The data for them is available for a session. There are various other options available with them which we will discuss below by taking an example of IBM DB2.

There are two ways of creating a Temporary table.

1) Declaring Global temporary table.
2) Creating Global Temporary table :


1) Declare GTT : Declare GTT is like a table created for a session only and its description is dropped
after session ends with DB. It has various advantages over Create GTT like

  • It does not need to worry about dropping it after our work is done.
  • The description can unique per session.
  • Constraints and Indexes can be created on Declared GTT.
  • Its like a table created per session and lives only for that session. The same is applicable for its data also.
2) Create GTT : Create GTT is a table which is created as a table , you will find its definition and description after the session ends and we connect to the DB again.
  • It needs to be dropped of you do not need it forward.
  • The description resides in DB after the session ends , so it is not unique per session. The data in it is unique per session.
  • Constraints and indexes can NOT be created.
  • Its like a table created in DB only and the data is created per session. So data from one session is unique instead on description as compared to DGTT. The data entered in one session will not be seen by other.

Example :


 DECLARE GLOBAL TEMPORARY TABLE MY_TEMP
    (ID BIGINT NOT NULL,
    AGE INTEGER
    ON COMMIT PRESERVE ROWS
    NOT LOGGED WITH REPLACE ON ROLLBACK DELETE ROWS ; 

Let me explain the main features which are used mostly : 
 ON COMMIT PRESERVE ROWS : On Committing the data the rows are preserve in session.

 I found this pretty useful in  case  of you are doing batch save and sending transaction in batches while saving large amount of data.

 ON COMMIT PRESERVE ROWS : save a result set in it and use it then delete.

WITH REPLACE : If a table with that name exists already, it will be deleted and created again. You can say it as truncate. This is not able if you are using CREATE  GLOBAL TEMPORARY TABLE

ON ROLLBACK DELETE ROWS : Once a  Rollback to a save point, the rows will be deleted.

GTT are  also used in Store procedure if you dont want to make a query to fetch something you have already fetched once, store it in a GTT .

Happy Coding.






Tuesday, July 12, 2016

SQL Best Practices : Java Interview

Best read for any SQL developer and how to optimize and increase your performance :

1) 10 Best Practices : SQL

2) 7 DO and DONTS : SQL

Please comment down below in case if you have more good articles.

Friday, July 8, 2016

Java Interview Questions : CTE Common Table Expression.

Q: What is CTE and how does it help in Improving the performance.

A : CTE as Common Table Expression. They are also called disposable Views. But there are some difference too. Like CTE can be recursive and can not be indexed. They tend to use the Existing indexes.

Syntax :

With cte as (select * from table1)

select * from table2  join cte on cte.key1 = table2.key2

----------------------------------------------------------------------

They are just like a table, so you can join on them and can not use them in where clause.
They can basically used to write neat SQL. But if you have some table calls written repetitive in lots of joins , you can put them in a CTE and use CTE. I have seen lots of performance in SQL optimization in this case.
 For any questions i can help you with CTE problems, please comment down below.

Java Interview Order of operation : SQL Directives

Q: Define the order in operation in which SQL directive gets executed.

A:
  1. FROM clause
  2. WHERE clause
  3. GROUP BY clause
  4. HAVING clause
  5. SELECT clause
  6. ORDER BY clause
First it is going to run the From clause to get the tables.
Then the WHERE clause to filter it. That is why FROM AS names are available in WHERE. EX: (select st.name  as newName from student st where st.age > 30 order by newName)
Then the GROUP BY AND HAVING to sum the groups and filter groups.
Then comes the SELECT clause.
In the last the ORDER BY , and the AS names from Select are available in ORDER BY clause.
More interview questions to follow.

Java Intevriew : Classic Singleton in Java

Q: How to create a Simple singleton class in Java.
A:
public class SimpleSingleton {

   private static SimpleSingleton instance = null;
   private SimpleSingleton () {
      // we declare it private so that you cant do below:
      //NonSynchronizedSingleton t = new NonSynchronizedSingleton(); 
   }
   public static SimpleSingleton getInstance() {
      if(instance == null) {
         instance = new SimpleSingleton();
      }
      return instance;
   }
}


public class SimpleSingletonDemo {
   public static void main(String[] args) {
      SimpleSingleton tmp1 = SimpleSingleton.getInstance( ); //Line no 1
      SimpleSingleton tmp1 = SimpleSingleton.getInstance( ); //Line no 2
   }
}


It is super easy to do this.
1) Declare the constructor as private  which is a must so that the no object can be initialized.
2) From Line no 1 :  In the public static getInstance method which returns SimpleSingleton we check if instance has already been initialized which is null as of now, so it goes ahead and initialize it.
3) Next time it will come to the getInstance method at Line No 2, it will find the instance as already initialize, so it will return the same object.

Enjoy Singleton.



Java Interview : Usage ArrayList vs LinkedList

Very Common Java Interview Question : When to use ArrayList and when LinkedList ?

Short is answer is : Use ArrayList for better traversal or search and use LinkedList for better insertions and deletions.

Explanation :

1) Linked List is a Doubly Linked List implementation of List Interface, where as ArrayList is an re sizable Array implementation of List Interface. LinkedList can act as both List and Queue where as ArrayList can only act as List.

2) LinkedList has a reverse iterator which it uses to reach an element from end if the path is shorter.

3) A search operation is faster in ArrayList  : it has a complexity of O(1) where as in an LinkedList it is O(n) as you have to iterate over number of records in LinkedLIst.

4) Insertions are faster in LinkedList than ArrayList. In LinkedList we just have to break the nodes and put our entry. But in case of arrayList, there is an overhead of moving the array indexes and then re sizing the array if the size has increased.

4) Deletions are almost the same in ArrayList and Linked List because we have to traverse the node to delete. And in ArrayList they have to update to a new Array. ArrayList is slower than LinkedList because it has to free up a slot in the middle of the array which means moving some references here and there and in the worst case reallocating the entire array. LinkedList just has to manipulate some references.

5) For Manipulation of data use LinkedList and for search operations use ArrayList.

Tuesday, June 21, 2016

Java interview Questions : For some very good interview questions ,please use below link :

Java World Interview questions

Java Interview questions