SlideShare uma empresa Scribd logo
1 de 31
Spring Part - 4
Course Contents
•   Who This Tutor Is For
•   Introduction
•   DAO: Spring and You
•   Steps for DAO
      • Configure Data Source
           • Driver Based Data Source
           • JNDI Data source
           • Pooled Data source
      • Configure JDBC Template
      • Writing DAO Layer
      • Different Template classes and their uses
           • JdbcTemplate
           • NamedParameterJdbcTemplate
           • SimpleJdbcTemplate
      • DAO Support class for different Templates
Who This Tutor Is For?
This session will let you know how to manage transactions in Spring. You can understand
the programmatic as well as declarative transaction management in Spring Framework.
Before going through this session you must understand how Spring supports Database
connection. You can visit our Spring part -2 (Spring and Database) and Spring part -3
(Spring and Hibernate) to understand working with Spring and Database.



You can download the source codes of the examples given in this tutor from Download
Links available at http://springs-download.blogspot.com/




Good Reading…

Author,
Santosh
Introduction:
What does transaction means?
A transaction is a logical unit of work that contains one or more SQL statements. A
transaction is an atomic unit. The effects of all the SQL statements in a transaction can be
either all committed (applied to the database) or all rolled back (undone from the database).
To illustrate the concept of a transaction, consider a banking database. When a bank customer
transfers money from a savings account to a checking account, the transaction can consist of
three separate operations:

          Decrement the savings account
          Increment the checking account
          Record the transaction in the transaction journal

Your application must allow for two situations. If all three SQL statements can be performed
to maintain the accounts in proper balance, the effects of the transaction can be applied to the
database. However, if a problem such as insufficient funds, invalid account number, or a
hardware failure prevents one or two of the statements in the transaction from completing, the
entire transaction must be rolled back so that the balance of all accounts is correct.
In software, all-or-nothing operations are called transactions. Transactions allow you to
 group several operations into a single unit of work that either fully happens or fully doesn’t
 happen. If everything goes well then the transaction is a success. But if anything goes
 wrong, the slate is wiped clean and it’s as if nothing ever happened.


In Spring part -2 (Spring and Database), we examined Spring’s data access support and saw
several ways to read from and write data to the database. When writing to a database, we must
ensure that the integrity of the data is maintained by performing the updates within a
transaction. Spring has rich support for transaction management, both programmatic and
declarative.



                         Spring’s transaction management support

   Like EJB Spring supports both programmatic and declarative transaction
   management support. But Spring’s transaction support is different from EJB. EJB uses
   JTA (Java Transaction Support) implementation. But Spring uses transaction support
   offered by the persistent mechanism such as JDBC, Hibernate, JDO, third party JTA
   implementation for distributed (XA) transaction.
Springs transaction manager
Spring does not directly manage transactions. Instead, it comes with a selection of transaction
managers that delegate responsibility for transaction management to a platform-specific
transaction implementation provided by either JTA or the persistence mechanism. You need to
choose the correct transaction manager as per your design.
    Transaction manager (org.springframework.*)                            Use at
 jdbc.datasource.DataSourceTransactionManager               Spring’s JDBC
 jms.connection.JmsTransactionManager                       JMS 1.1+.
 jms.connection.JmsTransactionManager102                    JMS 1.0.2.
 orm.hibernate.HibernateTransactionManager                  Hibernate 2
 orm.hibernate3.HibernateTransactionManager                 Hibernate 3
 orm.jpa.JpaTransactionManager                              JPA
 transaction.jta.WebLogicJtaTransactionManager              distributed transactions in WebLogic.
 transaction.jta.WebSphereTransactionManagerFactoryBean distributed transactions in WebSphere


Each of the transaction managers acts as a façade to a platform-specific transaction
implementation. This makes it possible for you to work with a transaction in Spring with
little regard to what the actual transaction implementation is. To use a transaction manager,
you’ll need to declare it in your application context.
Now let’s see the example.
We have two tables EMP & DEPT. The structure of the two tables would look like:




 Scenario:
 In a organization, there are Employees and each employee are associated with a Department.
 We need to add one Department and a collection of employees in a single transaction. The
 condition is,
 • if the department does not already exist in the DB as well as none of the employee from the
     collection even exists in DB, then insert both Department and all the employees in DB.

 •   if department does already exist, then neither department or any of the employees should be
     inserted into DB and so the transaction should be rolled back.

 •   if department does not exist but any of the employee already exists in DB, then the whole
     transaction must be rolled back and none of them should be inserted.
Spring Programmatic Transaction
With programmatic transaction management developers work with the Spring transaction
abstraction, which can run over any underlying transaction infrastructure. Programmatic
transactions are good when you want complete control over transactional boundaries.

When to use Programmatic Transaction Management?
Programmatic transaction management is usually a good idea only if you have a small number
of transactional operations. For example, if you have a web application that require
transactions only for certain update operations, you may not want to set up transactional
proxies using Spring or any other technology. Using programmatic transaction may be a good
approach.

2 ways you can use programmatic transaction in Springs

Spring provides two means of programmatic transaction management:
• Using the TransactionTemplate
• Using a PlatformTransactionManager implementation directly

I would recommend the first approach (using TransactionTemplate)

Now let’s see the example…
As we had seen the transaction scenario as:

•     if department does not exist and none of the employee already exist in DB, then add both
      Department and all the employees.
•     if department does already exist, then neither department or any of the employees should be
      inserted into DB and so the transaction should be rolled back.
•     if department does not exist but any of the employee already exists in DB, then the transaction
      must be rolled back and none of them should be inserted.

The client program would look like :




    Now all opearation happends inside the method:
    addDepartmentAndEmployee(Department, List<Employee>).

    Let’s consider the flow chart to understand when the transaction should be rolled back…
addDepartmentAndEmployee



                                Is
Throw exception   Yes
                           Dept. name
                           exists in DB
                                             In a single Transaction
                                             i.e. All or None
                                 No



                                Is
Throw exception   Yes
                           emp name
                           exists in DB



  Rollback the
  transaction
                         Commit the
                         transaction




                              end
Using programmatic transaction…
One approach to adding transactions is to programmatically add transactional boundaries
directly within the addDepartmentAndEmployee() method using Spring’s TransactionTemplate.
Like other template classes in Spring such as JdbcTemplate, TransactionTemplate utilizes a
callback mechanism.

Let’s follow the below steps:

In Spring config
1. Select appropriate TransactionManager for your application to inject into
    TransactionTemplate.
2. Select appropriate TransactionTemplate and inject into DAO class.

In DAO class:
3. Define the property for TransactionTemplate.
4. Call TransactionTemplate.execute() method,
    • implementing the TransactionCallback interface,
    • implement the method doTransaction() declared in TransactionCallback interface.

Now let’s see these 4 steps one by one in detail.
1: Selecting appropriate TransactionManager for your application.

Already we have seen there are difference transaction managers for different types of
application/framework     such      as       DataSourceTransactionManager for   simple
JDBC, HibernateTransactionManager for hibernate etc.

I am using DataSourceTransactionManager as I am using simple datasource to use JdbcTemplate. The
TransactionManager needs a datasource to be declared. (We already have seen how to work with different
spring data sources in previous parts).




2: Selecting appropriate TransactionTemplate and injecting into DAO

Like there is jdbcTemplate to use the SQL statements, we need to use TransactionTemplate to
use the transactions. The transactionTemplate needs a TransactionManager (which we
declared in step 1) which is used in the DAO to maintain the transaction.
3. Define the property for TransactionTemplate.


                                                  Concentrate on this class type.




4: Call TransactionTemplate.execute() method




                    Your transaction
Now in our example if you will see, the transaction would look like:


                                                                                         Multiple DB
                                                                                         operations in a
                                                                                         single transaction




If you see the above example, there are multiple DB operations are happening in a single transaction. If there
is any issue with one operation, then all the operations will be rolled back.

The addDepartment() checks if the department name already exists. If so it throws an exception else adds the
department.
Similarly there is the department existence check in the addDepartment() method.




How the transaction behaves in our example…
If there is no issue in the operation for adding the department, it goes to addEmployee().

The addEmployees() method checks if the Employee name already exists in database or not.
If name found for any employee in DB, it throws the exception and as result it rolls back all
the operations including the operation of adding the new department.

Here, although there was no issue in adding the department but issue found in adding
employee, still the operation of adding the department also rolled back along with the
operation of adding employee. The reason is both the operations are executed under a single
transaction, So if any exception occurs with any of the process inside a transaction, all the
operations inside that transaction needs to be rolled back.
Programmatic Transaction in Hibernate
It is not much difference to use programmatic transaction in Hibernate. You just need to use is the transaction
manager class: org.springframework.orm.hibernate3.HibernateTransactionManager. You should inject the session
factory used in hibernate into the transaction manager in place of data source. So your Spring configuration
would look like:
Output


Transaction committed…
No issue with any operation:




Transaction Rolled back…
No issue with Department but employee “Kishan” is still there in DB while executing the client.:
Spring Declarative Transaction
While programmatic transaction management affords you flexibility in precisely defining
transaction boundaries in your code, declarative transactions help you decouple an operation
from its transaction rules.
Spring’s support for declarative transaction management is implemented through Spring’s AOP
framework

When to use Declarative Transaction Management?
If your applications has numerous transactional operations, declarative transaction management
is usually worthwhile. It keeps transaction management out of business logic, and is not
difficult to configure in Spring. Using Spring, rather than EJB CMT, the configuration cost of
declarative transaction management is greatly reduced.

Spring provides three ways to declare transactional boundaries in the Spring configuration
1. By proxying transaction using Spring AOP
2. Simple XML-declared transactions
3. Annotation-driven transactions.
By proxying transaction using Spring AOP
In pre-2.0 versions of Spring, declarative transaction management was accomplished by proxying
your POJOs with Spring’s TransactionProxyFactoryBean. TransactionProxyFactoryBean is a
specialization of ProxyFactoryBean that knows how to proxy a POJO’s methods by wrapping
them with transactional boundaries.

When you are going to configure declarative transaction for proxy transaction, you have to
remember and follow the below steps:

Proxy your DAO using the class TransactionProxyFactoryBean
    e.g. instead
    <bean id="organizationDao" class="org.santosh.dao.OrganizationDao">
    it should be
    <bean id="organizationDao" class=" org.springframework.transaction.interceptor.TransactionProxyFactoryBean">


Define 5 bean properties for TransactionProxyFactoryBean:
    • target
    • proxyInterface
    • transactionManager
    • transactionAttributes
We will use the same example what we discussed in programmatic transaction in this part. So we
have:

• the interface: OrganizationDao.java. This has the method: addDepartmentAndEmployee() where
  we need to maintain the transaction.
• the class OrganizationDaoImpl.java which implemented the above interface.
  (This interface is used to make the proxy)

First we will define the bean using the class TransactionProxyFactoryBean . This bean will be
used in client to call the method that should run within the transaction. Now let’s see the
configuration.




  The client code would look like:
       OrganizationDao daoService= (OrganizationDao) ctx.getBean("organizationDao");
       daoService.<method that must run within a transaction>;
1.   Property - target
<property name="target" ref="organizationDaoTarget">

     Target in a Transaction is nothing but this refers the actual class which contains the
     implementation of the method where more than one operation run within a transaction. So if
     any of the operation could not be performed successfully then, all the operations will be
     rolled back. If there is no issue with any operations defined in the method, the transaction
     will be committed.

     Here ref="organizationDaoTarget" is the reference of a bean which refers the implementation
     class. So you have to define the bean with database template*.
     (*If you want to know more about the database templates, please visit our presentation:   Spring Part – 3
     (Spring and Database).) So make an entry for the target bean as:




2.   Property - proxyInterface
<property name=“proxyInterface" value="org.santosh.dao.OrganizationDao">
      This property is related to the property – Target.
      proxyInterface refers to the interface which is implemented by the target class. This interface
      is used to proxy the bean. So as you can see the value, the interface
      org.santosh.dao.OrganizationDao contains the declaration of the method which must run in a
      transaction and this is implemented by the target class.
3.   Property - transactionManager
<property name="transactionManager" ref="transactionManager"></property>
As we already discussed that Spring comes with a selection of transaction managers that delegate
responsibility for transaction management to a platform-specific transaction implementation
provided by either JTA or the persistence mechanism. To know more, go back to know the
different transaction managers that can be used with Spring. We use DataSourceTransactionManager.




3.   Property - transactionAttributes
<property name="transactionAttributes">
     <props>
            <prop key="add*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_SUPPORTS</prop>
     </props>
</property>

Transaction attributes are the most important part in Spring Transactions. In Spring declarative
transactions are defined with transaction attributes.

A transaction attribute is a description of how transaction policies should be applied to a method.
Regardless of which declarative transaction mechanism you use, you’ll
      have the opportunity to define these attributes.
Let’s examine each attribute to understand how it shapes a transaction.


Propagation behavior
Propagation behavior defines the boundaries of the transaction with respect to the client and to
the method being called.
Spring defines seven distinct propagation behaviors. These are the constants which you can find
in Interface TransactionDefinition. These fields are:

static int PROPOGATION_MANDATORY
static int PROPOGATION_REQUIRED
static int PROPOGATION_REQUIRES_NEW
static int PROPOGATION_SUPPORTS
static int PROPOGATION_NOT_SUPPORTED
static int PROPOGATION_NESTED
static int PROPOGATION_NEVER
PROPOGATION_MANDATORY: Support a current transaction; throw an exception if no
current transaction exists.

PROPOGATION_REQUIRED: Support a current transaction; create a new one if none
exists.

PROPOGATION_REQUIRES_NEW: Indicates that the current method must run within its
own transaction. A new transaction is started and if an existing transaction is in progress, it will
be suspended for the duration of the method. If using JTATransactionManager, access to
TransactionManager is required.

PROPOGATION_SUPPORTS: Indicates that the current method does not require a
Transactional context, but may run within a transaction if one is already in progress.

PROPOGATION_NOT_SUPPORTED: Indicates that the method should not run within a
transaction. If an existing transaction is in progress, it will be suspended for the duration of the
method. If using JTATransactionManager, access to TransactionManager is required.

PROPOGATION_NESTED: Execute within a nested transaction if a current transaction
exists, behave like PROPOGATION_REQUIRED else.

PROPOGATION_NEVER: Do not support a current transaction; throw an exception if a
current transaction exists
Isolation levels
An isolation level defines how much a transaction may be impacted by the activities of other
concurrent transactions. In a typical application, multiple transactions run concurrently, often
working with the same data to get their job done. One example, in one transaction,

For example, in the EMP table, the query is run to updated salary of Jack from 4000 to 5000.
     UPDATE emp SET sal=5000 WHERE name='Jack';
But here the it is waiting to commit/rollback the transaction.

Now in another transaction, it run the query to read the salary of Jack as:
     SELECT sal FROM emp WHERE name='Jack';



Now the question is, how much it should print?
         the committed value          4000
OR
         the uncommitted value        5000

So there we can set the Isolation level. E.g. when we set the Isolation level to
TRANSACTION_READ_UNCOMMITTED, the result should be: the uncommitted value –
5000
When we set the Isolation level to TRANSACTION_READ_COMMITTED, the result should
be: the committed value – 4000.
If you new to Isolation level, it would look very interesting.
So apart from read committed/read uncommitted, we do have other types of Isolation types. But
before we see different isolation levels we can set, we need to know why do we require to set the
Isolation levels?

Now let’s see the problems in a typical application where we run multiple transactions
concurrently. The problems are:
    • Dirty Read
    • Non-repeatable Read
    • Phantom Read

In table the record is:
   empid     name   Sal
   1         Jack   4000
   2         John   4700

There are two transaction Tx1 and Tx2

• Dirty Reads
Tx1 updated one record but not yet committed.
       UPDATE emp SET sal=5000 WHERE name='Jack';
Same time, Tx2 read that updated data (which is not yet committed by Tx1).
       SELECT sal FROM emp WHERE name='Jack'
Result : 5000
Now Tx1 has rollback.
Problem: data obtained by Tx2 is now invalid i.e. it’s 5000 where in DB after rolledback, it
remained 4000.
• Non-repeatable Reads
Tx2 read the data from the table in 3 different times.

     First time:
           SELECT sal FROM emp WHERE name='Jack‘
     Result: 4000

     Second time:
           SELECT sal FROM emp WHERE name='Jack‘
     Result: 5000

     Third time:
           SELECT sal FROM emp WHERE name='Jack‘
     Result: 6000

Problem: data obtained by Tx2 is different each time it executed.
The reason could be: other transactions such as Tx1 might have updated the data
multiple times in different time period and concurrently Tx2 reads those data.
• Phantom Reads
Tx2 reads all records from a table:
     SELECT name FROM emp WHERE sal >=4000 ;
Result :
           Jack
           John


     Tx1 inserted a records into the table:
     INSERT INTO emp VALUES (3, 'MIKE', 6300);



Tx2 again reads all records from a table:
     SELECT name FROM emp WHERE sal >=4000 ;

Result :
           Jack
           John
           Mike

Problem: Data obtained by Tx2 multiple times but it found additional record i.e. employee
Mike is found and this is inserted by the concurrent transaction Tx1.
To overcome these problems, we need to set the Isolation Level.
Similar to Propogation, Spring defines 5 distinct isolation levels that can be set along with
Propogation. These are the constants which you can find in Interface TransactionDefinition.

These fields are:

static int ISOLATION_DEFAULT
static int ISOLATION_READ_COMMITTED
static int ISOLATION_READ_UNCOMMITTED
static int ISOLATION_REPEATABLE_READ
static int ISOLATION_SERIALIZABLE

 Isolation Level                    Dirty Reads   Non-Repeatable Reads   Phantom Reads

 ISOLATION_READ_COMMITTED           Prevented     Allowed                Allowed

 ISOLATION_READ_UNCOMMITTED         Allowed       Allowed                Allowed

 ISOLATION_REPEATABLE_READ          Prevented     Prevented              Allowed

 ISOLATION_SERIALIZABLE             Prevented     Prevented              Prevented


