-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path__init__.py
45 lines (40 loc) · 1.06 KB
/
__init__.py
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
# -*- coding: utf-8 -*-
import traceback
from contextlib import contextmanager
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from nephele2.rds.rds_config import _RO_URI, _RW_URI
@contextmanager
def db_write():
write_engine = create_engine(_RW_URI)
sess_mkr = sessionmaker(bind=write_engine)
session = sess_mkr()
try:
yield session
session.commit()
except:
print('write except')
print(str(traceback.format_exc()))
session.rollback()
raise
finally:
session.close()
@contextmanager
def db_read():
"""
Creates a read context connection pool to the database.
"""
read_engine = create_engine(_RO_URI)
sess_mkr = sessionmaker(bind=read_engine)
session = sess_mkr()
try:
yield session
except:
print('read except')
print(str(traceback.format_exc()))
session.rollback()
raise
finally:
session.close()
# If you dispose of the engine, you kill the pool.
# _db.engine.dispose()