Spring Book – Chapter 9 – Transaction Management

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: 9597

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

1 thought on “Spring Book – Chapter 9 – Transaction Management

Leave a Reply

Your email address will not be published. Required fields are marked *