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

x
?
Solved

what is SessionFactory in hibernate

Posted on 2010-01-06
8
Medium Priority
?
1,600 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:gudii9
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 4

Assisted Solution

by:liorfr
liorfr earned 200 total points
ID: 26195190
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
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26197063
0
 
LVL 20

Expert Comment

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

Author Comment

by:gudii9
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
0
 
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 !!
0
 
LVL 40

Accepted Solution

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

Author Closing Comment

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

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