Friday, 27 June 2014

Memcache

Memcache 

           Free & open source, high-performance, distributed memory object caching system, but intended for use in speeding up dynamic web-apps by reducing database load.

Memcache is an arbitrary in memory storage i.e. it sits in the RAM where it stores small chunks of data possibly in key-value pairs that may be the response for database calls, API calls or page renders.

Key benefit of memcache is it has APIs for almost all programming language.

Java Caching Frameworks 

              Traditional Java caching frameworks are EHCache & OSCache are basicallly HashMap objects. Whenever  you add a new object in your cache it adds to the memory for your application. This strategy works when your data to be cached is in some MBs or probably KBs. But in large applications these framework falter because of their capacity to cache the objects. This is where memcache comes into picture. Unlike EHCache and OSCache where these are a part of your application memcache comes into two pieces - Memcache Server and Memcache client .  

Memcache :- 
         Memcache server runs as a separate process it stores all the cached object. For every memcache client there is a memcache server mapped wherein it gets the cache object from . Organizing data into memcache is based on key,value pairs.







Wednesday, 25 June 2014

Hibernate

Hibernate -

Hibernate is an open-source Java Persistence framework that lets applications connect to and work with relational databases using Object Relational Mapping . It implements the Java Persistence API - JPA which is the standard specification for object - relational mapping.Persistence is a process of storing the data to some permanent medium and retrieving it back at any point of time even after the application that had created the data ended.

 Architecture - 


Fig (a) Architecture of hibernate 

            The above diagram shows architecture of Hibernate. It creates a layer between Database and the Application. It loads the configuration details like Database connection string, entity classes, mappings etc.

Elements of the hibernate - 

SessionFactory

              The SessionFactory is a factory of session and client of ConnectionProvider. It holds second level cache (optional) of data. The org.hibernate.SessionFactory interface provides factory method to get the object of Session. 

Session

         The session object provides an interface between the application and data stored in the database. It is a short-lived object and wraps the JDBC connection. It is factory of Transaction, Query and Criteria. It holds a first-level cache (mandatory) of data. The org.hibernate.Session interface provides methods to insert, update and delete the object. It also provides factory methods for Transaction, Query and Criteria.
 

Transaction

            The transaction object specifies the atomic unit of work. It is optional. The org.hibernate.Transaction interface provides methods for transaction management.

ConnectionProvider

               It is a factory of JDBC connections. It abstracts the application from DriverManager or DataSource. It is optional.

 

TransactionFactory

               It is a factory of Transaction. It is optional.






Memory Leaks

Memory Leaks in Java


What is a memory leak?
         A memory leak is repetitive allocation of memory without consequential release of it when no longer used, leading to increase in consumption of memory. This may not be a common scenario in most Java standalone applications because Java has inbuilt Garbage Collection.

Garbage Collection in Java  


         Objects in Java are stored in Heap space , while static members are stored in memory area of the Java memory space. In java automatic Garbage collection is done using the Garbage Collection daemon thread.Whenever the object is not referenced by the code it becomes a candidate for garbage collection. Before  removing an object from Garbage Collection thread invokes finalize() method of that object so that it can perform any sort of cleanup.
         
          As a programmer you cannot invoke Garbage collection thread it will get invoked only if the JVM thinks that it needs Garbage Collection based on the Java Memory Space. You can invoke System.gc()  or Runtime.gc() to send request to Garbage Collection but it is not guaranteed that Garbage Collection thread will be invoked.


Types of Garbage Collection in Java 



  • Serial GC - 

    •  With the serial GC it uses internal logic for Garbage collection but in addition , it uses a compaction logic where it collects all the objects to the upper part of the heap, so that the new objects are allocated in continuous locations.


  • Concurrent Low Pause Collector-

    •  This Collector is used if the -Xingc or -XX:+UseConcMarkSweepGC is passed on the command line. This is also referred as Concurrent Mark Sweep Garbage collector. The concurrent collector is used to collect the tenured generation and does most of the collection concurrently with the execution of the application. The application is paused for short periods during the collection. A parallel version of the young generation copying collector is sued with the concurrent collector. Concurrent Mark Sweep Garbage collector is most widely used garbage collector in java and it uses algorithm to first mark object which needs to collected when garbage collection triggers.


  • The Incremental (Sometimes called train) low pause collector - 

    This collector is used only if -XX:+UseTrainGC is passed on the command line. This garbage collector has not changed since the java 1.4.2 and is currently not under active development. It will not be supported in future releases so avoid using this and please see 1.4.2 GC Tuning document for information on this collector. Important point to not is that -XX:+UseParallelGC should not be used with -XX:+UseConcMarkSweepGC. The argument passing in the J2SE platform starting with version 1.4.2 should only allow legal combination of command line options for garbage collector but earlier releases may not find or detect all illegal combination and the results for illegal combination are unpredictable. It’s not recommended to use this garbage collector in java.



           

Saturday, 21 June 2014

Object Relational Mapping - ORM

Object Relational Mapping (ORM)


        ORM is a technique for conversion of data between in-compatible type systems in object - oriented languages.Databases do not use the concept of Object Oriented languages wherein we associate objects to demonstrate the state of the systems. Database map values to primitive datatypes such as integer, real , float , String etc .Consider an example -         You have to insert an employee details into the database.If we use contemporary technique you need to convert that employee details to primitive value i.e. formulate an insert/update query with these details for that employee and then insert/update to the database.

This problem is solved by ORM .

          The Object Relational Mapping transforms any object data member type to a corresponding relational database (SQL) data source representation in any supported relational database. Relational mappings let you map an object model into a relational data model.

         Relational mappings transform object data members to relational database fields. Use them to map simple data types including primitives (such as int), JDK classes (such as String), and large object (LOB) values. You can also use them to transform object data members that reference other domain objects by way of association where data source representations require object identity maintenance (such as sequencing and back references) and possess various types of multiplicity and navigability. The appropriate mapping class is chosen primarily by the cardinality of the relationship.

Some ORM supporting APIs 

Java - 


 .Net 



Thursday, 19 June 2014

Hibernate Vs IBatis

Hibernate Vs Ibatis

There are major differences between iBatis and Hibernate but both the solutions work well, given their specific domain. Personally I would suggest you should use iBATIS if:
  • You want to create your own SQL's and are willing to maintain them.
  • Your environment is driven by relational data model.
  • You have to work on  existing and complex schema's.
  • Your environment is driven by object model and wants generates SQL automatically.
  • iBATIS is:
  • Hibernate:

And simply use Hibernate if:
You can differentiate between two as  - 
    • Simpler
    • Faster development time
    • Flexible
    • Much smaller in package size
    • Generates SQL for you which means you don't spend time on SQL
    • Provides much more advance cache
    • Highly scalable
Other difference is that iBATIS makes use of SQL which could be database dependent where as Hibernate makes use of HQL which is relatively independent of databases and it is easier to change db in Hibernate.
Hibernate maps your Java POJO objects to the Database tables where as iBatis maps the ResultSet from JDBC API to your POJO Objets.
If you are using stored procedures, well you can do it in Hibernate but it is little difficult in comparision of iBATIS. As an alternative solution iBATIS maps results sets to objects, so no need to care about table structures. This works very well for stored procedures, works very well for reporting applications, etc
Finally, Hibernate and iBATIS both are open source Object Relational Mapping(ORM) tools available in the industry. Use of each of these tools depends on the context you are using them. Hibernate and iBatis both also have good support from SPRING framework so it should not be a problem to chose one of them.