what is SessionFactory in hibernate

Hi,

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
LVL 7
gudii9Asked:
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
https://www.hibernate.org/hib_docs/v3/api/org/hibernate/Transaction.html
http://www.developer.com/open/article.php/10930_3559931_3/Hibernate-Basics.htm
https://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html
0
 
liorfrCommented:
SessionFactory is an interface
Definition:
https://www.hibernate.org/hib_docs/v3/api/org/hibernate/SessionFactory.html
Its implementors must be thread safe.
Simple sample:
http://www.laliluna.de/first-hibernate-example-tutorial.html

Lior
0
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:
HI,

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

Eg:

<hibernate-configuration>

    <session-factory>
        <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>
   
    </session-factory>

</hibernate-configuration>


if u want manuly define that

Configuration cfg = new Configuration()
    .addFile("controller.hbm.xml")
    .addFile("Dao.hbm.xml");
SessionFactory sF = cfg.buildSessionFactory();
0
 
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.
0
 
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
0
 
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 !!
0
 
gudii9Author Commented:
Thank you very much for your help. I appreciate it.
0
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.