java - getCurrentSession hibernate in web -


I am writing a web based application with hibernate and jsp / servlet. I have read about the sessionFactory.getCurrentSession and sessionFactory.openSession methods. I know the basic difference between them (you do not have to close the connection using getCurrentSession and when you do a transaction, your session will automatically close). According to my understanding, we should choose the option for getCurrentSession and it should be done through a session-per-request

Consider this scenario:

  1. Method used to call a call getCurrentSession and the current session

  2. In Law A, using session from step 1 has started .
  3. Method A call method B, which also includes getCurrentSession and initiate the transaction
  4. Method B reduces its transaction
  5. Returns on method A and also controls this transaction

Now my questions are

  1. Will the session be held in phase 1 and step 3 in the same session?
  2. If the answer to Question 1 is yes, how can I handle it in step 4? Ideally it should close its session and throw an exception in step 5.
  3. If question 1 is not answered, how do you want to handle this kind of scenario?

What sessions will be in session 1 and phase 3 in session? ?

They should be identical, which is part of the contract for getCurrentSession () and you will be bound with the session thread Unless the unit of work is completed (i.e. a transaction has been committed or refunded) Hibernate with Java compulsion This is how it is called (p.481):

All data-access codes that are getCurrentSession () Global shared Sansfinifier reaches the same current session - if it is said in the same thread when the work unit is completed transaction is committed (or rolled back). Hibernation also stops flushes and current sessions and its persistence context. If you return or roll back the transaction here, the implication here is that getCurrentSession () After a call or rollback, a new session and generates a fresh persistence reference.

And you can also read what the answer is.

If the answer to Question 1 is yes, then how will it be committed in step 4? Ideally this session should be closed, the error should be made in step 5.

Step 4 should not be a problem, session will be flushed, transaction will be committed and sessions will stop but I hope that to fail Phase 5 (this is my condition). But let me refer to this javadoc:

A transaction is associated with one session and usually for a call by session.beginTransaction () Is done immediately. Perception of any one session (an interaction between an application and a datastore) Percentage of transactions may be due to thick granularity due to the perception of a transaction. However, it is intended that at any time the most unearned transaction associated with a particular session .

As we have been underlined above, we are discussing something around this should not happen (i.e. a design problem).


Comments