0

If I use getCurrentSession() to obtain session and I don't begin transaction and save gives exception but when I use openSession() to get session then in the same scenario the program runs without any exception."hibernate.current_session_context_class" is configured with thread in hibernate configuration file.So what is the difference when I use getCurrentSession() and openSession().

Here is the code that gets session using getCurrentSession()-

            public static void main(String ar[])
            {

                SessionFactory sfac=null;

                sfac=new Configuration().configure("hibernateconfig/rs.xml")

                        .buildSessionFactory();

                try {

                Session session=sfac.getCurrentSession();
                Student stu=new Student();
                stu.setFirstName("abc23");
                stu.setLastName("xyz");
                System.out.println("-------------"+stu);
                session.save(stu);

                }
                catch(Exception e)
                {
                    System.out.println(e);
                }

            }

output-

-------------Student [id=0, firstName=abc23, lastName=xyz]
org.hibernate.HibernateException: save is not valid without active transaction


Here is the code that gets session using openSession()-

        public static void main(String ar[])
        {

            SessionFactory sfac=null;

            sfac=new Configuration().configure("hibernateconfig/rs.xml")

                    .buildSessionFactory();

            try {

            Session session=sfac.openSession();
            Student stu=new Student();
            stu.setFirstName("abc23");
            stu.setLastName("xyz");
            System.out.println("-------------"+stu);
            session.save(stu);

            }
            catch(Exception e)
            {
                System.out.println(e);
            }

        }


output-

-------------Student [id=0, firstName=abc23, lastName=xyz]
Hibernate: 
    insert 
    into
        student
        (firstName, lastName) 
    values
        (?, ?)
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

1

openSession():
When you call SessionFactory.openSession(), it always create new Session object and give it to you.You need to explicitly flush and close these session objects. As session objects are not thread safe, you need to create one session object per request in multithreaded environment and one session per request in web applications too.

getCurrentSession():
When you call SessionFactory. getCurrentSession(), it will provide you session object which is in hibernate context and managed by hibernate internally. It is bound to transaction scope.When you call SessionFactory.getCurrentSession() , it creates a new Session if not exists , else use same session which is in current hibernate context. It automatically flush and close session when transaction ends, so you do not need to do externally.If you are using hibernate in single threaded environment , you can use getCurrentSession(), as it is faster in performance as compare to creating new session each time.You need to add following property to hibernate.cfg.xml to use getCurrentSession method. thread

Sagarkommu
  • 11
  • 1