Tuesday, May 15, 2012

Transaction models




Local Transaction model
The Local Transaction model gets its name from the fact that transactions are managed by the underlying database resource manager, not the container or framework your application is running in. In this model, you manage connections rather than transactions.You can't use the Local Transaction model when you make database updates using an object-relational mapping framework such as Hibernate, TopLink, or the Java Persistence API (JPA). You can still apply it when using data-access object (DAO) or JDBC-based frameworks and database stored procedures.
You can use the Local Transaction model in one of two ways: let the database manage the connection, or manage the connection programmatically.
To let the database manage the connection, you set the autoCommit property on the JDBC Connection object totrue (the default value), which tells the underlying database management system (DBMS) to commit the transaction after the insert, update, or delete has completed, or roll back the work if it fails.
managing the connections programmatically. In this technique, you would set theautoCommit property on the Connection object to false and manually commit or roll back the connection.


Programmatic Transaction model

The Programmatic Transaction model, otherwise known as Bean Managed Transcations (BMT)
It gets its name from the fact that the developer is responsible for managing the transaction. In the Programmatic Transaction model, unlike the Local Transaction model, you manage transactions and are isolated from the underlying database connections.


In EJB
Programmer is responsible for calling the methods to start and end the transactions, EJB framework provides user transaction interface for this purpose.


Declarative Transaction model
The Declarative Transaction model, otherwise known as Container Managed Transactions (CMT), is the most common transaction model in the Java platform. In this model, the container environment takes care of starting, committing, and rolling back the transaction. The developer is responsible only for specifying the transactions' behavior. 


In EJB
The Transaction attributes are specified in the deployment descriptor. Responsibility of starting and ending the transaction remains of the container.

Both the Spring Framework and EJB 3.0 make use of annotations to specify the transaction behavior.
Spring uses the@Transactional annotation
EJB 3.0 uses the @TransactionAttribute annotation.

The container will not automatically roll back a transaction on a checked exception when you use the Declarative Transaction model. The developer must specify where and when to roll back a transaction when a checked exception occurs. In the Spring Framework, you specify this by using therollbackFor property on the @Transactional annotation. In EJB, you specify it by invoking the setRollbackOnly() method on the SessionContext.



No comments:

Post a Comment