what is SessionFactory in hibernate


I would like to know "what is SessionFactory in hibernate" and whther is is threadsafe by default or not and if it is Yes then why. Any suggestions, ideas, resources, links highly appreciated. Thanks in advance
Who is Participating?
Gurvinder Pal SinghCommented:
When you are carrying out a transaction, transaction manager of your server will get the session from the session factory. This session holds the data for one transaction, which can be viewed as a thread also. Now, other threads must not access (or hack into) this session. So, this session and the transaction is made thread-safe, other-wise the developer has to take care of session being thread-safe.

Please read the following for more info on this
SessionFactory is an interface
Its implementors must be thread safe.
Simple sample:

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Sathish David Kumar NArchitectCommented:

     Hibernate Have 2 type of Cache , Level-1 ,level 2
      Level 2 cache is know as sessionFactory !! (1st one is session )

In ur hibernate config xml u must give sessionFactory !!



        <property name="connection.username"></property>
        <property name="connection.url"></property>
        <property name="dialect">org.hibernate.dialect.</property>
        <property name="myeclipse.connection.profile"></property>
        <property name="connection.password"></property>
        <property name="connection.driver_class"></property>


if u want manuly define that

Configuration cfg = new Configuration()
SessionFactory sF = cfg.buildSessionFactory();
Sathish David Kumar NArchitectCommented:
Usees of Session factory !!

if u excute one query like select emp from emp table  menas ... 1st time only it will hi the DB

if the user excute the same query means  session or sessionfactory give the result without hit the DB

<<<<<<<<<<whther is is threadsafe by default or not and if it is Yes then why

SessionFactory is Hibernate's concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. A SessionFactory is usually only built once at startup. SessionFactory should be wrapped in some kind of singleton so that it can be easily accessed in an application code.
gudii9Author Commented:
>>>SessionFactory is Hibernate's concept of a single datastore and is threadsafe so that many threads can access it concurrently

I thought threadsafe means many threads cannot access same resource, concurrently.

I could not exactly understand meaning of threadsafe in this context. Please advise
Sathish David Kumar NArchitectCommented:
ya ur right threadsafe  means single at a time only...

but as per sessionFactory u can lock the table or flied level so that filed or table cant be update or modify the the table but u can view the table !!
gudii9Author Commented:
Thank you very much for your help. I appreciate it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.