.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.sqlitedb.DBSession Class Reference

Public Member Functions

sqlite3.Connection get_connect (cls, SQLiteAppl app)
 
 __init__ (self, SQLiteAppl app)
 
sqlite3.Connection conn (self)
 
 __del__ (self)
 

Public Attributes

 uuid = uuid.uuid4()
 
 app = app
 

Protected Attributes

 _conn = None
 

Detailed Description

A *thead-local* DB session

Definition at line 39 of file sqlitedb.py.

Constructor & Destructor Documentation

◆ __init__()

searx.sqlitedb.DBSession.__init__ ( self,
SQLiteAppl app )

Definition at line 55 of file sqlitedb.py.

55 def __init__(self, app: SQLiteAppl):
56 self.uuid = uuid.uuid4()
57 self.app = app
58 self._conn = None
59 # self.__del__ will be called, when thread ends
60 if getattr(THREAD_LOCAL, "DBSession_map", None) is None:
61 THREAD_LOCAL.DBSession_map = {}
62 THREAD_LOCAL.DBSession_map[self.app.db_url] = self
63

◆ __del__()

searx.sqlitedb.DBSession.__del__ ( self)

Definition at line 75 of file sqlitedb.py.

75 def __del__(self):
76 try:
77 if self._conn is not None:
78 # HINT: Don't use Python's logging facility in a destructor, it
79 # will produce error reports when python aborts the process or
80 # thread, because at this point objects that the logging module
81 # needs, do not exist anymore.
82 # msg = f"DBSession: close [{self.uuid}] {self.app.__class__.__name__}({self.app.db_url})"
83 # logger.debug(msg)
84 self._conn.close()
85 except Exception: # pylint: disable=broad-exception-caught
86 pass
87
88

References _conn.

Member Function Documentation

◆ conn()

sqlite3.Connection searx.sqlitedb.DBSession.conn ( self)

Definition at line 65 of file sqlitedb.py.

65 def conn(self) -> sqlite3.Connection:
66 msg = f"[{threading.current_thread().ident}] DBSession: " f"{self.app.__class__.__name__}({self.app.db_url})"
67 if self._conn is None:
68 self._conn = self.app.connect()
69 logger.debug("%s --> created new connection", msg)
70 # else:
71 # logger.debug("%s --> already connected", msg)
72
73 return self._conn
74

References _conn, and app.

◆ get_connect()

sqlite3.Connection searx.sqlitedb.DBSession.get_connect ( cls,
SQLiteAppl app )
Returns a thread local DB connection.  The connection is only
established once per thread.

Definition at line 43 of file sqlitedb.py.

43 def get_connect(cls, app: SQLiteAppl) -> sqlite3.Connection:
44 """Returns a thread local DB connection. The connection is only
45 established once per thread.
46 """
47 if getattr(THREAD_LOCAL, "DBSession_map", None) is None:
48 THREAD_LOCAL.DBSession_map = {}
49
50 session = THREAD_LOCAL.DBSession_map.get(app.db_url)
51 if session is None:
52 session = cls(app)
53 return session.conn
54

Member Data Documentation

◆ _conn

searx.sqlitedb.DBSession._conn = None
protected

Definition at line 58 of file sqlitedb.py.

Referenced by __del__(), and conn().

◆ app

searx.sqlitedb.DBSession.app = app

Definition at line 57 of file sqlitedb.py.

Referenced by conn().

◆ uuid

searx.sqlitedb.DBSession.uuid = uuid.uuid4()

Definition at line 56 of file sqlitedb.py.


The documentation for this class was generated from the following file: