11# Thread-Safe Transactions
22
3- Starting from version 3.5.0, the Entity library supports thread-safe transactions through connection pooling.
3+ Starting from version 3.5.0, the Entity library supports thread-safe transactions through connection pooling with a simplified API .
44
55## Problem
66
@@ -15,7 +15,7 @@ Thread1: МенеджерСущностей.ЗафиксироватьТранз
1515
1616## Solution
1717
18- The library now supports connection pooling. Each connection has its own transaction state , ensuring thread safety.
18+ The library now supports connection pooling with automatic context management . Each transaction gets its own context and connector , ensuring thread safety without complex parameter passing .
1919
2020## Usage
2121
@@ -31,49 +31,34 @@ The library now supports connection pooling. Each connection has its own transac
3131);
3232```
3333
34- ### Thread-Safe Transactions
34+ ### Simplified Thread-Safe Transactions
3535
3636``` bsl
37- // Thread 1: Get connection from pool
38- Соединение1 = МенеджерСущностей.НачатьТранзакцию();
37+ // Simple approach - library automatically manages contexts
38+ КонтекстID = МенеджерСущностей.НачатьТранзакцию();
3939
40- // Thread 2: Get different connection from pool
41- Соединение2 = МенеджерСущностей.НачатьТранзакцию();
40+ // All CRUD operations can optionally use the context
41+ МенеджерСущностей.Сохранить(Сущность, КонтекстID);
42+ ЗагруженныеСущности = МенеджерСущностей.Получить(Тип("МояСущность"), Неопределено, КонтекстID);
4243
43- // Each thread works with its own connection
44- МенеджерСущностей.Сохранить(Сущность1, Соединение1);
45- МенеджерСущностей.Сохранить(Сущность2, Соединение2);
46-
47- // Thread 1: Commit only its transaction
48- МенеджерСущностей.ЗафиксироватьТранзакцию(Соединение1);
49-
50- // Thread 2: Rollback only its transaction
51- МенеджерСущностей.ОтменитьТранзакцию(Соединение2);
44+ // Commit the transaction
45+ МенеджерСущностей.ЗафиксироватьТранзакцию(КонтекстID);
5246```
5347
54- ### Manual Connection Management
48+ ### Multiple Concurrent Transactions
5549
5650``` bsl
57- // Get connection from pool for custom operations
58- Соединение = МенеджерСущностей.ПолучитьСоединение();
51+ // Thread 1:
52+ КонтекстID1 = МенеджерСущностей.НачатьТранзакцию();
53+ МенеджерСущностей.Сохранить(Сущность1, КонтекстID1);
5954
60- // Use for CRUD operations
61- МенеджерСущностей.Сохранить(Сущность, Соединение);
62- МенеджерСущностей.Получить(Тип("МояСущность"), ОпцииПоиска, Соединение);
63-
64- // Always return connection to pool when done
65- МенеджерСущностей.ВернутьСоединение(Соединение);
66- ```
67-
68- ### Repository Usage
69-
70- ``` bsl
71- ХранилищеСущностей = МенеджерСущностей.ПолучитьХранилищеСущностей(Тип("МояСущность"));
55+ // Thread 2: Independent transaction
56+ КонтекстID2 = МенеджерСущностей.НачатьТранзакцию();
57+ МенеджерСущностей.Сохранить(Сущность2, КонтекстID2);
7258
73- // Thread-safe transaction through repository
74- Соединение = ХранилищеСущностей.НачатьТранзакцию();
75- ХранилищеСущностей.Сохранить(Сущность, Соединение);
76- ХранилищеСущностей.ЗафиксироватьТранзакцию(Соединение);
59+ // Each thread commits independently
60+ МенеджерСущностей.ЗафиксироватьТранзакцию(КонтекстID1); // Thread 1
61+ МенеджерСущностей.ОтменитьТранзакцию(КонтекстID2); // Thread 2
7762```
7863
7964## Backward Compatibility
@@ -88,15 +73,31 @@ All existing code continues to work without modification:
8873МенеджерСущностей.ЗафиксироватьТранзакцию();
8974```
9075
76+ ### CRUD Operations
77+
78+ All CRUD operations support optional context parameters:
79+
80+ ``` bsl
81+ // Without context (uses default connector)
82+ МенеджерСущностей.Сохранить(Сущность);
83+ Результат = МенеджерСущностей.Получить(Тип("МояСущность"));
84+
85+ // With context (uses transaction-specific connector)
86+ МенеджерСущностей.Сохранить(Сущность, КонтекстID);
87+ Результат = МенеджерСущностей.Получить(Тип("МояСущность"), ОпцииПоиска, КонтекстID);
88+ МенеджерСущностей.Удалить(Сущность, КонтекстID);
89+ ```
90+
9191## Connection Pool Configuration
9292
9393- ** Pool Size** : Determines the maximum number of concurrent connections
94- - ** Default** : Pool is disabled (backward compatibility)
94+ - ** Default** : Pool is disabled (backward compatibility)
9595- ** Recommendation** : Set pool size to expected number of concurrent threads
9696
97- ## Performance Considerations
97+ ## Key Features
9898
99- - Each connection in the pool maintains its own database connection
100- - Connections are reused to minimize connection overhead
101- - Pool automatically expands when more connections are needed than pool size
102- - Unused connections are closed when returned to an oversized pool
99+ - ** Simplified API** : No need to manually manage connection objects
100+ - ** Automatic Context Management** : Connectors are automatically assigned to contexts
101+ - ** Backward Compatibility** : All existing code works unchanged
102+ - ** Thread Safety** : Each transaction context is isolated
103+ - ** Performance** : Connection reuse minimizes database connection overhead
0 commit comments