top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is meant bye Cache in Java and What is the difference between first level cache and second level cache?

0 votes
485 views
What is meant bye Cache in Java and What is the difference between first level cache and second level cache?
posted Feb 29, 2016 by Dominic

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

A cache is an area of local memory that holds a copy of frequently accessed data that is otherwise expensive to get or compute.
Examples of such data include a result of a query to a database, a disk file or a report.
A typical interface for a Java cache provides access to the data using a unique key:

Syntax:

public interface Cache
{
Object get(final Object key);
Object put(final Object key, final Object value);
}

A cache works as the following:
An application requests data from cache using a key. If the key is not found, the application retrieves the data from a slow data source and puts it into the cache. The next request for a key is serviced from the cache.

Improving Performance with Caching
Caching may provide significant performance improvement for a Java application, often in orders of magnitude. The performance improvement comes from serving hard to get data from the local memory of the application.

First Level Cache

First-level cache always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one, that means it will not process one transaction many times. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.

Second-level cache

Second-level cache always associates with the Session Factory object. While running the transactions, in between it loads the objects at the Session Factory level, so that those objects will be available to the entire application, not bound to single user. Since the objects are already loaded in the cache, whenever an object is returned by the query, then at that time there is no need to go for a database transaction. In this way the second level cache works. Here we can use query level cache also.

answer Feb 29, 2016 by Josita Sarwan
0 votes

Hibernate is actually a very powerful, consistent, and reliable database mapping tool. Mapping between objects in Java to relational databases has many facets that you must be aware of. Hibernate does a particularly good job of making the process simple to start, and providing the facilities to allow it to scale well and meet exceedingly complex mapping demands.

Caching is all about application performance optimization and it sits between your application and the database to avoid the number of database hits as many as possible to give a better performance for performance critical applications.
Caching is important to Hibernate as well which utilizes a multilevel caching schemes as explained below:

One of the primary concerns of mappings between a database and our Java application is performance. This is the common concern of the all guys who working with hibernate and spent the more time in ORM tools for performance-enhancing changes to particular queries and retrievals. Today I want to discuss about some facets of the Hibernate infrastructure that are implemented to handle certain performance concerns -

The first-level cache - Session (Earlier hibernate already provide this level of cache)

The second-level cache -Session-factory-level cache and the query cache.

The first-level cache:

The first level cache type is the session cache. The session cache caches object within the current session but this is not enough for long level i.e. session factory scope.
The second-level cache: The second-level cache is called 'second-level' because there is already a cache operating for you in Hibernate for the duration you have a session open. A Hibernate Session is a transaction-level cache of persistent data. It is possible to configure a SessionFactory-level cache on a class-by-class and collection-by-collection basis.

second-level cache

                 -- Across sessions in an Application

                 -- Across applications (different applications on same servers with same database)

                 -- Across clusters (different applications on different servers with same database)

NOTE: Be careful Caches are never aware of changes made to the persistent store by another application i.e. suppose one application deploy one server with using hibernate and get the data from database and put to the cache for further using purpose but another application deployed another server which does not using any ORM tool so it does mot know about Cache and direct interacting with database and may be update data of database. Now data in Cache is invalid.

Hibernate uses first-level cache by default and you have nothing to do to use first-level cache. Let's go straight to the optional second-level cache. Not all classes benefit from caching, so it's important to be able to disable the second-level cache.

The 'second-level' cache exists as long as the session factory is alive. The second-level cache holds on to the 'data' for all properties and associations (and collections if requested) for individual entities that are marked to be cached.
In hibernate configuration file (hibernate.cfg.xml)

For Disabling the second level of cache we have to made following change to hibernate configuration file.

org.hibernate.cache.NoCacheProvider

answer Mar 2, 2016 by Karthick.c
Similar Questions
+1 vote

I had a couple *.iso disks in my CD/DVD drive recently. I had looked at the images, but did not install. Now, i have a software update prompt and when I go to install, I am asked to put the two iso's in the drive. If I cancel the request and try to continue with the updates, it fails to complete them.

My google efforts have failed to produce a solution to the problem. How do I clean this up so that I can get back to regular updates?

0 votes

I am developing a program in C and I want to lock some memory pages in cache, particularly in the core specific level (i.e. level 1). As far as I know the C libraries do not provide an interface to do that so I guess
that there must be some particular directives in gcc to do so. I am interested in locking both a struct and some arbitrary variables.

Could any one provide any information for that? If so it would be very helpful.

...