Friday, December 18, 2015

Features of Java Version

Features of Java 5

For-each loop
Varargs
Static Import
Autoboxing and Unboxing
Enum
Covariant Return Type
Annotation
Generics



Features of Java 6

Pluggable Annotation Processing API
Common Annotations
Java API for XML Based Web Services – 2.0
JAXB 2.0
Web Services Metadata
Streaming API for XML
XML Digital Signature
Java Class File Specification Update
Java Compiler API
JDBC 4.0
Scripting in the Java Platform


Features of Java 7 (55 New Features)

String in switch statement
Binary Literals
The try-with-resources
Caching Multiple Exceptions by single catch
Underscores in Numeric Literals
Diamond Operator
Fork and Join
New File System API (NIO 2.0)
Concurrency Utilities


Features of Java 8


  • Lambda expression − Adds functional processing capability to Java.

List primeNumbers = ReferenceToStaticMethod.testPredicate(numbers, a -> ReferenceToStaticMethod.isPrime(a));


  • Method references − Referencing functions by their names instead of invoking them directly. Using functions as parameter.

List primeNumbers = ReferenceToStaticMethod.testPredicate(numbers, ReferenceToStaticMethod::isPrime);


  • Default method − Interface to have default method implementation.
  • New tools − New compiler tools and utilities are added like ‘jdeps’ to figure out dependencies.
  • Stream API − New stream API to facilitate pipeline processing.
  • Date Time API − Improved date time API.
  • Optional − Emphasis on best practices to handle null values properly.
  • Nashorn, JavaScript Engine − A Java-based engine to execute JavaScript code.

Pessimistic and Optimistic concurrency control



There are 2 different concurrency control mechanisms
1. Pessimistic concurrency control
2. Optimistic concurrency control

What is the difference between optimistic and pessimistic concurrency control


Pessimistic concurrency involves locking rows to prevent other users from modifying the same data at the same time. Until the lock is released by the lock owner, no other users will be able to access that data. Pessimistic locking can very easily lead to performance bottle necks in an application.

Pessimistic Locking is when you lock the record for your exclusive use until you have finished with it. It has much better integrity than optimistic locking but requires you to be careful with your application design to avoid Deadlocks. To use pessimistic locking you need either a direct connection to the database (as would typically be the case in a two tier client server application) or an externally available transaction ID that can be used independently of the connection.




Optimistic concurrency does NOT involve locking rows when reading. Instead, this model checks if two users tried to update the same record at the same time. If that happens one user's changes are committed and the other user's changes are discarded and an exception will be thrown to notify the user.

Optimistic Locking is a strategy where you read a record, take note of a version number and check that the version hasn't changed before you write the record back.

If the record is dirty (i.e. different version to yours) you abort the transaction and the user can re-start it.

This strategy is most applicable to high-volume systems and three-tier architectures where you do not necessarily maintain a connection to the database for your session. In this situation the client cannot actually maintain database locks as the connections are taken from a pool and you may not be using the same connection from one access to the next.

Saturday, April 4, 2015

ehcache configuration for hibernate

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
 monitoring="autodetect" dynamicConfig="true">
 
 <!-- By default, Ehcache stored the cached files in temp folder. -->
 <!-- <diskStore path="java.io.tmpdir" /> -->
 
 <!-- Ask Ehcache to store cache in this path -->
 <diskStore path="c:\\cache" />
 
 <!-- Sample cache named cache1
    This cache contains a maximum in memory of 10000 elements, and will expire
    an element if it is idle for more than 5 minutes and lives for more than
    10 minutes.
 
    If there are more than 10000 elements it will overflow to the
    disk cache, which in this configuration will go to wherever java.io.tmp is
    defined on your system. On a standard Linux system this will be /tmp" -->
 <cache name="cache1" 
  maxEntriesLocalHeap="10000"
  maxEntriesLocalDisk="1000" 
  eternal="false" 
  diskSpoolBufferSizeMB="20"
  timeToIdleSeconds="300" timeToLiveSeconds="600"
  memoryStoreEvictionPolicy="LFU" 
  transactionalMode="off">
  <persistence strategy="localTempSwap" />
 </cache>
 
</ehcache>




For more information http://www.mkyong.com/ehcache/ehcache-hello-world-example/

Friday, April 3, 2015

Object oriented language vs Object based language

Object oriented language

 

Object based language

·         Object-oriented language supports all the features of OOPs.

·         Object-based language doesn't support all the features of OOPs like Polymorphism and Inheritance
·         Object-oriented language doesn't have in-built object.

·         Object-based language has in-built object like javascript has window object.
·         Object-oriented languages are C++, C#, Java etc.

