Thursday, 26 April 2018

session-per-request in hibernate

The most common pattern in a multi-user client/server application is session-per-request. In this model, a request from the client is sent to the server, where the Hibernate persistence layer runs. A new Hibernate Session is opened, and all database operations are executed in this unit of work. On completion of the work, and once the response for the client has been prepared, the session is flushed and closed. Use a single database transaction to serve the clients request, starting and committing it when you open and close the Session. The relationship between the two is one-to-one and this model is a perfect fit for many applications.

What does sessionFactory.getCurrentSession() do?

you can call it as many times and anywhere you like once you get hold of your org.hibernate.SessionFactory. The getCurrentSession() method always returns the "current" unit of work.
Remember that we switched the configuration option for this mechanism to "thread" in our src/main/resources/hibernate.cfg.xml? Due to that setting,
 the context of a current unit of work is bound to the current Java thread that executes the application.

 When the transaction ends, either through commit or rollback, Hibernate automatically
 unbinds the org.hibernate.Session from the thread and closes it for you. If you call
 getCurrentSession() again,
 you get a new org.hibernate.Session and can start a new unit of work.

 The scope of a Hibernate org.hibernate.Session is flexible but you should never design your application to use a new Hibernate org.hibernate.Session for every database operation. Even though it is used in the following examples,
 consider session-per-operation an anti-pattern


  1.  private List listEvents() {
  2.         Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  3.         session.beginTransaction();
  4.         List result = session.createQuery("from Event").list();
  5.         session.getTransaction().commit();
  6.         return result;
  7.     }

Advantage and Disadvantage of Interface

Interface:
 only know the customer requirement and don't know the implementation than go for interface.


Advantage:


  • 100% abstract
  • Multiple inheritance
  • All variables are public static and final.
  • all methods are abstract 
Disadvantage:

  • can't have contractor
  • can't serialized the object because you can't create the object of Interface.
  • can't declare method as static , final, synchronized
  • can't declare variable with private,protected,transient,volatile
  • can't declare instance block or static block for interface.

Friday, 20 April 2018

Primitive Data Types


A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are:

Type
Bit size
Range
Default value
byte
8-bit
f -128 and a maximum value of 127 (inclusive)
0
short
16-bit
 -32,768 and a maximum value of 32,767
0
int
32-bit
-231 and a maximum value of 231-1.
0
long
64-bit
-263 and a maximum value of 263-1
0
float
32-bit IEEE 754 floating poin

0.0f
double
64-bit IEEE 754 floating poin

0.0d
boolean

True,false
false
char
6-bit Unicode character
 minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive
'\u0000'

Locks in java






If you want to share or update the same data then the activity must be synchronized to avoid the error.in java using synchronize, wait, and notify keyword we can achieve in java.

If you want to perform and share same data then the activity must have control over it or you can say have the lock.








There are 4 type of lock:

1. Fat lock: where multiple thread have waited simultaneously for the lock.

2. Thin lock:  No thread waited for this lock,

3. Recursive lock: Same thread have taken the lock server times.

3. Lazy lock: Lock accrued by thread and not sure will it release or not

Thursday, 19 April 2018

Heap memory area in java


Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.

The heap is sometimes divided into two areas (or generations) called the nursery (or young space) and the old space. The nursery is a part of the heap reserved for allocation of new objects. When the nursery becomes full, garbage is collected by running a special young collection, where all objects that have lived long enough in the nursery are promoted(moved) to the old space, thus freeing up the nursery for more object allocation. When the old space becomes full garbage is collected there, a process called an old collection.

The reasoning behind a nursery is that most objects are temporary and short lived. A young collection is designed to be swift at finding newly allocated objects that are still alive and moving them away from the nursery. Typically, a young collection frees a given amount of memory much faster than an old collection or a garbage collection of a single-generational heap (a heap without a nursery).

The heap is the run-time data area from which memory for all class instances and arrays is allocated.
The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary.


if a computation requires more heap than can be made available by the automatic storage management system, the Java Virtual Machine throws anOutOfMemoryError.


  • heavy Java process, insufficient Heap size will cause the popular java.lang.OutOfMemoryError: 




Change the size of Heap.

-Xms<size> - Set initial Java heap size
-Xmx<size> - Set maximum Java heap size

Thursday, 12 April 2018

Why Map is not child interface of Collection ?

Now a days in interview interviewer always ask questions related to Collection and map
And they also ask why map interface is not child of collection interface.

Here is very simple resion

Collection talks about individual object. in collection we can store only individual objects.
Where Map talks about storing  the object as key and value pairs. here key and value are two objects.

That's why Map doesn't extends the Collection interface.

session-per-request in hibernate

The most common pattern in a multi-user client/server application is session-per-request . In this model, a request from the client is sent...