0
votes

Does Spring and Hibernate supports Session sharing between two different (nested or sequenced) physical Transactions ?

I know that Spring supports nested transactions, but it is the same Physical Transaction just with save points, i.e. nested transaction is separated logically :

From Spring docs:

PROPAGATION_NESTED uses a single physical transaction with multiple savepoints that it can roll back to.

So, can i achive behavior similar to:

@Transactional
void invokeOuterTransaction() {
    invokeInnerTransaction();
}
@Transactional
void invokeInnerTransaction() {
    // here are the same Session as in 'invokeOuterTransaction', 
    // but this transaction is new PHYSICAL transaction
}
1
Hibernate does not support savepoints.Grim

1 Answers

0
votes

So, exploring this issue, using setup below, i discovered, that Hibernate Session "per request" and not "per transaction" is quite interesting.

Setup: Hibernate 5, Spring 5, PostgreSQL

Below is a quick java-like pseudo-code for short:

@Controller {
    @Inject FirstService fServ;
    @Inject SecondService sServ;

    @RequestMapping
    handleHttpRequest() {
          fServ.invokeFirstTransactional();
          sServ.invokeSecondTransactional();
    }
}
FirstService {
   @Transactional
   void invokeFirstTransactional() {
        // Session object system hashcode = 187000543
        // Thread object system hashcode = 167000522

        // Transaction_ID in database = 650
   }
}
SecondService {
   @Transactional
   void invokeSecondTransactional() {
        // Session object system hashcode = 187000543
        // Thread object system hashcode = 167000522

        // Transaction_ID in database = 651
   }
}

As you can see - same Hibernate Session, Same Thread, but DIFFERENT PHYSICAL transactions !