ISOLATION_READ_UNCOMMITTED is the most efficient isolation level, but isolates the
transaction the least, leaving the transaction open to dirty, non-repeatable, and phantom reads. At
the other extreme, ISOLATION_SERIALIZABLE prevents all forms of isolation problems but is
the least efficient.
So as the transaction attribute, we have choosed:
          propogation as: PROPOGATION_REQUIRED
          Isolation as: ISOLATION_SERIALIZABLE
Do you have Questions ?
Please write to:
santosh.bsil@yahoo.co.in

Mais conteúdo relacionado

Mais procurados

An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockitoshaunthomas999
 
Type casting in java
Type casting in javaType casting in java
Type casting in javaFarooq Baloch
 
Notes of java first unit
Notes of java first unitNotes of java first unit
Notes of java first unitgowher172236
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In JavaSpotle.ai
 
Serialization & De-serialization in Java
Serialization & De-serialization in JavaSerialization & De-serialization in Java
Serialization & De-serialization in JavaInnovationM
 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51myrajendra
 
Python Advanced – Building on the foundation
Python Advanced – Building on the foundationPython Advanced – Building on the foundation
Python Advanced – Building on the foundationKevlin Henney
 
Decision Structures and Boolean Logic
Decision Structures and Boolean LogicDecision Structures and Boolean Logic
Decision Structures and Boolean LogicMunazza-Mah-Jabeen
 
Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in JavaOblivionWalker
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programmingRiccardo Cardin
 
Java Serialization
Java SerializationJava Serialization
Java Serializationimypraz
 
Data base connectivity and flex grid in vb
Data base connectivity and flex grid in vbData base connectivity and flex grid in vb
Data base connectivity and flex grid in vbAmandeep Kaur
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Edureka!
 

Mais procurados (20)

An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
Notes of java first unit
Notes of java first unitNotes of java first unit
Notes of java first unit
 
Java Streams
Java StreamsJava Streams
Java Streams
 
File handling
File handlingFile handling
File handling
 
Io streams
Io streamsIo streams
Io streams
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
Serialization & De-serialization in Java
Serialization & De-serialization in JavaSerialization & De-serialization in Java
Serialization & De-serialization in Java
 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
 
Python Advanced – Building on the foundation
Python Advanced – Building on the foundationPython Advanced – Building on the foundation
Python Advanced – Building on the foundation
 
Decision Structures and Boolean Logic
Decision Structures and Boolean LogicDecision Structures and Boolean Logic
Decision Structures and Boolean Logic
 
Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in Java
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programming
 
Java Serialization
Java SerializationJava Serialization
Java Serialization
 
Data base connectivity and flex grid in vb
Data base connectivity and flex grid in vbData base connectivity and flex grid in vb
Data base connectivity and flex grid in vb
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
 

Destaque

Writing simple web services in java using eclipse editor
Writing simple web services in java using eclipse editorWriting simple web services in java using eclipse editor
Writing simple web services in java using eclipse editorSantosh Kumar Kar
 
Spring Transaction Management
Spring Transaction ManagementSpring Transaction Management
Spring Transaction ManagementYe Win
 
Effective Spring Transaction Management
Effective Spring Transaction ManagementEffective Spring Transaction Management
Effective Spring Transaction ManagementUMA MAHESWARI
 
Deploy and Publish Web Service
Deploy and Publish Web ServiceDeploy and Publish Web Service
Deploy and Publish Web Servicepradeepfdo
 
Spring Transaction
Spring TransactionSpring Transaction
Spring Transactionpatinijava
 
Spring Framework - Data Access
Spring Framework - Data AccessSpring Framework - Data Access
Spring Framework - Data AccessDzmitry Naskou
 
Spring Data JPA - Repositories done right
Spring Data JPA - Repositories done rightSpring Data JPA - Repositories done right
Spring Data JPA - Repositories done rightOliver Gierke
 
Java Web Services [4/5]: Java API for XML Web Services
Java Web Services [4/5]: Java API for XML Web ServicesJava Web Services [4/5]: Java API for XML Web Services
Java Web Services [4/5]: Java API for XML Web ServicesIMC Institute
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesBrett Meyer
 
Spring @Transactional Explained
Spring @Transactional ExplainedSpring @Transactional Explained
Spring @Transactional ExplainedSmita Prasad
 

Destaque (20)

Spring & hibernate
Spring & hibernateSpring & hibernate
Spring & hibernate
 
Writing simple web services in java using eclipse editor
Writing simple web services in java using eclipse editorWriting simple web services in java using eclipse editor
Writing simple web services in java using eclipse editor
 
Spring database - part2
Spring database -  part2Spring database -  part2
Spring database - part2
 
Springs
SpringsSprings
Springs
 
Spring Transaction Management
Spring Transaction ManagementSpring Transaction Management
Spring Transaction Management
 
Effective Spring Transaction Management
Effective Spring Transaction ManagementEffective Spring Transaction Management
Effective Spring Transaction Management
 
Hibernate教程
Hibernate教程Hibernate教程
Hibernate教程
 
Silverlight
SilverlightSilverlight
Silverlight
 
Deploy and Publish Web Service
Deploy and Publish Web ServiceDeploy and Publish Web Service
Deploy and Publish Web Service
 
Thinking Beyond ORM in JPA
Thinking Beyond ORM in JPAThinking Beyond ORM in JPA
Thinking Beyond ORM in JPA
 
Spring Transaction
Spring TransactionSpring Transaction
Spring Transaction
 
Spring Framework - Data Access
Spring Framework - Data AccessSpring Framework - Data Access
Spring Framework - Data Access
 
Spring Data JPA - Repositories done right
Spring Data JPA - Repositories done rightSpring Data JPA - Repositories done right
Spring Data JPA - Repositories done right
 
Spring Security 3
Spring Security 3Spring Security 3
Spring Security 3
 
Maven (EN ESPANOL)
Maven (EN ESPANOL)Maven (EN ESPANOL)
Maven (EN ESPANOL)
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Maven Overview
Maven OverviewMaven Overview
Maven Overview
 
Java Web Services [4/5]: Java API for XML Web Services
Java Web Services [4/5]: Java API for XML Web ServicesJava Web Services [4/5]: Java API for XML Web Services
Java Web Services [4/5]: Java API for XML Web Services
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance Techniques
 
