Wednesday, 27 June 2012

Oracle - Temporary Table

Core part of Temporary Table

Ø        Data only visible from the current session

Added Advantages

Ø        Index can be created on temporary table
Ø        Check constraints is possible  (unicity, limited set of possible values…)
Ø        A view can reference a temporary table
Ø        No FOREIGN KEY, no storage allocation, no INITIAL extent, no tablespace
Ø        Temporary table allocates storage from the temporary tablespace of the connected user.

Why Temporary table is faster

Ø        Temporary tables generate no redo logs for their blocks.

What need to be avoided while using Temporary table.

Ø        Temporary tables generate undo, and the undo is logged.
Ø        So we must avoid deleting or updating from temporary tables. You can just let the temporary tables empty themselves automatically after a commit.
Ø        If billions of rows will be inserted producing a temporary tablespaces full, If such a process does not give performance increase

What should we do using Temporary table

Ø        You should use temporary tables mostly for inserting or selecting of data


Java : Constructors must not invoke overridable methods

public class Override_Test {
   public static void main(String... args) {
      new Maruthi();
   }
}

class Car {
   String sName;

   Car() {
      sName = "Car Cycle";
      print();
   }

   public void print() {
      System.out.println("CAR : " + sName);
   }
}

class Maruthi extends Car {
   String sModel = "900";

   Maruthi() {
      sModel = "800";
   }

   @Override
   public void print() {
      System.out.println("MARUTHI : " + sModel);
   }
}


Let we guess the output of this implementation…

In Car constructor we are calling an overridden method print…

Moral : Constructors must not invoke overridable methods







Output :   MARUTHI : null

JEE Web Services 5 Jargons

JEE Web Services 5 Jargons
JSON JavaScript Object Notation
JAX-WS Java API for XML-based Web Services
StAX Streaming API for XML
REST Representational state transfer
JAXB Java Architecture for XML Binding
WSIT Web Services Interoperability Technologies
WCF Windows Communication Foundation
SOA Service Oriented Architecture
BPEL Business Process Execution Language



Software As A Service (SaaS)

SaaS is a new model of how software is delivered. SaaS refers to software that is accessed via a web browser and is paid on a subscription basis (monthly or yearly). Different from the traditional model where a customer buys a license to software and assumes ownership for its maintenance and installation, SaaS presents significant advantages to the customer.

SaaS is faster and a cost effective way to getting implemented. There are no hardware, implementation or acquisition costs involved to run the application from the customer's side. It's the responsibility of the SaaS vendor (us) to manage and run the application with utmost security, performance and reliability.

Since customers pay a subscription, they have immediate access to the new features and functionality. Unlike traditional softwares where upgrades would happen once a year or once in 6 months (with the vendor coming to your office with a CD), the SaaS vendor continuously pushes new updates, fixes to the application, which is immediately accessible by the customer. This reduces the length of time it takes a customer to recognize value from the software.

Since the software application is delivered as a service, its important for the vendor to focus on customer service and experience. Since this is on a subscription model, the vendor is judged on a month-month basis and the pressure to innovate or risk losing business is greater.

SaaS can be used by Windows, Linux, or Mac users, providing true platform independence over the Internet.

{Synchronization} Object and Class level lock

package com.test;

public class TestThreadLock {

   public static void main(String... strings) {
      TestThread t = new TestThread();
      Thread t1 = new Thread(t);
      Thread t2 = new Thread(t);
      t1.start();
      t2.start();
   }

}

class TestThread implements Runnable {

   @Override
   public void run() {
      System.out.println("Hello");
      fun1();
      fun2();
      fun3();
      fun4();
      fun5();
      fun6();
   }

   public void fun1() {
      System.out.println("fun1");
   }

   public synchronized void fun2() {
      System.out.println("fun2");
   }

   public void fun3() {
      synchronized (this) {
         System.out.println("fun3");
      }
   }

   public static void fun4() {
      System.out.println("fun4");
   }

   public static synchronized void fun5() {
      System.out.println("fun5");
   }

   public void fun6() {
      synchronized (TestThread.class) {
         System.out.println("fun6");
      }
   }

}




Choosing Hibernate Cache Provider

Choosing a cache provider
 
