28-Transactions Flashcards

1
Q

Propagations in Transactional

A
  1. MANDATORY: throw an exception if no current txn
  2. NESTED: create nested transaction if current txn exists, create new txn otherwise
  3. NEVER: throws an exception if current txn exists
  4. NOT_SUPPORTED: run without txn
  5. REQUIRED: create new txn if not exist
  6. REQUIRES_NEW: create new txn if not exist. Run in new independent txn otherwise
  7. SUPPORTS: don’t create txn. Just use if exists
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Difference between Propagation.REQUIRED_NEW and Propagation.NESTED

A

REQUIRED_NEW: suspend current transaction, create a new transaction which won’t affect outer transaction if fails
NESTED: create a nested transaction if the current transaction exists. Failure in the nested transaction will roll back the current one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Isolations in Transactional

A
DEFAULT
READ_UNCOMMITTED
READ_COMMITTED
REPEATABLE_READ
SERIALIZABLE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

___means that none of the actions taken in a transaction violate any integrity constraints (e.g. foreign key constraint)

A

C in ACID

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Transaction APIs supported by Spring

A

JTA: java transaction API - distributed transaction
Hibernate
JPA
JDO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is D in ACID?

A

Committed changes are permanent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create JDBC transaction

A

conn = dataSource.getConnection()

conn. setAutoCommit(false)
conn. commit()
conn. rollback()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create JMS transaction

A

session = conn.createSession(true, 0)

session. commit()
session. rollback()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create JPA transaction

A

tx = entityManager.getTransaction()

tx. begin()
tx. commit()
tx. rollback()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create Hibernate transaction

A

tx = session.beginTransaction()

tx. commit()
tx. rollback()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between local and global transactions?

A

Local: single resource (e.g. db) & connection
Global: multiple resources (distributed transaction)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the requirement for using JTA?

A

JTA implementation e.g.

  1. Full application server (WebSphere, JBoss, WebLogic)
  2. Standalone implementation (Atomikos, JTOM)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What API does Spring use for the global and local transactions?

A

Use the same API

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the base interface that Spring uses to abstract transaction APIs?

A

PlatformTransactionManager

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Annotation to enable transaction in Spring. Where we should put it?

A

@EnableTransactionManager

Add it to a configuration class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

List down 6 implementations of PlatformTransactionManager?

A
  1. DataSourceTransactionManager
  2. JmsTransactionManager
  3. JpaTransactionManager
  4. JtaTransactionManager
  5. WebLogicJtaTransactionManager
  6. WebSphereUowTransactionManager
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Recommended bean name for transaction management bean?

A

transactionManager

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to create a transaction manager from a data source?

A

new DataSourceTransactionManager(dataSource)

19
Q

Create a JTA transaction manager bean?

A
@Bean PlatformTransactionManager transactionManager() {
    return new JtaTransactionManager();
}
20
Q

Get the current transaction manually

A

DataSourceUtils.getConnection(dataSource);

21
Q

What happens if putting @Transactional to the interface?

A

Transaction will be applied for all methods

22
Q

What happens if putting @Transactional to the class?

A
  1. Transaction will be applied for public methods only if it doesn’t implement an interface
  2. Apply to all interface methods if it’s a bean of an interface. For this, putting annotation to class or interface takes the same effect
23
Q

Java’s @Transactional vs. Spring’s Transactional

A
  1. Also supported by Spring but fewer options

2. Should use Spring’s Transactional instead

24
Q

What is the default for Transactional.propagation?

A

REQUIRED

25
Q

When does a transaction get rolled back?

A

When throwing RuntimeException e.g. DataAccessException, HibernateException, etc.
or Error

26
Q

How to customize the rollback behavior?

A

@Transactional(rollbackFor=MyCheckedException.class,

noRollbackFor={JmxException.class, MailException.class})

27
Q

What happens if adding @Transactional to test methods/classes?

A

Transaction will be rolled back afterward

No need to clean up the database after testing

28
Q

How to persist data in 1 specific test method if its class is annotated with @Transactional?

A
Using @Commit for that method e.g.
@Transactional
class TestClass {
    @Test @Commit testMethod() {}
}
29
Q

How to write transactional code manually?

A
return new TransactionTemplate(txManager).execute(status -> {
    try {} catch(SomeException e) {
        status.setRollbackOnly();
    }
});
30
Q

When should use @Transactional(readOnly=true)?

A

To optimize performance if all queries inside are read, it will use 1 connection only.

31
Q

How to prevent modified data when reading?

A

@Transactional(readonly=true, isolation=REPEATABLE_READ) to prevent reading modified data until transaction commits

32
Q

In Spring test, how to run code before/after a transaction?

A
Using @BeforeTransaction e.g.
class TestClass
    // run before and outside transaction
    @BeforeTransaction beforeTransaction() {} 
    // run before and inside transaction
    @BeforeEach setup() {}
    @Test @Transactional void test() {}
33
Q

How to configure transaction with @Sql

A

Using @Sql(config=@SqlConfig(transactionMode=***))

34
Q

TransactionModes in @SqlConfig

A
  1. ISOLATED: run @Sql script in a new transaction
  2. INFERRED: get existing one or create new otherwise
  3. DEFAULT:
    a. If it’s set for method –> get from @SqlConfig in class level. Use INFERRED if there is no @SqlConfig in class level
    b. If it’s set for class –> it’s INFERRED
35
Q

Usage of @Primary

A

If there’re multiple beans of the same type. The 1 with @Primary is the preference for @Autowired

36
Q

How to specify transaction manager in @Transactional if there’s more than 1 txManager?

A

@Transactional(“name-of-tx-manager”) or

@Transactional(transactionManager=”name-of-tx-manager”)

37
Q

How global transaction works?

A

Two-phase commit

38
Q

How does @Sql work if not passing SQL file name?

A

Class level: ClassName.sql in the same package

Method level: ClassName.methodName.sql in the same package

39
Q

What are execution phases in @Sql?

A

@Sql(executionPhase=BEFORE_TEST_METHOD)

@Sql(executionPhase=AFTER_TEST_METHOD)

40
Q

How to config ErrorMode in @Sql

A

@Sql(config=@SqlConfig(errorMode=***))

41
Q

ErrorModes in @SqlConfig

A
  1. DEFAULT: get from the parent (e.g. Class level), if not exist then FAIL_ON_ERROR
  2. FAIL_ON_ERROR
  3. CONTINUE_ON_ERROR
  4. IGNORE_FAILED_DROPS: ignore all errors from “DROP IF EXISTS”
42
Q

What can we control with @SqlConfig?

A
  1. errorMode
  2. transactionMode
  3. transactionManager: bean name
  4. dataSource: bean name
    and some others
43
Q

Ways to run SQL scripts before each test?

A
  1. Using @Sql
  2. Create a bean of DataSourceInitializer
    a. Create ResourceDatabasePopulator
    b. ResourceDatabasePopulator.addScript(“init.sql”);
    c. DataSourceInitializer. setDatabasePopulator(ResourceDatabasePopulator);
44
Q

Which of the following exceptions, when thrown, will cause a Spring transaction rollback by default?

A

If no custom rollback rules apply, the transaction will roll back on RuntimeException and Error but not on checked exceptions.