Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By Tolls

Thread: Is getCurrentSession() performance issue call many times

  1. #1
    stsivaraj is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Is getCurrentSession() performance issue call many times

    Hi All, I am calling sessionFactory.getCurrentSession() in a thread many times(Current Session from thread local). May I know is there any performance issue to access the session from thread local for each time ?
    Suggest me, which one is best way,

    Way1:
    =====
    public void update(){
    Session session=sessionFactory.getCurrentSession() ;
    ............
    ............
    updateMaster();
    }
    public void updateMaster(){
    Session session=sessionFactory.getCurrentSession() ;
    ............
    ............
    }

    or

    Way2:
    =====
    public void update(){
    Session session=sessionFactory.getCurrentSession() ;
    ............
    ............
    updateMaster(session);
    }
    public void updateMaster(Session session){
    ............
    ............
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Is getCurrentSession() performance issue call many times

    Depends on the session factory, but I doubt it's going to be an issue. It's intended to be called. It is not going to be the bottleneck in your code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    stsivaraj is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Re: Is getCurrentSession() performance issue call many times

    Quote Originally Posted by Tolls View Post
    Depends on the session factory, but I doubt it's going to be an issue. It's intended to be called. It is not going to be the bottleneck in your code.
    Thanks Tolls.. I designed the sessionfactory as thread local such way. For each thread I am getting a session from thread local(session pool).
    I am getting the session per thread. I am worrying about, each time calling getCurrentSession() is going to access the thread local. Is it ok or used one place getCurrentSession() and pass the session as argument where we want?

  4. #4
    stsivaraj is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Re: Is getCurrentSession() performance issue call many times

    Tolls, Please suggest me which way is best(performance and standard)

    Way1:
    =====
    public void update(){
    Session session=sessionFactory.getCurrentSession() ;
    ............
    ............
    updateMaster();
    }
    public void updateMaster(){
    Session session=sessionFactory.getCurrentSession() ;
    ............
    updateStub();
    }

    public void updateStub(){
    Session session=sessionFactory.getCurrentSession() ;
    ............
    }

    or

    Way2:
    =====
    public void update(){
    Session session=sessionFactory.getCurrentSession() ;
    ............
    ............
    updateMaster(session);
    }
    public void updateMaster(Session session){
    ............
    updateStub(session);
    }
    public void updateStub(Session session){
    ............
    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Is getCurrentSession() performance issue call many times

    I explained.
    It's not going to be a bottleneck.

    I don't like passing sessions around unless it's in private methods.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Is getCurrentSession() performance issue call many times

    If you don't want to believe Tolls then you will have to do what you should have done right away: measure it yourself! I'll bet you can't because it takes so little time that you would need to do microsecond measurements.

    In other words: you're worrying about micro optimizations. Worry about the big performance gains, such as inefficient queries.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    stsivaraj is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Re: Is getCurrentSession() performance issue call many times

    Quote Originally Posted by Tolls View Post
    I explained.
    It's not going to be a bottleneck.

    I don't like passing sessions around unless it's in private methods.
    Thanks tolls. But In my post I mentioned methods are private only. Thanks again

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Is getCurrentSession() performance issue call many times

    Quote Originally Posted by stsivaraj View Post
    Thanks tolls. But In my post I mentioned methods are private only. Thanks again
    All those methods above, that take a Session parameter, are public.
    That means you intend them to be accessible from outside the DAO (assuming that's what this is part of).
    Which means the Service layer will need to pass around a Session, when it shouldn't really need to know about the persistence side of it at all.
    stsivaraj likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    stsivaraj is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Re: Is getCurrentSession() performance issue call many times

    Yep got it. But it is originally like

    Way1:
    =====
    public void update(){
    Session session=sessionFactory.getCurrentSession() ;
    ............
    ............
    updateMaster();
    }
    private void updateMaster(){
    Session session=sessionFactory.getCurrentSession() ;
    ............
    updateStub();
    }

    private void updateStub(){
    Session session=sessionFactory.getCurrentSession() ;
    ............
    }

    or

    Way2:
    =====
    public void update(){
    Session session=sessionFactory.getCurrentSession() ;
    ............
    ............
    updateMaster(session);
    }
    private void updateMaster(Session session){
    ............
    updateStub(session);
    }
    private void updateStub(Session session){
    ............
    }

    Now it is correct. I supposed to show this code. Sorry for posted wrongly.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Is getCurrentSession() performance issue call many times

    In which case I would get the session once (in the public method) and pass it around internally to the class, so Way 2.
    This is assuming it's a DAO.
    stsivaraj likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    stsivaraj is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Re: Is getCurrentSession() performance issue call many times

    Yeah Tolls. Now I am much clear...Yeah that code I copied from DAO code layer only. Thanks.

Similar Threads

  1. How to call a class into the main method 5 times?
    By Valerie10b in forum New To Java
    Replies: 4
    Last Post: 03-16-2013, 09:02 AM
  2. Replies: 4
    Last Post: 05-25-2012, 03:54 PM
  3. Issue with saving multiple times to a variable
    By sidd0123 in forum New To Java
    Replies: 3
    Last Post: 06-06-2010, 02:02 AM
  4. Performance Issue
    By hiranya in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-24-2008, 11:32 PM
  5. Performance issue
    By mathes_n in forum Web Frameworks
    Replies: 8
    Last Post: 09-02-2008, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •