Implement Context-based Transactional Session Management #10
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
🚀 Implement Context-based Transactional Session Management with Depth Tracking
📋 Overview
This PR introduces a robust, nested-safe transactional session management system for PySpringModel with explicit session depth tracking. The implementation replaces manual session handling with a context-aware approach that automatically manages database sessions and transactions across the entire application stack, ensuring proper transaction boundaries through depth monitoring.
🎯 Problem Statement
Previously, the codebase had several session management issues:
withblockscreate_session()andcreate_managed_session()usage🛠️ Solution
Implemented a Context Variable-based Session Management system with explicit depth tracking featuring the following components:
🔧 Core Components
1. SessionContextHolder with Depth Tracking (
py_spring_model/core/session_context_holder.py)contextvars_session_depthContextVarget_or_create_session(),has_session(), andclear_session()methodsget_session_depth(): Returns current nesting levelenter_session(): Increments depth and returns new levelexit_session(): Decrements depth and cleans up session at level 02. @transactional Decorator with Depth Awareness
@Transactionalmethods reuse the outer transaction's session3. TransactionalDepth Enum
4. Session Middleware (
py_spring_model/py_spring_model_rest/controller/session_controller.py)🔄 Key Changes
Repository Layer (
py_spring_model/repository/crud_repository.py)with self.create_managed_session()SessionContextHolder.get_or_create_session()and@TransactionaldecoratorService Layer (
py_spring_model/py_spring_model_rest/service/)@Transactionaldecoratorwith PySpringModel.create_session()patternsModule Exports (
py_spring_model/__init__.py)SessionContextHolderto public API for depth management accessSessionControllerto REST controller registration🎁 Benefits
🔒 Transaction Safety with Depth Control
🎯 Smart Nested Transaction Support
📊 Depth-Aware Transaction Monitoring
🧹 Automatic Resource Management
with sessionblocks needed🚀 Performance Improvements
🧪 Testing Updates
Core Session Depth Tests (
tests/test_session_depth.py)Enhanced Transactional Tests (
tests/test_transactional_decorator.py)Repository & Service Tests
tests/test_crud_repository.pytests/test_crud_repository_implementation_service.pytests/test_query_modifying_operations.py🔄 Migration Guide
For Application Code:
For Repository Usage:
For Debugging Transaction Hierarchy:
sessionparameterswith sessionblocks may need updates🔍 Backwards Compatibility
save(),find_by_id(), etc.)SessionContextHolder🧪 How to Test
📚 Usage Examples
Simple Transaction
Complex Nested Transactions
Depth-Aware Error Handling
🔧 Session Depth Architecture
Depth Management Flow
Session Lifecycle with Depth
This implementation provides a solid foundation for reliable, performant database transaction management with intelligent depth tracking, ensuring proper transaction boundaries while maintaining clean, readable code patterns. The explicit depth management eliminates guesswork about transaction hierarchy and provides robust nested transaction support. 🎉