SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Professional Open Source™




      Conversations




© JBoss, Inc. 2003, 2004.                    07/17/04   1
Session Per Request
                                                                      Professional Open Source™


  The scope of the persistence context is often the same scope
  as the database transaction. This is also known as session-per-
   request .




                            See the example in conversation project


© JBoss, Inc. 2003, 2004.                                                                         2
What is Conversation ?
                                                       Professional Open Source™




  Conversations are units of work that span user think-time.




© JBoss, Inc. 2003, 2004.                                                          3
Propagation through thread-local
                                                              Professional Open Source™


  If sessionFactory.getCurrentSession() is called for the first time in the
   current Java thread, a new Session is opened and returned—you get
   a fresh persistence context.

  You can immediately begin a database transaction on this new
   Session, with the Hibernate Transaction interface

  All the data-access code that calls getCurrentSession() on the global
   shared SessionFactory gets access to the same current Session—if
   it’s called in the same thread. The unit of work completes when the
   Transaction is committed (or rolled back). Hibernate also flushes
   and closes the current Session and its persistence context if you
   commit or roll back the transaction.

  U should not close the session explicitly.


© JBoss, Inc. 2003, 2004.                                                                 4
Binding session to thread
                                                           Professional Open Source™


  Internally, Hibernate binds the current Session to the currently
   running Java thread.
  In the Hibernate community, this is also known as the ThreadLocal
   Session pattern.

  You have to enable this binding in your Hibernate configuration by
   setting as follows :
  hibernate.current_session_context_class = thread.




© JBoss, Inc. 2003, 2004.                                                              5
Propagation with JTA
                                                           Professional Open Source™


  The current Session is bound automatically to the current JTA system
   transaction.

  When the transaction completes, either through commit or rollback,
   the persistence context is flushed and the internally bound current
   Session is closed.

  You have to enable this binding in your Hibernate configuration by
   setting as follows :
  hibernate.current_session_context_class = jta




© JBoss, Inc. 2003, 2004.                                                              6
Conversations with detached objects
                                                          Professional Open Source™


  Let us assume a conversation that has two steps: The first step loads
   an object, and the second step makes changes to the loaded object
   persistent.




© JBoss, Inc. 2003, 2004.                                                             7
How is isolation guaranteed in this strategy ?
                                                           Professional Open Source™


  Isolation is guaranteed with optimistic locking.
  So, U need to enable Hibernate’s automatic versioning for all
   persistent classes.




© JBoss, Inc. 2003, 2004.                                                              8
How can u make the conversation atomic?
                                                          Professional Open Source™


  One solution is to not flush the persistence contexts on commit—that
   is, to set a FlushMode.MANUAL on a Session that isn’t supposed to
   persist modifications

  Another option is to use compensation actions that undo any step
   that made permanent changes, and to call the appropriate
   compensation actions when the user aborts the conversation. But this
   requires a lot of work from the application developer




© JBoss, Inc. 2003, 2004.                                                             9
Extending a Session for a conversation
                                                         Professional Open Source™


  The Hibernate Session has an internal persistence context. You can
   implement a conversation that doesn’t involve detached objects by
   extending the persistence context to span the whole conversation.
   This is known as the session-per-conversation strategy, as shown
   below :




© JBoss, Inc. 2003, 2004.                                                            10
Extending a Session for a conversation
                                                           Professional Open Source™


  A new Session and persistence context are opened at the beginning
   of a conversation.
  The Session is automatically disconnected from the underlying
  JDBC Connection as soon as you commit the database transaction.

  You can now hold on to this disconnected Session and its internal
   persistence context during user think-time. As soon as the user
   continues in the conversation and executes the next step, you
   reconnect the Session to a fresh JDBC Connection by beginning
  a second database transaction.

  Any object that has been loaded in this conversation is in persistent
   state: It’s never detached. Hence, all modifications you made to any
   persistent object are flushed to the database as soon as you call
  flush() on the Session.

