124 lines
3.2 KiB
Markdown
124 lines
3.2 KiB
Markdown
# ROA2WEB Shared Database Pool
|
|
|
|
Sistem de pool de conexiuni Oracle partajat între toate microserviciile ROA2WEB.
|
|
|
|
## Componente
|
|
|
|
### 📦 oracle_pool.py
|
|
Clasa singleton `OraclePool` pentru gestionarea pool-ului de conexiuni Oracle.
|
|
|
|
### 📋 models.py
|
|
Modele Pydantic comune:
|
|
- `User` - Model pentru utilizatori
|
|
- `Company` - Model pentru firme/scheme Oracle
|
|
- `DatabaseConfig` - Configurare conexiune database
|
|
|
|
### ⚙️ config.py (în utils/)
|
|
Configurări partajate prin environment variables.
|
|
|
|
### ❌ exceptions.py (în utils/)
|
|
Exception handlers personalizate pentru ROA2WEB.
|
|
|
|
## Utilizare
|
|
|
|
### Inițializare în aplicații FastAPI
|
|
|
|
```python
|
|
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
import sys
|
|
import os
|
|
|
|
# Import shared pool
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), '../../../shared'))
|
|
from database.oracle_pool import oracle_pool
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Startup - inițializare pool
|
|
await oracle_pool.initialize()
|
|
print("📊 Oracle pool initialized")
|
|
|
|
yield
|
|
|
|
# Shutdown - închidere pool
|
|
await oracle_pool.close_pool()
|
|
print("📊 Oracle pool closed")
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
```
|
|
|
|
### Utilizare conexiune în endpoint-uri
|
|
|
|
```python
|
|
from fastapi import APIRouter, HTTPException
|
|
from database.oracle_pool import oracle_pool
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/companies")
|
|
async def get_companies():
|
|
try:
|
|
async with oracle_pool.get_connection() as conn:
|
|
with conn.cursor() as cursor:
|
|
cursor.execute("SELECT schema, firma FROM vdef_util_grup WHERE id_firma <> 0")
|
|
results = cursor.fetchall()
|
|
|
|
companies = []
|
|
for row in results:
|
|
companies.append({
|
|
"code": row[0],
|
|
"name": row[1]
|
|
})
|
|
|
|
return companies
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
|
|
```
|
|
|
|
### Configurare Environment Variables
|
|
|
|
```bash
|
|
# Oracle Database
|
|
ORACLE_USER=your_oracle_username
|
|
ORACLE_PASSWORD=your_oracle_password
|
|
ORACLE_DSN=your_oracle_dsn
|
|
|
|
# Pool Settings
|
|
DB_MIN_CONNECTIONS=2
|
|
DB_MAX_CONNECTIONS=10
|
|
DB_CONNECTION_INCREMENT=1
|
|
|
|
# JWT (pentru autentificare)
|
|
JWT_SECRET_KEY=your-super-secret-key
|
|
ACCESS_TOKEN_EXPIRE_MINUTES=30
|
|
```
|
|
|
|
## Testare
|
|
|
|
Pentru a testa pool-ul de conexiuni:
|
|
|
|
```bash
|
|
cd roa2web/shared/database
|
|
python test_pool.py
|
|
```
|
|
|
|
**Notă**: Testul necesită configurarea variabilelor de environment pentru Oracle.
|
|
|
|
## Caracteristici
|
|
|
|
✅ **Singleton Pattern** - O singură instanță de pool pentru toată aplicația
|
|
✅ **Async Context Manager** - Gestionare automată a conexiunilor
|
|
✅ **Connection Pooling** - Performanță optimizată prin reutilizarea conexiunilor
|
|
✅ **Configurabil** - Setări flexibile prin environment variables
|
|
✅ **Logging** - Urmărirea operațiilor de pool
|
|
✅ **Error Handling** - Excepții personalizate pentru debugging
|
|
|
|
## Următorii Pași
|
|
|
|
👉 **ZIUA 3**: Implementarea sistemului JWT partajat (`shared/auth/`)
|
|
|
|
---
|
|
|
|
*Documentație generată pentru ROA2WEB Shared Database Pool - ZIUA 2* 🚀 |