Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


what is SessionFactory in hibernate

Posted on 2010-01-06
Medium Priority
Last Modified: 2013-11-23

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
Question by:gudii9
  • 3
  • 2
  • 2
  • +1

Assisted Solution

liorfr earned 200 total points
ID: 26195190
SessionFactory is an interface
Its implementors must be thread safe.
Simple sample:

LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26197063
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26197328

     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();
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 800 total points
ID: 26197363
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.

Author Comment

ID: 26212498
>>>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
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26212618
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 !!
LVL 40

Accepted Solution

Gurvinder Pal Singh earned 1000 total points
ID: 26265091
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

Author Closing Comment

ID: 31673751
Thank you very much for your help. I appreciate it.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question