Transaction Pitfalls (Part 2)

1. Non-Public Access Modifier

Spring transactions are implemented through AOP dynamic proxies. JDK dynamic proxy requires the target method to be public.

Problem Cause:

  • When the method is not public, it directly returns null, causing transaction to fail

Solution:

  • Ensure method is public
  • Avoid self-invocation
  • Consider using AspectJ mode

2. Database Storage Engine Does Not Support Transactions

MyISAM storage engine does not support transactions, while InnoDB fully supports ACID transaction features.

Solution:

  • Specify ENGINE=InnoDB when creating tables
  • Or use ALTER TABLE command to convert existing tables

3. Annotation Configuration Error (readOnly = true)

When readOnly = true is set, attempting to execute write operations throws an exception:

“Connection is read-only. Queries leading to data modification are not allowed”

Solution:

  • Write operation methods should remove readOnly attribute or set it to false

4. Transaction Timeout Too Short

timeout parameter set too short will cause transaction to directly timeout and fail.

5. Incorrect Propagation Mechanism

When using improper propagation mechanisms like NOT_SUPPORTED, although code throws an exception, database data will still be inserted.