Spring @Transactional Explained
Spring @Transactional ExplainedSpring @Transactional Explained
Spring @Transactional Explained
 

Semelhante a Spring transaction part4

Transaction and concurrency pitfalls in Java
Transaction and concurrency pitfalls in JavaTransaction and concurrency pitfalls in Java
Transaction and concurrency pitfalls in JavaErsen Öztoprak
 
Spring transaction management
Spring transaction managementSpring transaction management
Spring transaction managementHarshit Choudhary
 
Distributed Transaction Management in Spring & JEE
Distributed Transaction Management in Spring & JEEDistributed Transaction Management in Spring & JEE
Distributed Transaction Management in Spring & JEEMushfekur Rahman
 
Session 9 Tp9
Session 9 Tp9Session 9 Tp9
Session 9 Tp9phanleson
 
Overview of atg framework
Overview of atg frameworkOverview of atg framework
Overview of atg frameworkYousuf Roushan
 
Domain logic patterns of Software Architecture
Domain logic patterns of Software ArchitectureDomain logic patterns of Software Architecture
Domain logic patterns of Software ArchitectureShweta Ghate
 
Ruslan Platonov - Transactions
Ruslan Platonov - TransactionsRuslan Platonov - Transactions
Ruslan Platonov - TransactionsDmitry Buzdin
 
MongoDB WiredTiger Internals: Journey To Transactions
  MongoDB WiredTiger Internals: Journey To Transactions  MongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsM Malai
 
MongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsMongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsMydbops
 
Project description2012
Project description2012Project description2012
Project description2012ashish61_scs
 
RDB - Repairable Database Systems
RDB - Repairable Database SystemsRDB - Repairable Database Systems
RDB - Repairable Database SystemsAlexey Smirnov
 
Java Distributed Transactions
Java Distributed TransactionsJava Distributed Transactions
Java Distributed TransactionsAnjana Fernando
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to ReduxAmin Ashtiani
 
Developing Transactional JEE Apps With Spring
Developing Transactional JEE Apps With SpringDeveloping Transactional JEE Apps With Spring
Developing Transactional JEE Apps With SpringGuy Pardon
 
Effective Test Driven Database Development
Effective Test Driven Database DevelopmentEffective Test Driven Database Development
Effective Test Driven Database Developmentelliando dias
 
Java/Scala Lab 2016. Дмитрий Соколов: Принципы работы с транзакциями при помо...
Java/Scala Lab 2016. Дмитрий Соколов: Принципы работы с транзакциями при помо...Java/Scala Lab 2016. Дмитрий Соколов: Принципы работы с транзакциями при помо...
Java/Scala Lab 2016. Дмитрий Соколов: Принципы работы с транзакциями при помо...GeeksLab Odessa
 

Semelhante a Spring transaction part4 (20)

Transaction and concurrency pitfalls in Java
Transaction and concurrency pitfalls in JavaTransaction and concurrency pitfalls in Java
Transaction and concurrency pitfalls in Java
 
Spring transaction management
Spring transaction managementSpring transaction management
Spring transaction management
 
Distributed Transaction Management in Spring & JEE
Distributed Transaction Management in Spring & JEEDistributed Transaction Management in Spring & JEE
Distributed Transaction Management in Spring & JEE
 
Transaction
TransactionTransaction
Transaction
 
Session 9 Tp9
Session 9 Tp9Session 9 Tp9
Session 9 Tp9
 
Overview of atg framework
Overview of atg frameworkOverview of atg framework
Overview of atg framework
 
Autonomous transaction
Autonomous transactionAutonomous transaction
Autonomous transaction
 
Domain logic patterns of Software Architecture
Domain logic patterns of Software ArchitectureDomain logic patterns of Software Architecture
Domain logic patterns of Software Architecture
 
Ruslan Platonov - Transactions
Ruslan Platonov - TransactionsRuslan Platonov - Transactions
Ruslan Platonov - Transactions
 
MongoDB WiredTiger Internals: Journey To Transactions
  MongoDB WiredTiger Internals: Journey To Transactions  MongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To Transactions
 
MongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsMongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To Transactions
 
Project description2012
Project description2012Project description2012
Project description2012
 
RDB - Repairable Database Systems
RDB - Repairable Database SystemsRDB - Repairable Database Systems
RDB - Repairable Database Systems
 
Java Distributed Transactions
Java Distributed TransactionsJava Distributed Transactions
Java Distributed Transactions
 
Module04
Module04Module04
Module04
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to Redux
 
Developing Transactional JEE Apps With Spring
Developing Transactional JEE Apps With SpringDeveloping Transactional JEE Apps With Spring
Developing Transactional JEE Apps With Spring
 
Effective Test Driven Database Development
Effective Test Driven Database DevelopmentEffective Test Driven Database Development
Effective Test Driven Database Development
 
Java/Scala Lab 2016. Дмитрий Соколов: Принципы работы с транзакциями при помо...
Java/Scala Lab 2016. Дмитрий Соколов: Принципы работы с транзакциями при помо...Java/Scala Lab 2016. Дмитрий Соколов: Принципы работы с транзакциями при помо...
Java/Scala Lab 2016. Дмитрий Соколов: Принципы работы с транзакциями при помо...
 
About work flow
About work flowAbout work flow
About work flow
 

Mais de Santosh Kumar Kar

Operating electrical devices with PIR sensor. No coding, No controller
Operating electrical devices with PIR sensor. No coding, No controllerOperating electrical devices with PIR sensor. No coding, No controller
Operating electrical devices with PIR sensor. No coding, No controllerSantosh Kumar Kar
 
Temperature sensor with raspberry pi
Temperature sensor with raspberry piTemperature sensor with raspberry pi
Temperature sensor with raspberry piSantosh Kumar Kar
 
Pir motion sensor with raspberry pi
Pir motion sensor with raspberry piPir motion sensor with raspberry pi
Pir motion sensor with raspberry piSantosh Kumar Kar
 

Mais de Santosh Kumar Kar (7)

Smart home arduino
Smart home   arduinoSmart home   arduino
Smart home arduino
 
