https://github.com/jorgermduarte/poc-transactions-readonly
https://github.com/jorgermduarte/poc-transactions-readonly
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jorgermduarte/poc-transactions-readonly
- Owner: jorgermduarte
- License: mit
- Created: 2025-03-06T15:01:52.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-03-06T16:45:14.000Z (7 months ago)
- Last Synced: 2025-08-12T13:02:26.192Z (about 2 months ago)
- Size: 95.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# poc-transactions-readonly

As we can see, for the GET http://localhost:8080/users, we are using a standby database (which as a Transactional(readOnly = true)), while for the creation of the user its using the primary db.
The DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT strategy resolved the issue because it delays the acquisition of a database connection until Hibernate is certain the transaction's readOnly flag is properly set. This ensures the DataSource router can correctly identify the transaction context before selecting a connection.
## Why It Worked:
- Hibernate waits until the transaction is fully initialized (including the readOnly flag) before acquiring a connection.
- This ensures the ReadWriteRoutingDataSource sees the correct readOnly state when choosing between "primary" and "standby".## Potential Pitfalls:
- Connection Leaks: Ensure your connection pool (e.g., HikariCP) is configured to validate connections.
- Long Transactions: If transactions are held open for too long, connections might not be released promptly. Adjust timeouts if needed.