Transaction Models
Transaction models describe how a transaction should behave and how they are implemented in the Java Platform. They only provide rules and semantics for transaction processing but how it is actually applied in an application is completely with the developer. The three transaction models supported by the Java platform are:
- The Local Transaction model
- The Programmatic Transaction model
- The Declarative Transaction model
The Local Transaction Model
In this model, transactions are managed by the underlying database resource manager, not the container or framework your application is running in. In this model you don’t manage transactions instead you manage the connections which you have got from the underlying resource. This model cannot be used when you interact with databases using an object-relational mapping framework such as Hibernate, TopLink, or the Java Persistence API (JPA). However, it can be applied when you use data-access object (DAO) or JDBC-based frameworks and database stored procedures.
You can use this model in two ways: allowing the database to manage the connection for you, or manage the connection programmatically in our application. To let the database manage the connection, you set the autoCommit property on the JDBC Connection object to true. By doing so it lets the database management system to commit the transaction soon after insert, update and delete has completed, or rollback if it fails. This is shown in the Listing 9-1 below.
Listing 9-1. Local transaction model having single update
public class JdbcCustomerRepository implements CustomerRepository {
[java]//….
public void createCustomer(Customer customer) throws Exception {
Connection connection = null;
try {
XmlBeanFactory beanFactory = new XmlBeanFactory(new
ClassPathResource("aplicationContext.xml"));
DataSource dataSource = (DataSource)
beanFactory.getBean("dataSource");
connection = dataSource.getConnection();
connection.setAutoCommit(true);
Statement statement = connection.createStatement();
String stmt = "insert into CUSTOMER …";
statement.executeUpdate(stmt);
} finally {
if (connection != null)
connection.close();
}
}
//….
}[/java]
Lising 9-2 shows local trasaction model having multiple update. In this you actually decide when you want to commit the transaction and accordingly you do it programmatically in your application.
Listing 9-2. Local transaction model having multiple update
[java]public class JdbcCustomerRepository implements CustomerRepository {
//….
public void createAndUpdateCustomer(Customer customer) throws Exception {
Connection connection = null;
try {
XmlBeanFactory beanFactory = new XmlBeanFactory(new
ClassPathResource("aplicationContext.xml"));
DataSource dataSource = (DataSource)
beanFactory.getBean("dataSource");
connection = ds.getConnection();
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
String stmtOne = "insert into CUSTOMER …";
statement.executeUpdate(stmtOne);
String stmtTwo = "update CUSTOMER set first_name…";
statement.executeUpdate(stmtTwo);
connection.commit();
} catch (Exception e) {
connection.rollback();
throw e;
} finally {
if (connection!= null)
connection.close();
}
}
//….
}[/java]
Page Visitors: 9445

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Thanks, nice chapter.