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?

25
When does a transaction get rolled back?
When throwing RuntimeException e.g. DataAccessException, HibernateException, etc. or Error
26
How to customize the rollback behavior?
@Transactional(rollbackFor=MyCheckedException.class, | noRollbackFor={JmxException.class, MailException.class})
27
What happens if adding @Transactional to test methods/classes?
Transaction will be rolled back afterward | No need to clean up the database after testing
28
How to persist data in 1 specific test method if its class is annotated with @Transactional?
``` Using @Commit for that method e.g. @Transactional class TestClass { @Test @Commit testMethod() {} } ```
29
How to write transactional code manually?
``` return new TransactionTemplate(txManager).execute(status -> { try {} catch(SomeException e) { status.setRollbackOnly(); } }); ```
30
When should use @Transactional(readOnly=true)?
To optimize performance if all queries inside are read, it will use 1 connection only.
31
How to prevent modified data when reading?
@Transactional(readonly=true, isolation=REPEATABLE_READ) to prevent reading modified data until transaction commits
32
In Spring test, how to run code before/after a transaction?
``` 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
How to configure transaction with @Sql
Using @Sql(config=@SqlConfig(transactionMode=***))
34
TransactionModes in @SqlConfig
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
Usage of @Primary
If there're multiple beans of the same type. The 1 with @Primary is the preference for @Autowired
36
How to specify transaction manager in @Transactional if there's more than 1 txManager?
@Transactional("name-of-tx-manager") or | @Transactional(transactionManager="name-of-tx-manager")
37
How global transaction works?
Two-phase commit
38
How does @Sql work if not passing SQL file name?
Class level: ClassName.sql in the same package | Method level: ClassName.methodName.sql in the same package
39
What are execution phases in @Sql?
@Sql(executionPhase=BEFORE_TEST_METHOD) | @Sql(executionPhase=AFTER_TEST_METHOD)
40
How to config ErrorMode in @Sql
@Sql(config=@SqlConfig(errorMode=***))
41
ErrorModes in @SqlConfig
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
What can we control with @SqlConfig?
1. errorMode 2. transactionMode 3. transactionManager: bean name 4. dataSource: bean name and some others
43
Ways to run SQL scripts before each test?
1. Using @Sql 2. Create a bean of DataSourceInitializer a. Create ResourceDatabasePopulator b. ResourceDatabasePopulator.addScript("init.sql"); c. DataSourceInitializer. setDatabasePopulator(ResourceDatabasePopulator);
44
Which of the following exceptions, when thrown, will cause a Spring transaction rollback by default?
If no custom rollback rules apply, the transaction will roll back on RuntimeException and Error but not on checked exceptions.