For now, Hibernate forces you to choose a single cache provider for the whole application. Providers for the following products are built into Hibernate:
 
■ EHCache is intended for a simple process scope cache in a single JVM. It can cache in memory or on disk, and it supports the optional Hibernate query result cache.
■ OpenSymphony OSCache is a library that supports caching to memory and disk in a single JVM, with a rich set of expiration policies and query cache support.
■ SwarmCache is a cluster cache based on JGroups. It uses clustered invalidation but doesn’t support the Hibernate query cache.
■ JBossCache is a fully transactional replicated clustered cache also based on the JGroups multicast library. The Hibernate query cache is supported, assuming that clocks are synchronized in the cluster. It’s easy to write an adaptor for other products by implementing net.sf.hibernate.cache.CacheProvider.
 
Not every cache provider is compatible with every concurrency strategy. The
compatibility matrix in table will help you choose an appropriate combination.
 
Cache concurrency strategy support
 
Cache Provider read-only
 
Non strict read-write read-write transactional
 
EHCache X X X  
OSCache X X X  
SwarmCache X X    
JBossCache X     X

Java API's Used for Cache Implementation

Cache Implementation




Common source of memory leaks is caches.

-        Once you put an object reference into a cache, it’s easy to forget that it’s there and leave it in the cache long after it becomes irrelevant.

There are several solutions to this problem

-        An entry is relevant exactly, so long as there are references to its key outside of the cache; represent the cache as a WeakHashMap.
-        Entries will be removed automatically after they become obsolete.

More commonly, the useful lifetime of a cache entry is less well defined, with entries becoming less valuable over time.
The LinkedHashMap class facilitates the latter approach with its removeEldestEntry method.
Ø        Returns true if this map should remove its eldest entry. This method is invoked by put and putAll after inserting a new entry into the map. It provides the implementor with the opportunity to remove the eldest entry each time a new one is added. This is useful if the map represents a cache: it allows the map to reduce memory consumption by deleting stale entries.

Ø        Sample use: this override will allow the map to grow up to 100 entries and then delete the eldest entry each time a new entry is added, maintaining a steady state of 100 entries.
     private static final int MAX_ENTRIES = 100;

     protected boolean removeEldestEntry(Map.Entry eldest) {
        return size() > MAX_ENTRIES;
     }
 
The cache should occasionally be cleansed of entries that have fallen into disuse. This can be done by a background thread (perhaps a Timer or ScheduledThreadPoolExecutor)

Timer

Fires one or more ActionEvents at specified intervals.
Setting up a timer involves creating a Timer object, registering one or more action listeners on it, and starting the timer using the start method.

  int delay = 1000; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //...Perform a task...
      }
  };
  new Timer(delay, taskPerformer).start();

javax.swing.Timer
   Its event handling metaphor is familiar to GUI programmers and can make dealing with the event-dispatching thread a bit simpler
      Its automatic thread sharing means that you don't have to take special steps to avoid spawning too many threads. Instead, your timer uses the same thread used to make cursors blink, tool tips appear, and so on.
        We extensively used this class in AceTP.


ScheduledThreadPoolExecutor

A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically. This class is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required.
Delayed tasks execute no sooner than they are enabled, but without any real-time guarantees about when, after they are enabled, they will commence. Tasks scheduled for exactly the same execution time are enabled in first-in-first-out (FIFO) order of submission.
When a submitted task is cancelled before it is run, execution is suppressed. By default, such a cancelled task is not automatically removed from the work queue until its delay elapses. While this enables further inspection and monitoring, it may also cause unbounded retention of cancelled tasks. To avoid this, set setRemoveOnCancelPolicy(boolean) to true, which causes tasks to be immediately removed from the work queue at time of cancellation.
Why ScheduledThreadPoolExecutor
That is a good reason for the ScheduledThreadPoolExecutor to be designed the way it is. You can put any amount of jobs on the queue, but there are never executed more jobs at the same time than can be run efficiently. It's up to you to balance that, of course.
If your tasks are IO bound, I'd set the pool size small, and if they are CPU bound, a bit larger (32 or so). You can also make multiple ScheduledThreadPoolExecutors, one for IO bound tasks and one for CPU bound tasks.