10 conversations new

14
Professional Open Source™ © JBoss, Inc. 2003, 2004. 1 07/17/04 Conversations

Upload: thirumuru2012

Post on 18-Dec-2014

108 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 10 conversations new

Professional Open Source™

© JBoss, Inc. 2003, 2004. 1

07/17/04

Conversations

Page 2: 10 conversations new

© JBoss, Inc. 2003, 2004. 2

Professional Open Source™

Session Per Request

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

Page 3: 10 conversations new

© JBoss, Inc. 2003, 2004. 3

Professional Open Source™

What is Conversation ?

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

Page 4: 10 conversations new

© JBoss, Inc. 2003, 2004. 4

Professional Open Source™

Propagation through thread-local

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.

Page 5: 10 conversations new

© JBoss, Inc. 2003, 2004. 5

Professional Open Source™

Binding session to thread

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.

Page 6: 10 conversations new

© JBoss, Inc. 2003, 2004. 6

Professional Open Source™

Propagation with JTA

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

Page 7: 10 conversations new

© JBoss, Inc. 2003, 2004. 7

Professional Open Source™

Conversations with detached objects

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.

Page 8: 10 conversations new

© JBoss, Inc. 2003, 2004. 8

Professional Open Source™

How is isolation guaranteed in this strategy ?

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

persistent classes.

Page 9: 10 conversations new

© JBoss, Inc. 2003, 2004. 9

Professional Open Source™

How can u make the conversation atomic?

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

Page 10: 10 conversations new

© JBoss, Inc. 2003, 2004. 10

Professional Open Source™

Extending a Session for a conversation

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 :

Page 11: 10 conversations new

© JBoss, Inc. 2003, 2004. 11

Professional Open Source™

Extending a Session for a conversation

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.

Page 12: 10 conversations new

© JBoss, Inc. 2003, 2004. 12

Professional Open Source™

Delaying insertion until flush-time

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 .

Page 13: 10 conversations new

© JBoss, Inc. 2003, 2004. 13

Professional Open Source™

How to rollback a conversation ???

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.

Page 14: 10 conversations new

© JBoss, Inc. 2003, 2004. 14

Professional Open Source™

Managing the current Session

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….