Operating electrical devices with PIR sensor. No coding, No controller
Operating electrical devices with PIR sensor. No coding, No controllerOperating electrical devices with PIR sensor. No coding, No controller
Operating electrical devices with PIR sensor. No coding, No controller
 
Temperature sensor with raspberry pi
Temperature sensor with raspberry piTemperature sensor with raspberry pi
Temperature sensor with raspberry pi
 
Pir motion sensor with raspberry pi
Pir motion sensor with raspberry piPir motion sensor with raspberry pi
Pir motion sensor with raspberry pi
 
Angular js for Beginnners
Angular js for BeginnnersAngular js for Beginnners
Angular js for Beginnners
 
Raspberry pi complete setup
Raspberry pi complete setupRaspberry pi complete setup
Raspberry pi complete setup
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 

Último

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Último (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

Spring transaction part4

  • 2. Course Contents • Who This Tutor Is For • Introduction • DAO: Spring and You • Steps for DAO • Configure Data Source • Driver Based Data Source • JNDI Data source • Pooled Data source • Configure JDBC Template • Writing DAO Layer • Different Template classes and their uses • JdbcTemplate • NamedParameterJdbcTemplate • SimpleJdbcTemplate • DAO Support class for different Templates
  • 3. Who This Tutor Is For? This session will let you know how to manage transactions in Spring. You can understand the programmatic as well as declarative transaction management in Spring Framework. Before going through this session you must understand how Spring supports Database connection. You can visit our Spring part -2 (Spring and Database) and Spring part -3 (Spring and Hibernate) to understand working with Spring and Database. You can download the source codes of the examples given in this tutor from Download Links available at http://springs-download.blogspot.com/ Good Reading… Author, Santosh
  • 4. Introduction: What does transaction means? A transaction is a logical unit of work that contains one or more SQL statements. A transaction is an atomic unit. The effects of all the SQL statements in a transaction can be either all committed (applied to the database) or all rolled back (undone from the database). To illustrate the concept of a transaction, consider a banking database. When a bank customer transfers money from a savings account to a checking account, the transaction can consist of three separate operations: Decrement the savings account Increment the checking account Record the transaction in the transaction journal Your application must allow for two situations. If all three SQL statements can be performed to maintain the accounts in proper balance, the effects of the transaction can be applied to the database. However, if a problem such as insufficient funds, invalid account number, or a hardware failure prevents one or two of the statements in the transaction from completing, the entire transaction must be rolled back so that the balance of all accounts is correct.
  • 5. In software, all-or-nothing operations are called transactions. Transactions allow you to group several operations into a single unit of work that either fully happens or fully doesn’t happen. If everything goes well then the transaction is a success. But if anything goes wrong, the slate is wiped clean and it’s as if nothing ever happened. In Spring part -2 (Spring and Database), we examined Spring’s data access support and saw several ways to read from and write data to the database. When writing to a database, we must ensure that the integrity of the data is maintained by performing the updates within a transaction. Spring has rich support for transaction management, both programmatic and declarative. Spring’s transaction management support Like EJB Spring supports both programmatic and declarative transaction management support. But Spring’s transaction support is different from EJB. EJB uses JTA (Java Transaction Support) implementation. But Spring uses transaction support offered by the persistent mechanism such as JDBC, Hibernate, JDO, third party JTA implementation for distributed (XA) transaction.
  • 6. Springs transaction manager Spring does not directly manage transactions. Instead, it comes with a selection of transaction managers that delegate responsibility for transaction management to a platform-specific transaction implementation provided by either JTA or the persistence mechanism. You need to choose the correct transaction manager as per your design. Transaction manager (org.springframework.*) Use at jdbc.datasource.DataSourceTransactionManager Spring’s JDBC jms.connection.JmsTransactionManager JMS 1.1+. jms.connection.JmsTransactionManager102 JMS 1.0.2. orm.hibernate.HibernateTransactionManager Hibernate 2 orm.hibernate3.HibernateTransactionManager Hibernate 3 orm.jpa.JpaTransactionManager JPA transaction.jta.WebLogicJtaTransactionManager distributed transactions in WebLogic. transaction.jta.WebSphereTransactionManagerFactoryBean distributed transactions in WebSphere Each of the transaction managers acts as a façade to a platform-specific transaction implementation. This makes it possible for you to work with a transaction in Spring with little regard to what the actual transaction implementation is. To use a transaction manager, you’ll need to declare it in your application context.
  • 7. Now let’s see the example. We have two tables EMP & DEPT. The structure of the two tables would look like: Scenario: In a organization, there are Employees and each employee are associated with a Department. We need to add one Department and a collection of employees in a single transaction. The condition is, • if the department does not already exist in the DB as well as none of the employee from the collection even exists in DB, then insert both Department and all the employees in DB. • if department does already exist, then neither department or any of the employees should be inserted into DB and so the transaction should be rolled back. • if department does not exist but any of the employee already exists in DB, then the whole transaction must be rolled back and none of them should be inserted.
  • 8. Spring Programmatic Transaction With programmatic transaction management developers work with the Spring transaction abstraction, which can run over any underlying transaction infrastructure. Programmatic transactions are good when you want complete control over transactional boundaries. When to use Programmatic Transaction Management? Programmatic transaction management is usually a good idea only if you have a small number of transactional operations. For example, if you have a web application that require transactions only for certain update operations, you may not want to set up transactional proxies using Spring or any other technology. Using programmatic transaction may be a good approach. 2 ways you can use programmatic transaction in Springs Spring provides two means of programmatic transaction management: • Using the TransactionTemplate • Using a PlatformTransactionManager implementation directly I would recommend the first approach (using TransactionTemplate) Now let’s see the example…
  • 9. As we had seen the transaction scenario as: • if department does not exist and none of the employee already exist in DB, then add both Department and all the employees. • if department does already exist, then neither department or any of the employees should be inserted into DB and so the transaction should be rolled back. • if department does not exist but any of the employee already exists in DB, then the transaction must be rolled back and none of them should be inserted. The client program would look like : Now all opearation happends inside the method: addDepartmentAndEmployee(Department, List<Employee>). Let’s consider the flow chart to understand when the transaction should be rolled back…
  • 10. addDepartmentAndEmployee Is Throw exception Yes Dept. name exists in DB In a single Transaction i.e. All or None No Is Throw exception Yes emp name exists in DB Rollback the transaction Commit the transaction end
  • 11. Using programmatic transaction… One approach to adding transactions is to programmatically add transactional boundaries directly within the addDepartmentAndEmployee() method using Spring’s TransactionTemplate. Like other template classes in Spring such as JdbcTemplate, TransactionTemplate utilizes a callback mechanism. Let’s follow the below steps: In Spring config 1. Select appropriate TransactionManager for your application to inject into TransactionTemplate. 2. Select appropriate TransactionTemplate and inject into DAO class. In DAO class: 3. Define the property for TransactionTemplate. 4. Call TransactionTemplate.execute() method, • implementing the TransactionCallback interface, • implement the method doTransaction() declared in TransactionCallback interface. Now let’s see these 4 steps one by one in detail.
  • 12. 1: Selecting appropriate TransactionManager for your application. Already we have seen there are difference transaction managers for different types of application/framework such as DataSourceTransactionManager for simple JDBC, HibernateTransactionManager for hibernate etc. I am using DataSourceTransactionManager as I am using simple datasource to use JdbcTemplate. The TransactionManager needs a datasource to be declared. (We already have seen how to work with different spring data sources in previous parts). 2: Selecting appropriate TransactionTemplate and injecting into DAO Like there is jdbcTemplate to use the SQL statements, we need to use TransactionTemplate to use the transactions. The transactionTemplate needs a TransactionManager (which we declared in step 1) which is used in the DAO to maintain the transaction.
  • 13. 3. Define the property for TransactionTemplate. Concentrate on this class type. 4: Call TransactionTemplate.execute() method Your transaction
  • 14. Now in our example if you will see, the transaction would look like: Multiple DB operations in a single transaction If you see the above example, there are multiple DB operations are happening in a single transaction. If there is any issue with one operation, then all the operations will be rolled back. The addDepartment() checks if the department name already exists. If so it throws an exception else adds the department.
  • 15. Similarly there is the department existence check in the addDepartment() method. How the transaction behaves in our example… If there is no issue in the operation for adding the department, it goes to addEmployee(). The addEmployees() method checks if the Employee name already exists in database or not. If name found for any employee in DB, it throws the exception and as result it rolls back all the operations including the operation of adding the new department. Here, although there was no issue in adding the department but issue found in adding employee, still the operation of adding the department also rolled back along with the operation of adding employee. The reason is both the operations are executed under a single transaction, So if any exception occurs with any of the process inside a transaction, all the operations inside that transaction needs to be rolled back.
  • 16. Programmatic Transaction in Hibernate It is not much difference to use programmatic transaction in Hibernate. You just need to use is the transaction manager class: org.springframework.orm.hibernate3.HibernateTransactionManager. You should inject the session factory used in hibernate into the transaction manager in place of data source. So your Spring configuration would look like:
  • 17. Output Transaction committed… No issue with any operation: Transaction Rolled back… No issue with Department but employee “Kishan” is still there in DB while executing the client.:
  • 18. Spring Declarative Transaction While programmatic transaction management affords you flexibility in precisely defining transaction boundaries in your code, declarative transactions help you decouple an operation from its transaction rules. Spring’s support for declarative transaction management is implemented through Spring’s AOP framework When to use Declarative Transaction Management? If your applications has numerous transactional operations, declarative transaction management is usually worthwhile. It keeps transaction management out of business logic, and is not difficult to configure in Spring. Using Spring, rather than EJB CMT, the configuration cost of declarative transaction management is greatly reduced. Spring provides three ways to declare transactional boundaries in the Spring configuration 1. By proxying transaction using Spring AOP 2. Simple XML-declared transactions 3. Annotation-driven transactions.
  • 19. By proxying transaction using Spring AOP In pre-2.0 versions of Spring, declarative transaction management was accomplished by proxying your POJOs with Spring’s TransactionProxyFactoryBean. TransactionProxyFactoryBean is a specialization of ProxyFactoryBean that knows how to proxy a POJO’s methods by wrapping them with transactional boundaries. When you are going to configure declarative transaction for proxy transaction, you have to remember and follow the below steps: Proxy your DAO using the class TransactionProxyFactoryBean e.g. instead <bean id="organizationDao" class="org.santosh.dao.OrganizationDao"> it should be <bean id="organizationDao" class=" org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> Define 5 bean properties for TransactionProxyFactoryBean: • target • proxyInterface • transactionManager • transactionAttributes
  • 20. We will use the same example what we discussed in programmatic transaction in this part. So we have: • the interface: OrganizationDao.java. This has the method: addDepartmentAndEmployee() where we need to maintain the transaction. • the class OrganizationDaoImpl.java which implemented the above interface. (This interface is used to make the proxy) First we will define the bean using the class TransactionProxyFactoryBean . This bean will be used in client to call the method that should run within the transaction. Now let’s see the configuration. The client code would look like: OrganizationDao daoService= (OrganizationDao) ctx.getBean("organizationDao"); daoService.<method that must run within a transaction>;
  • 21. 1. Property - target <property name="target" ref="organizationDaoTarget"> Target in a Transaction is nothing but this refers the actual class which contains the implementation of the method where more than one operation run within a transaction. So if any of the operation could not be performed successfully then, all the operations will be rolled back. If there is no issue with any operations defined in the method, the transaction will be committed. Here ref="organizationDaoTarget" is the reference of a bean which refers the implementation class. So you have to define the bean with database template*. (*If you want to know more about the database templates, please visit our presentation: Spring Part – 3 (Spring and Database).) So make an entry for the target bean as: 2. Property - proxyInterface <property name=“proxyInterface" value="org.santosh.dao.OrganizationDao"> This property is related to the property – Target. proxyInterface refers to the interface which is implemented by the target class. This interface is used to proxy the bean. So as you can see the value, the interface org.santosh.dao.OrganizationDao contains the declaration of the method which must run in a transaction and this is implemented by the target class.
  • 22. 3. Property - transactionManager <property name="transactionManager" ref="transactionManager"></property> As we already discussed that Spring comes with a selection of transaction managers that delegate responsibility for transaction management to a platform-specific transaction implementation provided by either JTA or the persistence mechanism. To know more, go back to know the different transaction managers that can be used with Spring. We use DataSourceTransactionManager. 3. Property - transactionAttributes <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_SUPPORTS</prop> </props> </property> Transaction attributes are the most important part in Spring Transactions. In Spring declarative transactions are defined with transaction attributes. A transaction attribute is a description of how transaction policies should be applied to a method.
  • 23. Regardless of which declarative transaction mechanism you use, you’ll have the opportunity to define these attributes. Let’s examine each attribute to understand how it shapes a transaction. Propagation behavior Propagation behavior defines the boundaries of the transaction with respect to the client and to the method being called. Spring defines seven distinct propagation behaviors. These are the constants which you can find in Interface TransactionDefinition. These fields are: static int PROPOGATION_MANDATORY static int PROPOGATION_REQUIRED static int PROPOGATION_REQUIRES_NEW static int PROPOGATION_SUPPORTS static int PROPOGATION_NOT_SUPPORTED static int PROPOGATION_NESTED static int PROPOGATION_NEVER
  • 24. PROPOGATION_MANDATORY: Support a current transaction; throw an exception if no current transaction exists. PROPOGATION_REQUIRED: Support a current transaction; create a new one if none exists. PROPOGATION_REQUIRES_NEW: Indicates that the current method must run within its own transaction. A new transaction is started and if an existing transaction is in progress, it will be suspended for the duration of the method. If using JTATransactionManager, access to TransactionManager is required. PROPOGATION_SUPPORTS: Indicates that the current method does not require a Transactional context, but may run within a transaction if one is already in progress. PROPOGATION_NOT_SUPPORTED: Indicates that the method should not run within a transaction. If an existing transaction is in progress, it will be suspended for the duration of the method. If using JTATransactionManager, access to TransactionManager is required. PROPOGATION_NESTED: Execute within a nested transaction if a current transaction exists, behave like PROPOGATION_REQUIRED else. PROPOGATION_NEVER: Do not support a current transaction; throw an exception if a current transaction exists
  • 25. Isolation levels An isolation level defines how much a transaction may be impacted by the activities of other concurrent transactions. In a typical application, multiple transactions run concurrently, often working with the same data to get their job done. One example, in one transaction, For example, in the EMP table, the query is run to updated salary of Jack from 4000 to 5000. UPDATE emp SET sal=5000 WHERE name='Jack'; But here the it is waiting to commit/rollback the transaction. Now in another transaction, it run the query to read the salary of Jack as: SELECT sal FROM emp WHERE name='Jack'; Now the question is, how much it should print? the committed value 4000 OR the uncommitted value 5000 So there we can set the Isolation level. E.g. when we set the Isolation level to TRANSACTION_READ_UNCOMMITTED, the result should be: the uncommitted value – 5000 When we set the Isolation level to TRANSACTION_READ_COMMITTED, the result should be: the committed value – 4000. If you new to Isolation level, it would look very interesting.
  • 26. So apart from read committed/read uncommitted, we do have other types of Isolation types. But before we see different isolation levels we can set, we need to know why do we require to set the Isolation levels? Now let’s see the problems in a typical application where we run multiple transactions concurrently. The problems are: • Dirty Read • Non-repeatable Read • Phantom Read In table the record is: empid name Sal 1 Jack 4000 2 John 4700 There are two transaction Tx1 and Tx2 • Dirty Reads Tx1 updated one record but not yet committed. UPDATE emp SET sal=5000 WHERE name='Jack'; Same time, Tx2 read that updated data (which is not yet committed by Tx1). SELECT sal FROM emp WHERE name='Jack' Result : 5000 Now Tx1 has rollback. Problem: data obtained by Tx2 is now invalid i.e. it’s 5000 where in DB after rolledback, it remained 4000.
  • 27. • Non-repeatable Reads Tx2 read the data from the table in 3 different times. First time: SELECT sal FROM emp WHERE name='Jack‘ Result: 4000 Second time: SELECT sal FROM emp WHERE name='Jack‘ Result: 5000 Third time: SELECT sal FROM emp WHERE name='Jack‘ Result: 6000 Problem: data obtained by Tx2 is different each time it executed. The reason could be: other transactions such as Tx1 might have updated the data multiple times in different time period and concurrently Tx2 reads those data.
  • 28. • Phantom Reads Tx2 reads all records from a table: SELECT name FROM emp WHERE sal >=4000 ; Result : Jack John Tx1 inserted a records into the table: INSERT INTO emp VALUES (3, 'MIKE', 6300); Tx2 again reads all records from a table: SELECT name FROM emp WHERE sal >=4000 ; Result : Jack John Mike Problem: Data obtained by Tx2 multiple times but it found additional record i.e. employee Mike is found and this is inserted by the concurrent transaction Tx1.
  • 29. To overcome these problems, we need to set the Isolation Level. Similar to Propogation, Spring defines 5 distinct isolation levels that can be set along with Propogation. These are the constants which you can find in Interface TransactionDefinition. These fields are: static int ISOLATION_DEFAULT static int ISOLATION_READ_COMMITTED static int ISOLATION_READ_UNCOMMITTED static int ISOLATION_REPEATABLE_READ static int ISOLATION_SERIALIZABLE Isolation Level Dirty Reads Non-Repeatable Reads Phantom Reads ISOLATION_READ_COMMITTED Prevented Allowed Allowed ISOLATION_READ_UNCOMMITTED Allowed Allowed Allowed ISOLATION_REPEATABLE_READ Prevented Prevented Allowed ISOLATION_SERIALIZABLE Prevented Prevented Prevented ISOLATION_READ_UNCOMMITTED is the most efficient isolation level, but isolates the transaction the least, leaving the transaction open to dirty, non-repeatable, and phantom reads. At the other extreme, ISOLATION_SERIALIZABLE prevents all forms of isolation problems but is the least efficient.
  • 30. So as the transaction attribute, we have choosed: propogation as: PROPOGATION_REQUIRED Isolation as: ISOLATION_SERIALIZABLE
  • 31. Do you have Questions ? Please write to: santosh.bsil@yahoo.co.in