Results 1 to 7 of 7

Thread: What are considerations for creating user threads when using OSIV?

  1. #1
    Join Date
    Aug 2004
    Posts
    1,905

    Default What are considerations for creating user threads when using OSIV?

    Hi all,

    I am using OSIV (filter because it is a web app) but I want to create some user threads that need to access the same hibernate session, or at least they should take part in the same transaction)

    Should each thread have it's own session, or is session multithreaded?

    Is it as simple as replicating the behaviour of OSIV for each user thread I create?

    I suppose I am asking; can I use OSIV for multiple threads, or should I view each thread as it's own request, each having it's own invocation of OSIV (you get what I mean).

    Has anyone else tried this?

    Thanks all,

    Col

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Not sure what your use case is but since HB sessions are not thread-safe, the easiest way is to have one session per thread. Even if you spawn child threads which will end before the parent thread starts, you can create sessions which are part of the same transaction by reusing the underlying connection:
    Session newThreadSession = sessionFactory.openSession(getCurrentSession().get Connection());
    I assume there are other means to share the transaction/session connection - as I said it depends on what is your requirement.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Once use case that was discussed in this forum was the usage of Quartz task with lazy objects. Since quartz threads and jobs are managed outside your application, the easiest solution (IMO at least) was to open a new session and reattach the objects to it.
    You can do the same thing in your case.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  4. #4
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Quote Originally Posted by costin
    Not sure what your use case is but since HB sessions are not thread-safe, the easiest way is to have one session per thread. Even if you spawn child threads which will end before the parent thread starts, you can create sessions which are part of the same transaction by reusing the underlying connection:


    I assume there are other means to share the transaction/session connection - as I said it depends on what is your requirement.
    Essentially I have a Page which is made up of many Sections, each Section get's it data from different places (UrlBackedSection, FileBackedSection etc.).

    Rather then asking each section to retrieve it's data sequentially, I want to do it in it's own thread.... All of this happens just before rendering (i.e. after the filter stack has done it's thing..)

    I will have a look at your suggestion.

    Thanks again

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    A bit more info.... I don't explictly want to access the session; I want lazy loading of objects to happen.

    So:

    - OSIV starts binds session to single thread
    - load pageA which is attached to session
    - start new thread, traverse pageA.getSomeLazilyLoadedAccessor()

    etc.

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    I am not aware of any magic bullet solution. If want to do processing in parallel, you can put a barrier on the main thread, start the child threads and in each thread open a session and reattach the object to it.
    Even if you use something similar to OSIV so that the session is opened after the thread starts, you still have to reattach the objects since they use the main session thread.
    However, since the main thread (and thus the HB session) are not closed until the page is rendered, you can simply just pass the main session to the child threads which will simply reuse it.
    This way, you don't have to reattach/detach any objects since they are reusing the same session across the threads. You can manually attach the session to Spring internal thread-local by doing a bind/unbind on the TransactionSynchronizationManager at the startup and shutdown of the thread.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  7. #7
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Excellent. Thanks Costin.

Posting Permissions

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