what is SessionFactory in hibernate

Posted on 2010-01-06
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
    LVL 4

    Assisted Solution

    SessionFactory is an interface
    Its implementors must be thread safe.
    Simple sample:

    LVL 40

    Expert Comment

    LVL 20

    Expert Comment

    by:Sathish David Kumar N

         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();
    LVL 20

    Assisted Solution

    by:Sathish David Kumar N
    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.
    LVL 7

    Author Comment

    >>>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
    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

    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
    LVL 7

    Author Closing Comment

    Thank you very much for your help. I appreciate it.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    factorial example challenge 10 45
    array6 challenfge 6 49
    countAbc challenge 9 35
    wordlen challenge 3 30
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    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…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now