© JBoss, Inc. 2003, 2004.                                                              11
Delaying insertion until flush-time
                                                              Professional Open Source™


  The save() method on the Session requires that the new database
   identifier of the saved instance must be returned. So, the identifier
   value has to be generated when the save() method is called. This is
   no problem with most identifier generator strategies; for example,
   Hibernate can call a sequence, do the in-memory increment, or ask
   the hilo generator for a new value. Hibernate doesn’t have to execute
   an SQL INSERT to return the identifier value on save() and assign it
   to the now-persistent instance.

  The exceptions are identifier-generation strategies that are triggered
   after the INSERT occurs. One of them is identity, the other is select;
   both require that a row is inserted first. If you map a persistent class
   with these identifier generators, an immediate INSERT is executed
   when you call save()!
  So, we should avoid these identifier generation strategies .If we use,
   we have to write the compensation actions when we rollback the
   conversation .
© JBoss, Inc. 2003, 2004.                                                                 12
How to rollback a conversation ???
                                                           Professional Open Source™


  As we have enabled FlushMode.MANUAL , the changes made to
   the persistent objects will not cause any DML statements until we
   flush the session .

  Simply close the session with out flushing it. So, the changes made to
   the persistent objects in this session will not be propagated to the
   database.




© JBoss, Inc. 2003, 2004.                                                              13
Managing the current Session
                                                          Professional Open Source™


  You have to enable the following setting :
  hibernate.current_session_context_class = managed.
  The Hibernate built-in implementation you just enabled is called
   managed because it delegates the responsibility for managing the
   scope, the start and end of the current Session, to you. You manage
   the scope of the Session with three static methods:
  public class ManagedSessionContext implements
   CurrentSessionContext {
  public static Session bind(Session session) { ... }
  public static Session unbind(SessionFactory factory) { ... }
  public static boolean hasBind(SessionFactory factory) { ... }
 }

  U must write ur own interceptor or filter for this .
  See the example….

© JBoss, Inc. 2003, 2004.                                                             14

Weitere ähnliche Inhalte

Ähnlich wie Professional Open Source Session Per Request Strategy

01 persistence and domain modeling
01 persistence and domain modeling01 persistence and domain modeling
01 persistence and domain modelingthirumuru2012
 
02 hibernateintroduction
02 hibernateintroduction02 hibernateintroduction
02 hibernateintroductionthirumuru2012
 
12 global fetching strategies
12 global fetching strategies12 global fetching strategies
12 global fetching strategiesthirumuru2012
 
Hibernate complete Training
Hibernate complete TrainingHibernate complete Training
Hibernate complete Trainingsourabh aggarwal
 
1-introduction to ejb
1-introduction to ejb1-introduction to ejb
1-introduction to ejbashishkirpan
 
Module 3: Working with Jazz Source Control
Module 3: Working with Jazz Source ControlModule 3: Working with Jazz Source Control
Module 3: Working with Jazz Source ControlIBM Rational software
 
06 association of value types
06 association of value types06 association of value types
06 association of value typesthirumuru2012
 
Free EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsFree EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsVirtual Nuggets
 
Hibernate Interview Questions | Edureka
Hibernate Interview Questions | EdurekaHibernate Interview Questions | Edureka
Hibernate Interview Questions | EdurekaEdureka!
 
Managing enterprise with PowerShell remoting
Managing enterprise with PowerShell remotingManaging enterprise with PowerShell remoting
Managing enterprise with PowerShell remotingConcentrated Technology
 
159747608 a-training-report-on
159747608 a-training-report-on159747608 a-training-report-on
159747608 a-training-report-onhomeworkping7
 
HTTP Session Replication with Oracle Coherence, GlassFish, WebLogic
HTTP Session Replication with Oracle Coherence, GlassFish, WebLogicHTTP Session Replication with Oracle Coherence, GlassFish, WebLogic
HTTP Session Replication with Oracle Coherence, GlassFish, WebLogicOracle
 
Sanboxed Solutions SharePoint 2010
Sanboxed Solutions SharePoint 2010Sanboxed Solutions SharePoint 2010
Sanboxed Solutions SharePoint 2010Salman Ghani
 

Ähnlich wie Professional Open Source Session Per Request Strategy (20)

01 persistence and domain modeling
01 persistence and domain modeling01 persistence and domain modeling
01 persistence and domain modeling
 
02 hibernateintroduction
02 hibernateintroduction02 hibernateintroduction
02 hibernateintroduction
 