·         Object-based languages are Javascript, VB etc.

Sunday, March 22, 2015

Hibernate lazy fetch true/false



lazy=false means hibernate will load the objects entities all at once. 

Means it  loads parent records and child records as well in single query. 



lazy=true means hibernate will load the objects whenever they are needed, i.e. any particular entity object will be loaded when that entity object is referenced from your application.


Means it loads only parent records (primary key records) only and its recommended for memory optimizations

Sunday, March 8, 2015

ECLIPSE “CANNOT BE RESOLVED TO A TYPE” ERROR




Try this


Windows–>Preferences–>Java–>Compiler–>Building–>Output folder–>”Rebuild class files modified by others”.

Friday, March 6, 2015

ClassNotFoundException vs NoClassDefFoundError





ClassNotFoundException
NoClassDefFoundError
Thrown when an application tries to load in a class through its name, but no definition for the class with the specified name could be found

Thrown if the JVM tries to load in the definition of a class and no definition of the class could be found.
It occurs when class loader could not find the required class in class path.

So, basically you should check your class path and add the class in the classpath.
This is more difficult to debug and find the reason.

This is thrown when at compile time the required classes are present, but at run time the classes are changed or removed or class's static initializes threw exceptions.

It means the class which is getting loaded is present in classpath, but one of the classes which are required by this class, are either removed or failed to load by compiler.

So you should see the classes which are dependent on this class .

Friday, February 20, 2015

What is .profile in Unix

Environment is defined by environment variables.

 Which are set by:

·         The system
·         Some by you
·         Some by the shell
·         Any program that loads another program.

 Environment variables are set without using $ sign but while accessing them we use $sign as prefix. These variables retain their values until we come out shell.

When you login to the system, the shell undergoes a phase called initialization to set up various environments. This is usually a two-step process that involves the shell reading the following files:
·         /etc/profile
·         profile
The process is as follows:
1.    The shell checks to see whether the file /etc/profile exists.
2.    If it exists, the shell reads it. Otherwise, this file is skipped. No error message is displayed.
3.    The shell checks to see whether the file .profile exists in your home directory. Your home directory is the directory that you start out in after you log in.
4.    If it exists, the shell reads it; otherwise, the shell skips it. No error message is displayed.
As soon as both of these files have been read, the shell displays a prompt:
$
This is the prompt where you can enter commands in order to have them execute.

Usually the type of terminal you are using is automatically configured by either the login or getty programs. Sometimes, the auto configuration process guesses your terminal incorrectly.
If your terminal is set incorrectly, the output of commands might look strange, or you might not be able to interact with the shell properly.

for more information http://www.tutorialspoint.com/unix/unix-environment.htm



Friday, February 13, 2015

Awk script to read and write to file

Awk script to read from a file and format the string and write the out in another file.

Input file

ABC;def;ghi;jkl
Mno;pqr;stu;vwx

Awk script

#!/bin/ksh

FILENAME= $1

awk 'BEGIN {FS=";"} {printf " update table set column= '\''%s'\'' where column= something \n" , $2 > "output file.txt"} ' $FILENAME

…..............

Run script
Scriptname inputfilename

Friday, January 16, 2015

First-level vs Second-level cache Hibernate

First level cache

·         First level cache is associated with “session” object.
·         The scope of cache objects is of session.
·         Once session is closed, cached objects are gone forever.
·         First level cache is enabled by default and you cannot disable it.
·         When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session. If we query same object again with same session object, it will be loaded from cache and no sql query will be executed.
·         The loaded entity can be removed from session using evict() method.
·         The next loading of this entity will again make a database call if it has been removed using evict() method.
·         The whole session cache can be removed using clear() method. It will remove all the entities stored in cache.

Second level cache

·         Second level cache is apart from first level cache which is available to be used globally in session factory scope.
·         Second level cache is created in session factory scope and is available to be used in all sessions which are created using that particular session factory. It also means that once session factory is closed, all cache associated with it die and cache manager also closed down.
·         Whenever hibernate session try to load an entity, the very first place it look for cached copy of entity in first level cache (associated with particular hibernate session).
1.    If cached copy of entity is present in first level cache, it is returned as result of load method.
2.    If there is no cached entity in first level cache, then second level cache is looked up for cached entity. If second level cache has cached entity, it is returned as result of load method. But, before returning the entity, it is stored in first level cache also so that next invocation to load method for entity will return the entity from first level cache itself, and there will not be need to go to second level cache again.
·         If entity is not found in first level cache and second level cache also, then database query is executed and entity is stored in both cache levels, before returning as response of load() method.