Return to site

๐Ÿƒ๐ŸŽ“ SPRING CERTIFICATION QUESTION: What is the default rollback policy? How can you override it?

ยท vcp,spring,java

โ€๐Ÿซ Answer:

The default rollback policy is defined by DefaultTransactionAttribute

which states that rollback should be performed only โ€ผ๏ธ in case of RuntimeException and Error โŒ but not on checked exceptions.

This default behavior can be overridden using (in case a declarative approach is used):

- Transactional#noRollbackFor()

- Transactional#noRollbackForClassName()

- Transactional#rollbackFor()

- Transactional#rollbackForClassName()

annotation attributes.

#spring #certificationquestion #vcp

Transaction Rollback ๐Ÿ‘‰ https://lnkd.in/gbTBtG6W

The @Transactional annotation is the metadata that specifies the semantics of the transactions on a method.

We have two ways to roll back a transaction: declarative and programmatic.

In the declarative approach, we annotate the methods with the @Transactional annotation.

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 roll back โ†ฉ๏ธon runtime exceptions.

1๏ธโƒฃ Let's see a simple example using the declarative approach to rollback a transaction for runtime exceptions or errors:

@๐“๐ซ๐š๐ง๐ฌ๐š๐œ๐ญ๐ข๐จ๐ง๐š๐ฅ

๐ฉ๐ฎ๐›๐ฅ๐ข๐œ ๐ฏ๐จ๐ข๐ ๐œ๐ซ๐ž๐š๐ญ๐ž๐‚๐จ๐ฎ๐ซ๐ฌ๐ž๐ƒ๐ž๐œ๐ฅ๐š๐ซ๐š๐ญ๐ข๐ฏ๐ž๐–๐ข๐ญ๐ก๐‘๐ฎ๐ง๐ญ๐ข๐ฆ๐ž๐„๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง(๐‚๐จ๐ฎ๐ซ๐ฌ๐ž ๐œ๐จ๐ฎ๐ซ๐ฌ๐ž) {

๐œ๐จ๐ฎ๐ซ๐ฌ๐ž๐ƒ๐š๐จ.๐œ๐ซ๐ž๐š๐ญ๐ž(๐œ๐จ๐ฎ๐ซ๐ฌ๐ž);

๐ญ๐ก๐ซ๐จ๐ฐ ๐ง๐ž๐ฐ ๐ƒ๐š๐ญ๐š๐ˆ๐ง๐ญ๐ž๐ ๐ซ๐ข๐ญ๐ฒ๐•๐ข๐จ๐ฅ๐š๐ญ๐ข๐จ๐ง๐„๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง("๐“๐ก๐ซ๐จ๐ฐ๐ข๐ง๐  ๐ž๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง ๐Ÿ๐จ๐ซ ๐๐ž๐ฆ๐จ๐ข๐ง๐  ๐‘๐จ๐ฅ๐ฅ๐›๐š๐œ๐ค!!!");

}

2๏ธโƒฃ Next, we'll use the declarative approach to roll back a transaction for the listed checked exceptions.

The rollback e is on SQLException:

@๐“๐ซ๐š๐ง๐ฌ๐š๐œ๐ญ๐ข๐จ๐ง๐š๐ฅ(๐ซ๐จ๐ฅ๐ฅ๐›๐š๐œ๐ค๐…๐จ๐ซ = { ๐’๐๐‹๐„๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง.๐œ๐ฅ๐š๐ฌ๐ฌ })

๐ฉ๐ฎ๐›๐ฅ๐ข๐œ ๐ฏ๐จ๐ข๐ ๐œ๐ซ๐ž๐š๐ญ๐ž๐‚๐จ๐ฎ๐ซ๐ฌ๐ž๐ƒ๐ž๐œ๐ฅ๐š๐ซ๐š๐ญ๐ข๐ฏ๐ž๐–๐ข๐ญ๐ก๐‚๐ก๐ž๐œ๐ค๐ž๐๐„๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง(๐‚๐จ๐ฎ๐ซ๐ฌ๐ž ๐œ๐จ๐ฎ๐ซ๐ฌ๐ž) ๐ญ๐ก๐ซ๐จ๐ฐ๐ฌ ๐’๐๐‹๐„๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง {

๐œ๐จ๐ฎ๐ซ๐ฌ๐ž๐ƒ๐š๐จ.๐œ๐ซ๐ž๐š๐ญ๐ž(๐œ๐จ๐ฎ๐ซ๐ฌ๐ž);

๐ญ๐ก๐ซ๐จ๐ฐ ๐ง๐ž๐ฐ ๐’๐๐‹๐„๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง("๐“๐ก๐ซ๐จ๐ฐ๐ข๐ง๐  ๐ž๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง ๐Ÿ๐จ๐ซ ๐๐ž๐ฆ๐จ๐ข๐ง๐  ๐ซ๐จ๐ฅ๐ฅ๐›๐š๐œ๐ค");

}

3๏ธโƒฃ Let's see a simple use of attribute noRollbackFor in the declarative approach to prevent rollback of the transaction for the listed exception:

@๐“๐ซ๐š๐ง๐ฌ๐š๐œ๐ญ๐ข๐จ๐ง๐š๐ฅ(๐ง๐จ๐‘๐จ๐ฅ๐ฅ๐›๐š๐œ๐ค๐…๐จ๐ซ = { ๐’๐๐‹๐„๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง.๐œ๐ฅ๐š๐ฌ๐ฌ })

๐ฉ๐ฎ๐›๐ฅ๐ข๐œ ๐ฏ๐จ๐ข๐ ๐œ๐ซ๐ž๐š๐ญ๐ž๐‚๐จ๐ฎ๐ซ๐ฌ๐ž๐ƒ๐ž๐œ๐ฅ๐š๐ซ๐š๐ญ๐ข๐ฏ๐ž๐–๐ข๐ญ๐ก๐๐จ๐‘๐จ๐ฅ๐ฅ๐๐š๐œ๐ค(๐‚๐จ๐ฎ๐ซ๐ฌ๐ž ๐œ๐จ๐ฎ๐ซ๐ฌ๐ž) ๐ญ๐ก๐ซ๐จ๐ฐ๐ฌ ๐’๐๐‹๐„๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง {

๐œ๐จ๐ฎ๐ซ๐ฌ๐ž๐ƒ๐š๐จ.๐œ๐ซ๐ž๐š๐ญ๐ž(๐œ๐จ๐ฎ๐ซ๐ฌ๐ž);

๐ญ๐ก๐ซ๐จ๐ฐ ๐ง๐ž๐ฐ ๐’๐๐‹๐„๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง("๐“๐ก๐ซ๐จ๐ฐ๐ข๐ง๐  ๐ž๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง ๐Ÿ๐จ๐ซ ๐๐ž๐ฆ๐จ๐ข๐ง๐  ๐ซ๐จ๐ฅ๐ฅ๐›๐š๐œ๐ค");

}