.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.sqlitedb.DBSession Class Reference
Collaboration diagram for searx.sqlitedb.DBSession:

Public Member Functions

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

Public Attributes

uuid.UUID uuid = uuid.uuid4()
SQLiteAppl app = app

Protected Attributes

sqlite3.Connection|None _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 56 of file sqlitedb.py.

56 def __init__(self, app: "SQLiteAppl"):
57 self.uuid: uuid.UUID = uuid.uuid4()
58 self.app: SQLiteAppl = app
59 self._conn: sqlite3.Connection | None = None
60 # self.__del__ will be called, when thread ends
61 if getattr(THREAD_LOCAL, "DBSession_map", None) is None:
62 url_to_session: dict[str, DBSession] = {}
63 THREAD_LOCAL.DBSession_map = url_to_session
64 THREAD_LOCAL.DBSession_map[self.app.db_url] = self
65

◆ __del__()

searx.sqlitedb.DBSession.__del__ ( self)

Definition at line 77 of file sqlitedb.py.

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

References _conn.

Member Function Documentation

◆ conn()

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

Definition at line 67 of file sqlitedb.py.

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

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 url_to_session: dict[str, DBSession] = {}
49 THREAD_LOCAL.DBSession_map = url_to_session
50
51 session: DBSession | None = THREAD_LOCAL.DBSession_map.get(app.db_url)
52 if session is None:
53 session = cls(app)
54 return session.conn
55

Member Data Documentation

◆ _conn

sqlite3.Connection | None searx.sqlitedb.DBSession._conn = None
protected

Definition at line 59 of file sqlitedb.py.

Referenced by __del__(), and conn().

◆ app

SQLiteAppl searx.sqlitedb.DBSession.app = app

Definition at line 58 of file sqlitedb.py.

Referenced by conn().

◆ uuid

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

Definition at line 57 of file sqlitedb.py.


The documentation for this class was generated from the following file:
  • /home/andrew/Documents/code/public/searxng/searx/sqlitedb.py