File size: 1,481 Bytes
9cb712d
 
4995bb4
aa34f6b
57d78b2
aa34f6b
 
 
 
9545da5
aa34f6b
9545da5
 
aa34f6b
9545da5
 
 
aa34f6b
 
9545da5
 
4995bb4
 
 
9545da5
 
 
 
9cb712d
 
 
14cd2bc
9cb712d
 
9545da5
 
 
9cb712d
 
9545da5
 
57d78b2
4d860a2
 
 
 
 
 
aa34f6b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from app.core.config import settings
from psycopg_pool import ConnectionPool
from contextlib import contextmanager


DB_URL_FOR_CHECKPOINTER_STORE=settings.DB_URL_FOR_CHECKPOINTER_STORE


pool = ConnectionPool(
    conninfo=DB_URL_FOR_CHECKPOINTER_STORE,
    min_size=1,
    max_size=10,
    check=ConnectionPool.check_connection,
    max_idle=300,
    kwargs={"autocommit": True,"sslmode": "require"}
)

# sslmode=require means that our application will only connect to the database if a secure, encrypted connection can be established. 


DB_URL_FOR_SQL_AL=settings.DB_URL_FOR_SQL_AL


# pool_pre_ping=True: It checks if a connection is still alive before using it. If it's dead, it recycles it so the app doesn't crash.
# pool_recycle=300: It closes and recreates connections every 5 minutes (300 seconds) to prevent stale connections.

engine = create_engine(
    DB_URL_FOR_SQL_AL, 
    pool_pre_ping=True, 
    pool_recycle=300
)



# This is a factory that generates individual database sessions
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)

# This creates a session factory. We set autoflush=False and autocommit=False because we want manual control over when data is saved (committed) to the database, which prevents accidental or premature changes.


def get_session():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()