Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between physical and logical transactions in spring

In this spring document http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/transaction.html .It talks about physical and logical transactions.I have written a very simple program to understand it

TransactionObject.java

class TransactionObject {

    private static Connection conn;

    TransactionObject() {
        try {
            conn = DriverManager.getConnection("DB_URL", "USER", "PASS");
        } catch (SQLException e) {}
    }

    void rollBack() {
        try {
            conn.rollback();
        } catch (SQLException e) { }
    }

    void Commit() {
        try {
            conn.commit();
        } catch (SQLException e) { }
    }
}

ClassMethodUnderTransactionObject.java

public class ClassMethodUnderTransactionObject {
    // start fresh TransactionObject
    TransactionObject  logical= new TransactionObject();

    M2(); // call M2 use same logical object
    M3(); // call M3 use same logical object
    // For M2 and M3 it is logical transaction because both using same transaction object

    if(error){
        logical.rollBack();  
    } else {
        logical.Commit(); 
    }

    // create another instance of TransactionObject
    TransactionObject  physical=new TransactionObject();

    M4(); // call M4 use different TransactionObject object
    if(error){
        physical.rollBack();  
    } else {
        physical.Commit(); 
    }

So M4 uses physical transaction in respect to M2 and M3 becuase M4 uses different trasnactionObject

Is that the way we should understand ?Is there more to understand.Please help me to understand it.

like image 791
abishkar bhattarai Avatar asked May 24 '13 09:05

abishkar bhattarai


People also ask

How many types of transactions are there in Spring?

Spring supports both programmatic and declarative transaction management.

What is the difference between Spring transaction and hibernate transaction?

Spring framework is useful for transaction management, dependency injection; aspect-oriented programming for applications whereas Hibernate framework is useful for object-relational persistence, access data layers, and query retrieval services for enterprise-level applications.

What is use of @transactional annotation in Spring?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

What is @transactional annotation in Spring MVC?

The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics; for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction".


2 Answers

I understand this like this

  • physical transaction is from external world
  • logical transaction is your internal.

This has many consequences described in documentation.

I dont get your example.

like image 189
MariuszS Avatar answered Oct 05 '22 05:10

MariuszS


According to the Spring documentation, PROPAGATION.PROPAGATION_REQUIRED creates a new transaction if a transaction doesn't exist yet or uses an existing one if one already exists.

Suppose we already have a transaction running and are going to apply a transaction to methodA, methodB and methodC in one of our services.

In this scenario, methodA, methodB, methodC will be running in logical transactions while there will be only one physical transaction which encapsulates all other logical transactions. All these logical transaction scopes are logically independent from the physical transaction scope. But they are mapped to the same physical transaction.

So: logical transaction of methodA + logical transaction of methodB + logical transaction of methodB -> physical transaction

Every logical transaction can define its separate rollback rules. Once the rollback rule triggered from logical transaction, an UnexpectedRollbackException is thrown but the physical transaction can still call commit because the physical transaction is independent from the scope of the logical transaction. But when the physical transaction encounters UnexpectedRollbackException (which is an unchecked RuntimeException), it triggers the rollback of the entire physical transaction and the client will see that a rollback occurred.

like image 21
vipal kaila Avatar answered Oct 05 '22 06:10

vipal kaila