Spring & hibernate
Spring & hibernateSpring & hibernate
Spring & hibernate
 
12 global fetching strategies
12 global fetching strategies12 global fetching strategies
12 global fetching strategies
 
14 hql
14 hql14 hql
14 hql
 
Hibernate complete Training
Hibernate complete TrainingHibernate complete Training
Hibernate complete Training
 
Hibernate3 q&a
Hibernate3 q&aHibernate3 q&a
Hibernate3 q&a
 
1-introduction to ejb
1-introduction to ejb1-introduction to ejb
1-introduction to ejb
 
jsf2 Notes
jsf2 Notesjsf2 Notes
jsf2 Notes
 
Module 3: Working with Jazz Source Control
Module 3: Working with Jazz Source ControlModule 3: Working with Jazz Source Control
Module 3: Working with Jazz Source Control
 
UNIT 4.pptx
UNIT 4.pptxUNIT 4.pptx
UNIT 4.pptx
 
06 association of value types
06 association of value types06 association of value types
06 association of value types
 
Free EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsFree EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggets
 
Hibernate Interview Questions | Edureka
Hibernate Interview Questions | EdurekaHibernate Interview Questions | Edureka
Hibernate Interview Questions | Edureka
 
Managing enterprise with PowerShell remoting
Managing enterprise with PowerShell remotingManaging enterprise with PowerShell remoting
Managing enterprise with PowerShell remoting
 
159747608 a-training-report-on
159747608 a-training-report-on159747608 a-training-report-on
159747608 a-training-report-on
 
Jboss Tutorial Basics
Jboss Tutorial BasicsJboss Tutorial Basics
Jboss Tutorial Basics
 
HTTP Session Replication with Oracle Coherence, GlassFish, WebLogic
HTTP Session Replication with Oracle Coherence, GlassFish, WebLogicHTTP Session Replication with Oracle Coherence, GlassFish, WebLogic
HTTP Session Replication with Oracle Coherence, GlassFish, WebLogic
 
15 jpa
15 jpa15 jpa
15 jpa
 
Sanboxed Solutions SharePoint 2010
Sanboxed Solutions SharePoint 2010Sanboxed Solutions SharePoint 2010
Sanboxed Solutions SharePoint 2010
 

Mehr von thirumuru2012

Mehr von thirumuru2012 (6)

15 jpa introduction
15 jpa introduction15 jpa introduction
15 jpa introduction
 
14 criteria api
14 criteria api14 criteria api
14 criteria api
 
12 hibernate int&cache
12  hibernate int&cache12  hibernate int&cache
12 hibernate int&cache
 
07 association of entities
07 association of entities07 association of entities
07 association of entities
 
05 inheritance
05 inheritance05 inheritance
05 inheritance
 
15 jpaql
15 jpaql15 jpaql
15 jpaql
 

