使用$ this-> load->数据库(“Db”,TRUE)时,我有未解决的情况;请注意,我在这里使用codeigniter。
以下是这种情况:
我创建一个名为“cdata”的控制器控制器,并从缺席机器中保存设备日志。我正在使用 ADMS型缺席机器。
ADMS计算机如何工作?
当员工进行手指扫描时,缺席机器会发送名为“cdata”的 Http 请求。此“cdata”包含原始数据中缺少的日志,因此我将使用codeigniter中的句柄原始数据来捕获日志缺失,如时间,用户ID,状态签入或签出。因此,当ADMS机器发送请求“cdata”时,我将有一个名为“cdata”的控制器来处理它。
脚本“cdata”如下所示:
class cdata extends CI_Controller {
function __construct(){
parent::__construct();
}
function index(){
$result = "NONE";
$table = $this->input->get('table');
$sn = $this->input->get('SN');
$op = $this->input->get('options');
if ($table == "ATTLOG"){
$data = file_get_contents("php://input");
log_message('error', $data);
echo "OK:1
POST from: ".$sn."";
$result = "OK:1";
$arraydata = explode(chr(9),trim($data));
if (count($arraydata) > 0) {
$userid = $arraydata[0];
$date = $arraydata[1];
$state = $arraydata[2];
$verification = $arraydata[3];
$workcode = $arraydata[5];
$reserved = $arraydata[6];
$insertdata = array("sn_device"=>$sn,
"user_id"=>$userid,
"date_time"=>$date,
"state"=>$state,
"verification"=>$verification,
"work_code"=>$workcode,
"reserved"=>$reserved,
"create_date"=>date('Y-m-d H:i:s'),
"ip_address"=>$_SERVER["REMOTE_ADDR"]);
$result = $this->db->insert("tbl_log",$insertdata);
}
$this->db->trans_start();
$this->db->from('tbl_forward');
$this->db->where(array('sn_device = ' => $this->db->escape_str($sn)));
$row = $this->db->get()->result();
foreach ($row as $rows) {
$db = array(
'dsn' => '',
'hostname' => $rows->hostname,
'port' => $rows->port,
'username' => $rows->username,
'password' => $rows->password,
'database' => $rows->database_name,
'dbdriver' => $rows->db_type,
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE);
$this->sqldb = $this->load->database($db, TRUE);
if ($rows->db_type == "sqlsrv") {
$LogNo = $this->models_database->GenerateNo ('DeviceLogs','LogNo');
}
if(count($arraydata) > 0) {
$userid = $arraydata[0];
$date = $arraydata[1];
$state = $arraydata[2];
$verification = $arraydata[3];
$workcode = $arraydata[5];
$reserved = $arraydata[6];
if ($rows->db_type == "mysqli") {
$insertdata = array("DeviceNo" => $rows->receiver_id,
"UserID" => $userid,
"LogDate" => $date,
"EditedUserID" => $userid,
"EditedDeviceNo" => $rows->receiver_id,
"EditedLogDate" => $date,
"CreatedBy" => "ADMS",
"CreatedOn" => date('Y-m-d H:i:s'),
"LogTypeNo" => $state,
"EditedLogTypeNo" => $state,
"IsEdit" => True,
"IsImport" => 1,
"IsAutoCreated" => 1,
"LogSourceNo" => 1,
"IsDefault" => NULL,
"Remarks" => "None",
"UpdateOn" => date('Y-m-d H:i:s'),
"UpdateBy" => "ADMS",
"FileID" => 1,
"TempID" => 1,
"VerifyMethodNo" => 0,
"BranchNo" => NULL,
"FileName" => NULL,
"CardNo" => NULL);
$result = $this->sqldb->insert("DeviceLogs",$insertdata);
} else if ($rows->db_type == "sqlsrv") {
$insertdata = array("LogNo"=>$LogNo,
"DeviceNo" => $rows->receiver_id,
"UserID" => $userid,
"LogDate" => $date,
"EditedUserID" => $userid,
"EditedDeviceNo" => $rows->receiver_id,
"EditedLogDate" => $date,
"CreatedBy" => "ADMS",
"CreatedOn" => date('Y-m-d H:i:s'),
"LogTypeNo" => $state,
"EditedLogTypeNo" => $state,
"IsEdit" => True,
"LogSourceNo" => 1,
"IsDefault" => 1,
"Remarks" => "None",
"UpdateOn" => date('Y-m-d H:i:s'),
"UpdateBy" => "ADMS",
"CardNo" => NULL);
$result = $this->sqldb->insert("DeviceLogs",$insertdata);
}
}
}
$this->db->trans_complete();
}
}
}
该脚本用于将缺席日志保存到本地数据库,然后将缺勤日志转发到另一个数据库,这取决于主机名,用户名,密码和数据库名称。
问题在于:
连接稳定或良好时。控制器将执行手指扫描的速度并不重要,数据保存到本地并保存到另一个数据库而没有错误。
但是当我连接不稳定或连接不良时(我在这两个ip中创建一个高数据包,一个来自机器和一个计算机)我们可以调用连接LAGGER,当我重复做一次手指扫描,在这行中拨打 cdata 两次时出现错误:
$ this-> sqldb = $ this-> load->数据库($ db,TRUE);
错误是:
消息:mysqli :: real_connect():( HY000 / 2002):一个连接 尝试失败,因为关联方没有正确回应 一段时间后,或建立连接失败,因为 连接主机无法响应。\ r \ n
看起来像控制器cdata被调用时我在连接不稳定时无法调用load database两次。
我该怎么办?这里有什么问题?
根据我的观点,也许第一个“cdata”加载数据库 $ this-> sqldb = $ this-> load->数据库($ db,TRUE) ; 仍在加载,然后另一个名为“cdata”加载数据库 $ this-> sqldb = $ this-> load->数据库($ db,TRUE); 也被调用。
谢谢
Dennis Liu