Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse existing MySQL connection #245

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions auth/admin/classes/common/DBService.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php
/*
**************************************************************************************************************************
** CORAL Usage Statistics Reporting Module v. 1.0
**
**
** Copyright (c) 2010 University of Notre Dame
**
** This file is part of CORAL.
Expand All @@ -17,16 +16,18 @@
*/



class DBService extends Object {

protected $db;
protected $config;
protected $error;
private static $currDBH;//used to hold current DB connection for reuse.

protected function init(NamedArguments $arguments) {
parent::init($arguments);
$this->config = new Configuration;
$this->connect();
$this->connect();
}

protected function dealloc() {
Expand All @@ -35,48 +36,48 @@ protected function dealloc() {
}

protected function checkForError() {
if ($this->error = mysqli_error($this->db)) {
$this->error = mysqli_error($this->db);
if ($this->error) {
throw new Exception(_("There was a problem with the database: ") . $this->error);
}
}

protected function connect() {
if(empty(self::$currDBH)){
$host = $this->config->database->host;
$username = $this->config->database->username;
$password = $this->config->database->password;
$databaseName = $this->config->database->name;
$this->db = mysqli_connect($host, $username, $password, $databaseName);
$this->checkForError();
$this->db->set_charset('utf8');
$this->checkForError();
mysqli_set_charset($this->db, 'utf8');
self::$currDBH=$this->db;
} else {
$this->db=self::$currDBH;
}
}

protected function disconnect() {
//$this->db->close();
}

public function changeDB($databaseName) {
//$databaseName='coral_reporting_pprd';
$this->db->select_db($databaseName);
$this->checkForError();
//mysqli_close($this->db);
}

public function escapeString($value) {
return $this->db->escape_string($value);
return $this->db->real_escape_string($value);
}

public function getDatabase() {
return $this->db;
}

public function query($sql) {
public function query($sql) {
$result = $this->db->query($sql);
$this->checkForError();
return $result;
}
}

public function processQuery($sql, $type = NULL) {
//echo $sql. "<br />";
$result = $this->db->query($sql);
//echo $sql. "<br />";
$result = mysqli_query($this->db, $sql);
$this->checkForError();
$data = array();

Expand All @@ -85,16 +86,16 @@ public function processQuery($sql, $type = NULL) {
if ($type == 'assoc') {
$resultType = MYSQLI_ASSOC;
}
while ($row = $result->fetch_array($resultType)) {
if ($this->db->affected_rows > 1) {
while ($row = mysqli_fetch_array($result, $resultType)) {
if (mysqli_affected_rows($this->db) > 1) {
array_push($data, $row);
} else {
$data = $row;
}
}
$result->free();
mysqli_free_result($result);
} else if ($result) {
$data = $this->db->insert_id;
$data = mysqli_insert_id($this->db);
}

return $data;
Expand Down
42 changes: 19 additions & 23 deletions licensing/admin/classes/common/DBService.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php
/*
**************************************************************************************************************************
** CORAL Licensing Module v. 1.0
**
**
** Copyright (c) 2010 University of Notre Dame
**
** This file is part of CORAL.
Expand All @@ -23,11 +22,12 @@ class DBService extends Object {
protected $db;
protected $config;
protected $error;
private static $currDBH;//used to hold current DB connection for reuse.

protected function init(NamedArguments $arguments) {
parent::init($arguments);
$this->config = new Configuration;
$this->connect();
$this->connect();
}

protected function dealloc() {
Expand All @@ -36,40 +36,48 @@ protected function dealloc() {
}

protected function checkForError() {
if ($this->error = mysqli_error($this->db)) {
$this->error = mysqli_error($this->db);
if ($this->error) {
throw new Exception(_("There was a problem with the database: ") . $this->error);
}
}

protected function connect() {
if(empty(self::$currDBH)){
$host = $this->config->database->host;
$username = $this->config->database->username;
$password = $this->config->database->password;
$databaseName = $this->config->database->name;
$this->db = mysqli_connect($host, $username, $password, $databaseName);
$this->checkForError();
$this->checkForError();
mysqli_set_charset($this->db, 'utf8');
self::$currDBH=$this->db;
} else {
$this->db=self::$currDBH;
}
}

protected function disconnect() {
//mysqli_close($this->db);
}

public function escapeString($value) {
return mysqli_real_escape_string($this->db, $value);
return $this->db->real_escape_string($value);
}

public function getDatabase() {
return $this->db;
}

public function query($sql) {
$result = $this->db->query($sql);
$this->checkForError();
return $result;
}

public function processQuery($sql, $type = NULL) {
//echo $sql. "<br />";
$query_start = microtime(true);
$result = mysqli_query($this->db, $sql);
$query_end = microtime(true);
$this->log($sql, $query_end - $query_start);

$result = mysqli_query($this->db, $sql);
$this->checkForError();
$data = array();

Expand Down Expand Up @@ -154,18 +162,6 @@ private static function refValues($arr){
}
return $refs;
}

public function log($sql, $query_time) {
$threshold = $this->config->database->logQueryThreshold;
if ($this->config->database->logQueries == "Y" && (!$threshold || $query_time >= $threshold)) {
$util = new Utility();
$log_path = $util->getModulePath()."/log";
$log_file = $log_path."/database.log";
$log_string = date("c")."\n".$_SERVER['REQUEST_URI']."\n".$sql."\nQuery completed in ".sprintf("%.3f", round($query_time, 3))." seconds";
error_log($log_string."\n\n", 3, $log_file);
}
}

}

?>
64 changes: 28 additions & 36 deletions management/admin/classes/common/DBService.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php
/*
**************************************************************************************************************************
** CORAL Management Module v. 1.0
**
**
** Copyright (c) 2010 University of Notre Dame
**
** This file is part of CORAL.
Expand All @@ -23,11 +22,12 @@ class DBService extends Object {
protected $db;
protected $config;
protected $error;
private static $currDBH;//used to hold current DB connection for reuse.

protected function init(NamedArguments $arguments) {
parent::init($arguments);
$this->config = new Configuration;
$this->connect();
$this->connect();
}

protected function dealloc() {
Expand All @@ -36,44 +36,48 @@ protected function dealloc() {
}

protected function checkForError() {
if ($this->error = $this->db->error) {
$this->error = mysqli_error($this->db);
if ($this->error) {
throw new Exception(_("There was a problem with the database: ") . $this->error);
}
}

protected function connect() {
if(empty(self::$currDBH)){
$host = $this->config->database->host;
$username = $this->config->database->username;
$password = $this->config->database->password;
$this->db = new mysqli($host, $username, $password);
$this->checkForError();

$databaseName = $this->config->database->name;
$this->db->select_db($databaseName);
$this->db->set_charset('utf8');
$this->checkForError();
$this->db = mysqli_connect($host, $username, $password, $databaseName);
$this->checkForError();
mysqli_set_charset($this->db, 'utf8');
self::$currDBH=$this->db;
} else {
$this->db=self::$currDBH;
}
}

protected function disconnect() {
//mysqli_close($this->db);
}

public function escapeString($value) {
return $this->db->real_escape_string($value);
return $this->db->real_escape_string($value);
}

public function query($sql) {
$result = $this->db->query($sql);
$this->checkForError();
return $result;
public function getDatabase() {
return $this->db;
}

public function processQuery($sql, $type = NULL) {
$query_start = microtime(true);
$result = $this->db->query($sql);
$query_end = microtime(true);
$this->log($sql, $query_end - $query_start);
public function query($sql) {
$result = $this->db->query($sql);
$this->checkForError();
return $result;
}

public function processQuery($sql, $type = NULL) {
//echo $sql. "<br />";
$result = mysqli_query($this->db, $sql);
$this->checkForError();
$data = array();

Expand All @@ -82,16 +86,16 @@ public function processQuery($sql, $type = NULL) {
if ($type == 'assoc') {
$resultType = MYSQLI_ASSOC;
}
while ($row = $result->fetch_array($resultType)) {
if ($this->db->affected_rows > 1) {
while ($row = mysqli_fetch_array($result, $resultType)) {
if (mysqli_affected_rows($this->db) > 1) {
array_push($data, $row);
} else {
$data = $row;
}
}
$result->free();
mysqli_free_result($result);
} else if ($result) {
$data = $this->db->insert_id;
$data = mysqli_insert_id($this->db);
}

return $data;
Expand Down Expand Up @@ -158,18 +162,6 @@ private static function refValues($arr){
}
return $refs;
}

public function log($sql, $query_time) {
$threshold = $this->config->database->logQueryThreshold;
if ($this->config->database->logQueries == "Y" && (!$threshold || $query_time >= $threshold)) {
$util = new Utility();
$log_path = $util->getModulePath()."/log";
$log_file = $log_path."/database.log";
$log_string = date("c")."\n".$_SERVER['REQUEST_URI']."\n".$sql."\nQuery completed in ".sprintf("%.3f", round($query_time, 3))." seconds";
error_log($log_string."\n\n", 3, $log_file);
}
}

}

?>
12 changes: 9 additions & 3 deletions organizations/admin/classes/common/DBService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class DBService extends Object {
protected $db;
protected $config;
protected $error;
private static $currDBH;//used to hold current DB connection for reuse.

protected function init(NamedArguments $arguments) {
parent::init($arguments);
Expand All @@ -41,16 +42,21 @@ protected function checkForError() {
}

protected function connect() {
if(empty(self::$currDBH)){
$host = $this->config->database->host;
$username = $this->config->database->username;
$password = $this->config->database->password;
$this->db = new mysqli($host, $username, $password);
$this->checkForError();
$this->db->set_charset('utf8');

$this->checkForError();
$this->db->set_charset('utf8');
$databaseName = $this->config->database->name;
$this->db->select_db($databaseName);
$this->checkForError();
mysqli_set_charset($this->db, 'utf8');
self::$currDBH=$this->db;
} else {
$this->db=self::$currDBH;
}
}

protected function disconnect() {
Expand Down
Loading