(self, with_db: bool)
| 109 | self._pool_init_lock = asyncio.Lock() |
| 110 | |
| 111 | async def create_connection(self, with_db: bool) -> None: |
| 112 | if charset_by_name(self.charset) is None: |
| 113 | raise DBConnectionError(f"Unknown charset {self.charset}") |
| 114 | self._template = { |
| 115 | "host": self.host, |
| 116 | "port": self.port, |
| 117 | "user": self.user, |
| 118 | "db": self.database if with_db else None, |
| 119 | "autocommit": True, |
| 120 | "charset": self.charset, |
| 121 | "minsize": self.pool_minsize, |
| 122 | "maxsize": self.pool_maxsize, |
| 123 | **self.extra, |
| 124 | } |
| 125 | try: |
| 126 | self._pool = await mysql.create_pool(password=self.password, **self._template) |
| 127 | |
| 128 | if isinstance(self._pool, mysql.Pool): |
| 129 | async with self.acquire_connection() as connection: |
| 130 | async with connection.cursor() as cursor: |
| 131 | if self.storage_engine: |
| 132 | await cursor.execute( |
| 133 | f"SET default_storage_engine='{self.storage_engine}';" |
| 134 | ) |
| 135 | if self.storage_engine.lower() != "innodb": # pragma: nobranch |
| 136 | self.capabilities.__dict__["supports_transactions"] = False |
| 137 | # Only set session timezone when use_tz=True |
| 138 | if get_use_tz(): |
| 139 | from datetime import datetime as _datetime |
| 140 | |
| 141 | offset = _datetime.now(tz=get_default_timezone()).utcoffset() |
| 142 | total_seconds = int(offset.total_seconds()) # type: ignore |
| 143 | hours = total_seconds // 3600 |
| 144 | minutes = abs(total_seconds) % 3600 // 60 |
| 145 | tz = f"{hours:+d}:{minutes:02d}" |
| 146 | await cursor.execute(f"SET time_zone='{tz}';") |
| 147 | await self._post_connect() |
| 148 | self.log.debug("Created connection %s pool with params: %s", self._pool, self._template) |
| 149 | except errors.OperationalError: |
| 150 | raise DBConnectionError(f"Can't connect to MySQL server: {self._template}") |
| 151 | |
| 152 | async def _expire_connections(self) -> None: |
| 153 | if self._pool: # pragma: nobranch |
no test coverage detected