Kürzlich hochgeladen

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Professional Open Source Session Per Request Strategy

  • 1. Professional Open Source™ Conversations © JBoss, Inc. 2003, 2004. 07/17/04 1
  • 2. Session Per Request Professional Open Source™  The scope of the persistence context is often the same scope  as the database transaction. This is also known as session-per- request . See the example in conversation project © JBoss, Inc. 2003, 2004. 2
  • 3. What is Conversation ? Professional Open Source™  Conversations are units of work that span user think-time. © JBoss, Inc. 2003, 2004. 3
  • 4. Propagation through thread-local Professional Open Source™  If sessionFactory.getCurrentSession() is called for the first time in the current Java thread, a new Session is opened and returned—you get a fresh persistence context.  You can immediately begin a database transaction on this new Session, with the Hibernate Transaction interface  All the data-access code that calls getCurrentSession() on the global shared SessionFactory gets access to the same current Session—if it’s called in the same thread. The unit of work completes when the Transaction is committed (or rolled back). Hibernate also flushes and closes the current Session and its persistence context if you commit or roll back the transaction.  U should not close the session explicitly. © JBoss, Inc. 2003, 2004. 4
  • 5. Binding session to thread Professional Open Source™  Internally, Hibernate binds the current Session to the currently running Java thread.  In the Hibernate community, this is also known as the ThreadLocal Session pattern.  You have to enable this binding in your Hibernate configuration by setting as follows :  hibernate.current_session_context_class = thread. © JBoss, Inc. 2003, 2004. 5
  • 6. Propagation with JTA Professional Open Source™  The current Session is bound automatically to the current JTA system transaction.  When the transaction completes, either through commit or rollback, the persistence context is flushed and the internally bound current Session is closed.  You have to enable this binding in your Hibernate configuration by setting as follows :  hibernate.current_session_context_class = jta © JBoss, Inc. 2003, 2004. 6
  • 7. Conversations with detached objects Professional Open Source™  Let us assume a conversation that has two steps: The first step loads an object, and the second step makes changes to the loaded object persistent. © JBoss, Inc. 2003, 2004. 7
  • 8. How is isolation guaranteed in this strategy ? Professional Open Source™  Isolation is guaranteed with optimistic locking.  So, U need to enable Hibernate’s automatic versioning for all persistent classes. © JBoss, Inc. 2003, 2004. 8
  • 9. How can u make the conversation atomic? Professional Open Source™  One solution is to not flush the persistence contexts on commit—that is, to set a FlushMode.MANUAL on a Session that isn’t supposed to persist modifications  Another option is to use compensation actions that undo any step that made permanent changes, and to call the appropriate compensation actions when the user aborts the conversation. But this requires a lot of work from the application developer © JBoss, Inc. 2003, 2004. 9
  • 10. Extending a Session for a conversation Professional Open Source™  The Hibernate Session has an internal persistence context. You can implement a conversation that doesn’t involve detached objects by extending the persistence context to span the whole conversation. This is known as the session-per-conversation strategy, as shown below : © JBoss, Inc. 2003, 2004. 10
  • 11. Extending a Session for a conversation Professional Open Source™  A new Session and persistence context are opened at the beginning of a conversation.  The Session is automatically disconnected from the underlying  JDBC Connection as soon as you commit the database transaction.  You can now hold on to this disconnected Session and its internal persistence context during user think-time. As soon as the user continues in the conversation and executes the next step, you reconnect the Session to a fresh JDBC Connection by beginning  a second database transaction.  Any object that has been loaded in this conversation is in persistent state: It’s never detached. Hence, all modifications you made to any persistent object are flushed to the database as soon as you call  flush() on the Session. © JBoss, Inc. 2003, 2004. 11
  • 12. Delaying insertion until flush-time Professional Open Source™  The save() method on the Session requires that the new database identifier of the saved instance must be returned. So, the identifier value has to be generated when the save() method is called. This is no problem with most identifier generator strategies; for example, Hibernate can call a sequence, do the in-memory increment, or ask the hilo generator for a new value. Hibernate doesn’t have to execute an SQL INSERT to return the identifier value on save() and assign it to the now-persistent instance.  The exceptions are identifier-generation strategies that are triggered after the INSERT occurs. One of them is identity, the other is select; both require that a row is inserted first. If you map a persistent class with these identifier generators, an immediate INSERT is executed when you call save()!  So, we should avoid these identifier generation strategies .If we use, we have to write the compensation actions when we rollback the conversation . © JBoss, Inc. 2003, 2004. 12
  • 13. How to rollback a conversation ??? Professional Open Source™  As we have enabled FlushMode.MANUAL , the changes made to the persistent objects will not cause any DML statements until we flush the session .  Simply close the session with out flushing it. So, the changes made to the persistent objects in this session will not be propagated to the database. © JBoss, Inc. 2003, 2004. 13
  • 14. Managing the current Session Professional Open Source™  You have to enable the following setting :  hibernate.current_session_context_class = managed.  The Hibernate built-in implementation you just enabled is called managed because it delegates the responsibility for managing the scope, the start and end of the current Session, to you. You manage the scope of the Session with three static methods:  public class ManagedSessionContext implements CurrentSessionContext {  public static Session bind(Session session) { ... }  public static Session unbind(SessionFactory factory) { ... }  public static boolean hasBind(SessionFactory factory) { ... } }  U must write ur own interceptor or filter for this .  See the example…. © JBoss, Inc. 2003, 2004. 14