|
@@ -27,7 +27,7 @@ class FyySqlServerService extends Service
|
|
|
public $sUserID = "0001";
|
|
|
public $sPassword = "";
|
|
|
|
|
|
- public function __construct($user_id = [], $is_db2 = false)
|
|
|
+ public function __construct($user_id = [])
|
|
|
{
|
|
|
try {
|
|
|
//用户信息校验
|
|
@@ -60,53 +60,74 @@ class FyySqlServerService extends Service
|
|
|
$this->sPassword = $emp->sqlserver_password ?? '';
|
|
|
$this->url = $this->host_api . "/U8Sys/U8API";
|
|
|
|
|
|
- if(!$this->db) {
|
|
|
- $config = [
|
|
|
- 'driver' => 'sqlsrv',
|
|
|
- 'host' => $this->host,
|
|
|
- 'port' => $this->port,
|
|
|
- 'database' => $this->database,
|
|
|
- 'username' => env('SQLSRV_USERNAME'),
|
|
|
- 'password' => env('SQLSRV_PASSWORD'),
|
|
|
- ];
|
|
|
+ $this->createConnection();
|
|
|
+ } catch (\Throwable $e) {
|
|
|
+ $this->error = $e->getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // 进行数据库连接
|
|
|
- Config::set('database.connections.sqlsrvs', $config);
|
|
|
+ private function createConnection()
|
|
|
+ {
|
|
|
+ // 主数据库连接
|
|
|
+ $mainConnName = 'sqlsrv_main_' . uniqid();
|
|
|
+ $mainConfig = [
|
|
|
+ 'driver' => 'sqlsrv',
|
|
|
+ 'host' => $this->host,
|
|
|
+ 'port' => $this->port,
|
|
|
+ 'database' => $this->database,
|
|
|
+ 'username' => env('SQLSRV_USERNAME'),
|
|
|
+ 'password' => env('SQLSRV_PASSWORD'),
|
|
|
+ 'options' => [
|
|
|
+// \PDO::ATTR_TIMEOUT => 30, // 查询超时30秒
|
|
|
+ \PDO::SQLSRV_ATTR_QUERY_TIMEOUT => 30, // SQL Server专用超时
|
|
|
+ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
|
|
+// \PDO::ATTR_PERSISTENT => false, // 重要:禁用持久连接
|
|
|
+ ],
|
|
|
+ ];
|
|
|
|
|
|
- $pdo = DB::connection('sqlsrvs')->getPdo();
|
|
|
- if ($pdo instanceof \PDO) {
|
|
|
- // 连接成功的逻辑代码
|
|
|
- $this->db = DB::connection('sqlsrvs');
|
|
|
- } else {
|
|
|
- $this->error = '连接失败!';
|
|
|
- return;
|
|
|
- }
|
|
|
+ Config::set("database.connections.{$mainConnName}", $mainConfig);
|
|
|
+ $this->db = DB::connection($mainConnName);
|
|
|
+
|
|
|
+ // 测试连接有效性
|
|
|
+ $this->validateConnection($this->db);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function validateConnection($connection)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $pdo = $connection->getPdo();
|
|
|
+ $stmt = $pdo->prepare("SELECT 1 AS connection_test");
|
|
|
+ $stmt->execute();
|
|
|
+ $result = $stmt->fetch(\PDO::FETCH_ASSOC);
|
|
|
+
|
|
|
+ if (empty($result) || $result['connection_test'] != 1) {
|
|
|
+ $this->error = "数据库连接失败";
|
|
|
}
|
|
|
+ } catch (\Throwable $e) {
|
|
|
+ $this->error = "数据库连接验证失败: " . $e->getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if($is_db2){
|
|
|
- $config = [
|
|
|
- 'driver' => 'sqlsrv',
|
|
|
- 'host' => $this->host,
|
|
|
- 'port' => $this->port,
|
|
|
- 'database' => 'UFSystem',
|
|
|
- 'username' => env('SQLSRV_USERNAME'),
|
|
|
- 'password' => env('SQLSRV_PASSWORD'),
|
|
|
- ];
|
|
|
+ public function __destruct()
|
|
|
+ {
|
|
|
+ // 主动关闭连接
|
|
|
+ $this->safeDisconnect($this->db);
|
|
|
+ }
|
|
|
|
|
|
- // 进行数据库连接
|
|
|
- Config::set('database.connections.sqlsrvs', $config);
|
|
|
+ private function safeDisconnect(&$connection)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ if ($connection instanceof \Illuminate\Database\Connection) {
|
|
|
+ // 物理断开连接
|
|
|
+ $connection->disconnect();
|
|
|
|
|
|
- $pdo = DB::connection('sqlsrvs')->getPdo();
|
|
|
- if ($pdo instanceof \PDO) {
|
|
|
- // 连接成功的逻辑代码
|
|
|
- $this->db2 = DB::connection('sqlsrvs');
|
|
|
- } else {
|
|
|
- $this->error = '连接失败!';
|
|
|
- return;
|
|
|
- }
|
|
|
+ // 清除连接引用
|
|
|
+ $connection = null;
|
|
|
+ Log::channel('sendData')->info('动作', ["param" => "执行了析构"]);
|
|
|
}
|
|
|
} catch (\Throwable $e) {
|
|
|
- $this->error = $e->getMessage();
|
|
|
+ // 静默处理断开错误
|
|
|
+ Log::channel('sendData')->info('错误', ["param" => $e->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|