-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.py
50 lines (39 loc) · 1.69 KB
/
database.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
46
47
48
49
50
import psycopg2
from psycopg2 import pool
def connect():
return psycopg2.connect(user='learnadmin', password = 'Hi5Michael', database='SnapshotDev', host='snapshotdev.cpeg0owf2zfo.us-west-2.rds.amazonaws.com')
class Database():
__connection_pool = None
@staticmethod
def initialize(cls, **kwargs):
Database.connection_pool = pool.SimpleConnectionPool(1,
10,
# the below arguments can be passed in another function
database="",
user="",
password="",
host="")
@classmethod
def get_connection(cls):
return cls.connection.getconn()
@classmethod
def return_connection(cls, connection):
return Database.connection_pool.putconn(connection)
@classmethod
def close_all_connection(cls):
return Database.connection_pool.closeall()
class CursorFromConnectionFromPool:
def __init__(self):
self.connection = None
self.cursor = None
def __enter__(self):
self.connection = Database.get_connection()
self.cursor = self.connection.cursor()
return self.cursor
def __exit__(self, exception_type, exception_value, exception_traceback):
if exception_value is not None:
self.connection.rollback()
else:
self.cursor.close()
self.connection.commit()
Database.return_connection(self.connection)