1212 */
1313class DatabaseManager extends BaseDatabaseManager
1414{
15+ /**
16+ * Default heartbeat SQL (most SQL databases).
17+ *
18+ * @var string
19+ */
20+ private const HEARTBEAT_SQL_DEFAULT = 'SELECT 1 AS ping ' ;
21+
22+ /**
23+ * Oracle heartbeat SQL.
24+ *
25+ * @var string
26+ */
27+ private const HEARTBEAT_SQL_ORACLE = 'SELECT 1 AS ping FROM DUAL ' ;
28+
1529
1630 /**
1731 * @var Pool[]
@@ -41,7 +55,7 @@ public function __construct(...$args)
4155 * @return mixed
4256 * @throws Throwable
4357 */
44- public function connection ($ name = null )
58+ public function connection ($ name = null ): mixed
4559 {
4660 $ name = $ name ?: $ this ->getDefaultConnection ();
4761 [$ database , $ type ] = $ this ->parseConnectionName ($ name );
@@ -59,11 +73,7 @@ public function connection($name = null)
5973 $ this ->closeAndFreeConnection ($ connection );
6074 });
6175 $ pool ->setHeartbeatChecker (function ($ connection ) {
62- if (in_array ($ connection ->getDriverName (), ['mysql ' , 'pgsql ' , 'sqlite ' , 'sqlsrv ' ])) {
63- $ connection ->select ('select 1 ' );
64- } elseif ($ connection ->getDriverName () === 'mongodb ' ) {
65- $ connection ->command (['ping ' => 1 ]);
66- }
76+ $ this ->heartbeat ($ connection );
6777 });
6878 static ::$ pools [$ name ] = $ pool ;
6979 }
@@ -84,6 +94,45 @@ public function connection($name = null)
8494 return $ connection ;
8595 }
8696
97+ /**
98+ * Heartbeat checker for pooled connections.
99+ *
100+ * @param mixed $connection
101+ * @return void
102+ */
103+ private function heartbeat (mixed $ connection ): void
104+ {
105+ $ driver = strtolower ((string )$ connection ->getDriverName ());
106+
107+ // MongoDB (mongodb/laravel-mongodb or jenssegers/mongodb)
108+ if ($ driver === 'mongodb ' ) {
109+ $ connection ->command (['ping ' => 1 ]);
110+ return ;
111+ }
112+
113+ $ sql = $ this ->getHeartbeatSql ($ driver );
114+ if ($ sql !== '' ) {
115+ $ connection ->select ($ sql );
116+ }
117+ }
118+
119+ /**
120+ * Get heartbeat SQL by driver name.
121+ *
122+ * Illuminate\Database supports mysql/mariadb/pgsql/sqlite/sqlsrv by default.
123+ * Oracle (oracle/oci/oci8) may be provided by third-party drivers.
124+ *
125+ * @param string $driver
126+ * @return string
127+ */
128+ private function getHeartbeatSql (string $ driver ): string
129+ {
130+ return match ($ driver ) {
131+ 'oracle ' , 'oci ' , 'oci8 ' => self ::HEARTBEAT_SQL_ORACLE ,
132+ default => self ::HEARTBEAT_SQL_DEFAULT ,
133+ };
134+ }
135+
87136 /**
88137 * Close connection.
89138 *
0 commit comments