Professional Documents
Culture Documents
Session 5
Is your transaction
safe?
–
What is Transaction ?
–
ACID Properties of Transaction
–
EJB Transactions
–
Container Managed Transaction Setup
–
Forcing Rollback
–
How Transaction React to System/Application
Exceptions
•
A transaction is a mechanism to handle
groups of operations as though they were
one
•
In a transaction, either all operations occur
or none at all.
•
Operations involved in a transaction may
rely on many different servers and
databases.
•
A transaction is formally defined by the set
of properties known by the acronym ACID
•
The acronym of ACID stands for:
•
Atomic
•
Consistent
•
Isloated
•
Durable
A transactions can be determined:
By a client around one or more method invocations
−
This is an example of a
Clien client
t demarcated
transaction. Notice that
tx1.begin(); the transaction can
span one or more
EJBs, one or more
EJB1.method();
methods, and one
or more databases.
EJB2.method();
tx1.commit();
Transactions Grey Matter, Sella Synergy India 2min
The <transaction-type> Tag
The <transaction-type> tag:
−
Specifies whether a bean uses container or bean managed
transactions
Used only with <session> or <message-driven>
−
Examples:
<enterprise-beans>
<sessions>
<ejb-name>JspSession</ejb-name>
...
<transaction-type>Container</transaction-type>
</session>
Transactions Grey Matter, Sella Synergy India 2min
Container-Managed Transactions
CMT
Are started and stopped by the container before and after a
method invocations
Is configured in the deployment descriptor
Is mandatory for entity EJBs.
container ●
Etc. 3
starts a new
invoke()
invoke()
Component
transaction
1 From
for a single
Client Post - invocation4
method call. Container ●
DB Store
●
Commit / Rollback TX
●
Etc.
•
The <Container-transaction> tag declares how
container-managed transactions should work for one
or more methods.
<container-transaction> Example:
...
</ enterprise-beans>
<assembly-descriptors>
<container-transactions>
<method>
<ejb-name>JspSession</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
. . . </assembly-descriptor>
•
The <trans-attribute>tag can have six values
Never
−
Mandatory
−
NotSupported
− This is the
Supports
− default,
Required
−
when
transaction
RequiresNew
−
attributes are
not specified
•
Never specifies that the EJB must never be invoked within the
context of a transactions
Active
Transaction
Scenario 1 (Client TX): No Active
Transaction EJ
Client 1 – begin Contai B
2 – method ner
Remote Exception
•
Mandatory transactions require the client to pass a global
transaction to the EJB
Active
Transaction
Scenario 1 (Client TX): No Active
Transaction EJ
Client 1 – begin Contai method B
2 – method ner
3–
Commit
Transaction
Transactions Grey Matter, Sella Synergy India
Required Exception 2min
NotSupported
•
NotSupported cause client transaction to be suspended during
the EJB's execution.
Active
Transaction
Scenario 1 (Client TX): No Active
Transaction EJ
Client 1 – begin Contai method B
2 – method ner
3–
Commit
•
Supports implies that the EJB will be included in any client-
started transactions.
Active
Transaction
Scenario 1 (Client TX): No Active
Transaction EJ
Client 1 – begin Contai method B
2 – method ner
3–
Commit
•
Required transactions force the EJB to be executed within a
transaction started either by the client or the container
Active
Transaction
Scenario 1 (Client TX): No Active
Transaction EJ
Client 1 – begin Contai method B
2 – method ner
3–
Commit
•
RequiresNew transactions force the EJB to execute within a new
transaction that can only be started the container
Active
Transaction
No Active
Scenario 1 (Client TX): Transaction
New EJ
Client 1 – begin Contai Transaction B
method
2 – method ner
3–
Commit
ejb-jar.xml Snippet:
<enterprise-beans>
<session>
<ejb-name> AnEJB</ejb-name>
...
<transaction – type>Containers</transaction – type>
</session>
</enterprise-bean>
...
<assembly-descriptor>
<container – transaction>
<method>
<ejb-name>AnEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
TRansactioins Grey Matter, Sella Synergy India 2min
Puzzle Corner
•
The transaction isolation level controls the behavior of a
transaction, by balancing:
The integrity of the data vs
−
•
A Dirty Read happens when reading a row that was modified by
someone else (and that person did not commit yet).
•
A Non-repeatable Read happens when reading a new committed
value while in the middle of a transaction (i.e. The data change
during processing)
•
A Phantom Row happens when reading a row that was deleted by
someone else ( and that person did not commit yet)
•
TRANSACTION_READ_UNCOMMITTED
Enables reading in modified data even if that data was not committed
−
yet
•
TRANSACTION_READ_COMMITTED
•
TRANSACTION_REPEATABLE_READ
−
Enables reading in modified data if it has been committed, but with the
guarantee that the data will not change again
−
Does NOT allow dirty reads and non-repeatable reads
−
Allows phantom rows
•
TRANSACTION_SERIALIZABLE
−
Locks data so that only 1 transaction at a time can access it: transactions
are done in 'series'
−
Is the most restrictive transaction isolation level, resulting in the highest
integrity
−
Is the slowest transaction isolation level
−
Does NOT allow dirty reads, phantom rows and non-repeatable reads.
•
If possible, always use a CMT
•
Think about transaction granularity when designing
EJBs:
Transaction configuration can have a big impact on
−
performance.
−
Try to limit a web request to a single transaction
−
Do not have transactions span user input
−
Try creating a facade session bean that wraps multiple EJB
method invocations to avoid creating multiple transactions
−
Use Required on methods that update, create, delete or
modify databases
−
Use Supports or Not Supported on methods that perform get
or read-only operations.
• When the method is called, the container uses the attributes to do one of
the five things:
●
Run the method in the caller’s transaction. OR
●
Suspends the caller’s transaction and Start a new transaction. OR
●
Suspend the caller’s transaction and run the method without a
transaction. OR
●
Throw an exception because the caller does not have a transaction.
JavaException Handling
Collections Framework Grey Matter, Sella Synergy India 2min