PHP oci_connect() stuck / no time-out - php

We are using a Lumen 5.2.x (Laravel) application to get data from a Oracle Database. For that reason we use oci_connect() to connect to the database. (Extra info: we use Oracle instantclient)
For a reason unknown, the application was not responsive and wouldn't return any data. After lots of hours debugging we found out that it got stuck in that very same method: oci_connect(). Apparently the function did not return a 'time-out'-message or anything similar.
Later, it seemed the database moved to another host, which is the reason it couldn't connect. However, we expected a error, instead of a huge amount of waiting.
This is the reason we are trying to force a time-out to be set, until now this has not worked out.
Things we have tried:
Adding this to the connection string: (CONNECT_TIMEOUT=10)(RETRY_COUNT=3) which is completely ignored.
Setting max_execution_time and set_time_limit to 1
Adding a sqlnet.ora with settings:
TCP.CONNECT_TIMEOUT=10
SQLNET.INBOUND_CONNECT_TIMEOUT=10
SQLNET.OUTBOUND_CONNECT_TIMEOUT=10
Everything we have tried failed, does anyone know how to work around this bug? Any help is appreciated!
Edit:
System info:
Windows Server 2012 R2, IIS 8, PHP 5.6

below is laravel package used for oracle, you can try this,
laravel package for oracle

I copied the oracle array from oracle.php to the database.php config file and the issue has gone away.
Contents of my oracle.php file:
return [
'oracle' => [
'driver' => 'oracle',
'tns' => env('DB_TNS', ''),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1521'),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => env('DB_CHARSET', 'AL32UTF8'),
'prefix' => env('DB_PREFIX', ''),
],
];

Unfortunately, oci_connect is already too high-level a function to allow timeout control - OSI model layer 5, if you consider it is there to establish a session for what follows. I suggest you try fsockopen on port 1521, level 4, which 5th argument sets the timeout in seconds. If fsockopen() returns a valid resource, then proceed with oci_connect(), otherwise report error / throw exception.
I checked it today, part of a "preflight assessment" when establishing four Oracle connections with various remote sites. It actually gives up after timeout seconds!

You have tried several approach, which is great. The max_execution_time is a good one. You can register a shutdown function so that you can log the error if any - or do whatever you need.
<?php
function shutdown(){
$error=error_get_last();
if(is_null($error))
echo "No errors"; //or do nothing
else
print_r($a); //or log it properly
}
register_shutdown_function('shutdown');
ini_set('max_execution_time',3 );//max 3 seconds
sleep(5); //just for test it out
In any case, according to the web (for instance : "don't loose your head, move to Linux"), you should try using Linux to run your webserver when working with Oracle connection, if possible).
(I know that there were, at some time, a lot of rage between Linux and Windows People. But if operating system is more suited for some use case, why bother using the other)

From the docs:
"Sometimes Oracle doesn't cleanup shadow processes when accessed from PHP. To avoid that, check your
$ORACLE_HOME/network/admin/tnsnames.ora file in your Oracle Client directory and remove the (SERVER=DEDICATED) token if is set.
To let Oracle delete shadow process on timeouts, add the following line in your
$ORACLE_HOME/network/admin/sqlnet.ora
found in your ORACLE Server directory:
SQLNET.EXPIRE_TIME=n
Where 'n' is the number of minutes to let connection idle befor shutting them out."
Have you tried this?

Related

Laravel - SQLSTATE[HY001] Unable to allocate sufficient memory - MsSQL

I'm making a second connection of my project in laravel with a view in an MsSql database, I configured my .env and config correctly, however this is an error of memory overflow:
$ php artisan tinker
Psy Shell v0.10.5 (PHP 7.3.24-3+ubuntu18.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> use App\Condinvest\BoletoPropCondominio as BPC
>>> BPC::first();
Illuminate\Database\QueryException with message 'SQLSTATE[HY001] Unable to allocate sufficient memory (meudominio.com.br:5000) (severity 8) (SQL: select top 1 * from [View_Boleto_Prop_Condominio])'
>>>
already changed in my php.ini:
memory_limit = 128M
but the error continues.
My models briefly look like this:
BaseView.php
<?php
namespace App\Condinvest;
use Illuminate\Database\Eloquent\Model;
class BaseView extends Model
{
protected $connection = 'condinvest';
}
BoletoPropCondominio.php
<?php
namespace App\Condinvest;
class BoletoPropCondominio extends BaseView
{
protected $table = 'View_Boleto_Prop_Condominio';
protected $fillable = [
'Id_Condo_lan',
...
'Id_titular'
];
}
when I do the same query directly through the command terminal:
SELECT TOP 1 * FROM View_Boleto_Prop_Condominio;
returns my data successfully.
Can anyone tell me what may be happening, or how I can debug better to understand where the error is, please.
EDIT
>>> DB::connection('condinvest')->getConfig()['driver']
=> "sqlsrv"
Since the error is apparently being reported by the database process (not the php process), I would not expect changes to memory limits in php.ini to have any effect.
I found this issue which mentions this specific error when using a deprecated driver with MSSQL Server.
To check which driver Laravel is using, type DB::connection()->getConfig()['driver'] into your Tinker console. If you see sqlsrv then everything is ok here, but if you see dblib then this might be the source of the error. This problem was supposedly fixed in Laravel 5.7 to prefer the supported drivers if more than one is available, but it's also possible that your database.php config file uses the wrong one.
It is also possible that the memory limits of the database server or the system it resides on are actually being exceeded. Being able to run the query in a command prompt without getting the error suggests that this is not the case, but it may still be worth investigating. If the available memory is very low then it's possible that there is not enough to run both php and the database query at the same time. You can check the available system memory by running the free -h command in the terminal, as long as the database process is running on the same machine as your terminal. However, if you are using a shared hosting provider then it is possible that the database is on a separate machine.
If it helps, i encoutered the same issue. What i did in order to fix it was to check my configuration in database.php, if you use sqlserver, make sure you have charset set to utf8 as follows. It was previously set to utf8mb4.
'sqlserver' => [
'driver' => 'sqlsrv',
'host' => env('DB_SQL_HOST'),
'port' => env('DB_SQL_PORT'),
'database' => env('DB_SQL_DATABASE', 'forge'),
'username' => env('DB_SQL_USERNAME', 'forge'),
'password' => env('DB_SQL_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'options' => [
PDO::ATTR_TIMEOUT => 300
]
]

How to connect to PostgreSQL DB with PHP?

As you can already see in the title I want to connect to a PostgreSQL Database I created on a server using PHP and Yii2. Unfortunately I am a total novice when it comes to this kind of job and I have never done it before so I have several questions that I hope some of you can answer.
I am using Yii2 basic and in the config directory there is a db.php file containing the following code:
return [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=my1DB',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];
Is the code actually OK? Instead of localhost I will be using the host IP of course. The db my1DB is actually created and already contains a relation named countries.
I have Yii2 basic installed on my client. Now my question is, do I need to install it on the server as well? It may be a dumb question and I am truly sorry if it is, but I am not sure if I am understanding it all correctly. So, does the db.php file need to be on the server? Or is it OK if it is simply installed on my client?
The db.php would need to be on server side where the PHP is running at, in case the PostgreSQL server is running on the same server that PHP is, you can leave it as localhost and it should work fine, just make sure the credentials and db name exist.

Gii Model Generator generates Database Exception (I use SQL Server)

I have a Yii2 framework connected to an SQL Server 2012 database.
I have already configured the config/db.php file as follows:
return [
'class' => 'yii\db\Connection',
'dsn' => 'sqlsrv:Server=localhost;Database=Evaluators;MultipleActiveResultSets=true',
'username' => '_myUsername_',
'password' => '_myPassword_',
'charset' => 'utf8',
];
I have also installed the necessary extension files in the /ext directory.
I am using SQL Server 2012 instead of MySql.
When I try to Start Gii Model Generator I get the following error :
Database Exception – yii\db\Exception could not find driver
Caused by: PDOException could not find driver
Any ideas what should I change or do?
The issue can be from your php configuration.
In that case, I solved it by doing the following: uncomment extension=php_pdo_mysql.dll in your php.ini. (I am on MySQL instead of SQL server 2012)
More explanations here: https://www.jeffgeerling.com/blog/2018/installing-php-7-and-composer-on-windows-10
Since your using SQL server 2012, I cannot confirm for sure, but you should investigate this: https://www.php.net/manual/en/ref.pdo-sqlsrv.php
Good luck

CakePHP Database connection "Mysql" is missing, or could not be created

There have been several other posts about this, but none of the answers seemed to work for me.
When I navigate to the CakePHP page on my local machine, there is one error:
Cake is NOT able to connect to the database. Database connection
"Mysql" is missing, or could not be created.
When I run this helpful code in my home.ctp, I get the following response:
Error!: SQLSTATE[42000] [1049] Unknown database 'test'
However, my Users/Ben/Sites/myapp/app/Config/database.php looks like this (I set MAMP to look for the document root in Users/Ben/Sites):
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'Ben',
'password' => 'mypass',
'database' => 'CV',
);
}
I have created a mysql user called Ben with password mypass and created a database called CV under that. Moreover, I can't find mention of a test database anywhere. Help?
Try adding the socket:
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
An alternative to unix_socket (especially for OS X people) is to replace localhost with 127.0.0.1
Would be as Follows :
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => '127.0.0.1',
'login' => 'user',
'password' => 'password',
'database' => 'database-name',
'prefix' => '',
'encoding' => 'utf8',
);
Edit php.ini and add:
extension=php_pdo_mysql.dll
Then restart your web server
On Mac, using MAMP as a development platform, for cake the correct solution is using Domingo Casarrubio solution.
Add the unix_socket parameter to your database configurations.
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
This error can also be caused if your connecting database user doesn't have the proper privileges. I believe you only need a minimum of INSERT, SELECT, UPDATE, and DELETE.
Always check username/password and the user privileges first since CakePHP will most likely give a vague database connection error for either.
I noticed that you've had asked this an year ago, and most probably would've solved this by now. However, for those facing the same issues when attempting to install CakePHP on XAMPP, all you have to do is change the 'login' to 'root', i.e. the default login of XAMPP, and leave the 'password' as '', i.e. blank. The complete code in your database.php file should look like this:
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'ckblog',//replace with your own database name
'prefix' => '',
//'encoding' => 'utf8',
);
That's it.
I had the same problem and found out eventually that it was caused by CakePhp not accepting that I used a user with a password, even if that user was created in PHPMyAdmin. I had to use the user 'root' with no password.
I found this out after making the following change to the file /lib/Cake/Error/exceptions.php.
The original line:
protected $_messageTemplate = 'Database connection "%s" is missing, or could not be created.';
is changed into this instead (note the change from single to double quotes):
protected $_messageTemplate = "Database connection \"%s\" is missing, or could not be created:\n %s";
This will give you the reason for the problem so that you may change the cause properly.
I have had this problem since upgrading to OSX Yosemite and inserting following line did the trick for me:
'unix_socket' => '/tmp/mysql.sock'
It can be that mysql PDO support is missing.
as root (or using sudo):
apt-get install php5-mysql
Just to help Ubuntu users out:
I had the same error in my ubuntu 13.10 machine with the newest xampp downlaoded directly from apachefriends. Tried most of the stuff in every post I could find about this error, but not the mac-specific stuff.
In the end, the fix happened to be the same as the elected answer here:
Find the socket that mysqld creates for programs to connect to:
user#host /opt$ find . -name mysql.sock
/opt/lampp/var/mysql/mysql.sock
add it to your cakePHP database configuration file (cakePHP)/app/Config/database.php
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'
To me, this finally resulted in my cake commands being able to be executed without the "Error: Database connection "Mysql" is missing, or could not be created.".
Because, cake bake use unix socket for connecting to database
so that you need add unix_socket for connection string.
You have to confirm location that store mysql.sock in WAS
Example: in my case i'm using xampp on MACOS 10.11
(edit file Config/database.php)
public $default = array(
‘datasource’ => ‘Database/Mysql’,
‘persistent’ => false,
‘host’ => ‘localhost’,
‘login’ => ‘root’,
‘password’ => ‘root’,
‘database’ => ‘cakephp’,
‘encoding’ => ‘utf8’,
‘unix_socket’ => ‘/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock’
);
Finally, It's work for me!
What did it for me in the end was that I had created a table in my database, but there was no data in it.
In order for CakePHP to recognize the MySql connection, there has to be a table with data in it.
You might need to create the table in your php file... Open up phpMyAdmin and check to ensure that they database CV exists.
It's your model. Open that up and there must be the following line
public $useDbConfig = 'local';
This overwrites global config & set it back to local
I tried splicing the code from Example 2 of http://php.net/manual/en/pdo.connections.php into /app/View/Pages/home.ctp. I had to fix the arguments the PDO constructor and change the name of the table in the query. The example 2 code returned the error "Error!: could not find driver". Based on King Jk's answer I was attempting to modify the php.ini when I started to wonder where a php_pdo_mysql.so might live. http://php.net/pdo_mysql showed how it was compiled as part of PHP via the --with-pdo-mysql option to configure. Recompiling fixed my problem. Note I'm working on a Ubuntu 12.10 system with PHP 5.5.9 and Apache Webserver 2.4.6
In my case it was because the database didn't exist. I expected running ./app/Console/cake schema create would create it but it did not. Creating it with create database <database name> in mysql did the trick (although I had already assigned privileges).
I've been struggling with this the whole weekend and finally solved it. Turns out that the php.ini is pointing to a non-existing "extensions dir". Create a phpinfo() file and look at the value of this field:
I noticed that in the mamp php installed folder there is a no-debug-non-zts-20131226 folder, which is different from the value shown in the phpinfo(). What I did was to clone this folder and changed the name to the value of the phpinfo(). Probably you could modify the php.ini file but I didn't want to.
I don't know if you solved your problem, but I'm posting this because my problem was different and google took me here, so I hope to help future googlers having a similiar issue.
Hope this helps.
If you're on Godaddy (or any other shared hosting for that matter), they may be limiting outgoing connections to ports 80 and 443 only.
System configuration:
Fedora 32
php-fpm 7.4.13
mariadb 10.4.17
CAKE 2.10.17
Error message from CAKE:
Database connection "Mysql" is missing, or could not be created.
Enhanced error message using answer at https://stackoverflow.com/a/24722976/5025060
Database connection "Mysql" is missing, or could not be created: Selected driver is not enabled
My problem was no "connector" between PHP and SQL was installed. The solution was:
dnf install php-mysqlnd
This allowed PHP to connect to the database as specified in CAKE's database.php configuration file.

CakePHP: error when trying to use mssql datasource

This is my first time using ms sql with cakephp. Usually I use mysql. I've edited my database.php file:
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mssql',
'persistent' => false,
'host' => 'jura',
'login' => 'sa',
'password' => '********',
'database' => 'clientportal',
'prefix' => '',
);
var $test = array(
'driver' => 'mssql',
'persistent' => false,
'host' => 'jura',
'login' => 'sa',
'password' => '********',
'database' => 'clientportal',
'prefix' => '',
);
}
However when I view the index page i get this error:
PHP SQL Server interface is not installed, cannot continue. For troubleshooting information, see http://php.net/mssql/
Fatal error: Call to undefined function mssql_min_message_severity() in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\clientportaladmin\cake\libs\model\datasources\dbo\dbo_mssql.php on line 123
It's coming from this constructor in the dbo_mssql.php file:
function __construct($config, $autoConnect = true) {
if ($autoConnect) {
if (!function_exists('mssql_min_message_severity')) {
trigger_error(__("PHP SQL Server interface is not installed, cannot continue. For troubleshooting information, see http://php.net/mssql/", true), E_USER_WARNING);
}
mssql_min_message_severity(15);
mssql_min_error_severity(2);
}
return parent::__construct($config, $autoConnect);
}
I have the mssql extension in the php ini file
extension=php_mssql.dll
I've been using mssql databases with PHP on my setup extensively but am wondering if its because the ini file in my php directory are php.ini file is called php.ini-development and php.ini-production but I actually use a php.ini file sitting on the root of C:
Has anyone had to deal with this before? Or anyone know what I need to do? Using W7 btw.
Jonesy
You can`t use extension=php_mssql.dll in php 5.3* versions.
You need to use MSSQL driver for phpMSSQL driver
Open php.ini ,just add this line
extension=php_sqlsrv_53_ts_vc9.dll
use proper extensions files depending upon your version of PHP, whether its Thread safe or not, what was the version of Visual C++ to build your PHP installation.
php_sqlsrv_53_ts_vc9.dll: for instance this driver is for PHP version 5.3 which is thread safe and build using Visual C++ 9, you should also ensure that you have proper php.dll in php installation dir like php5ts.dll for thread safe version of PHP 5.xx
I am using the SQLSRV drivers and the according dbo_sqlsrv.php datasource for CakePHP.
the DATABASE_CONFIG the looks like this
var $default = array(
'driver' => 'sqlsrv',
'connect' => 'sqlsrv',
'persistent' => false,
'host' => 'tcp:NAUFRAGADOS',
'login' => 'sa',
'password' => '********',
'database' => 'geotest',
'prefix' => 'cake_',
);
notice the 'tcp:' in the host definition. This was what I was tripping about till I've got it right
What's the version of CakePHP you're using ?
Another thing, please check about the extension at http://localhost/phpinfo.php.
You should find the extensions that are enabled and seen by the server.
The problem you have is in two parts. Firstly as others have pointed out you're using PHP 5.3 which has no mssql PHP extension. You need to use the Sqlsrv drivers, update the php.ini file and test that you can connect to SQL Server using the sqlsrv drivers.
The second problem you then have is getting CakePHP to use the sqlsrv drivers. Depending on the CakePHP version you have you might need to install the separate sqlsrv database driver. I'm still using CakePHP 1.2 and I believe certainly also in CakePHP 1.3 you need to install the sqlsrv database drivers separately. I don't know if they've included sqlsrv drivers in CakePHP 2 yet.
There is an offical source for CakePHP v1.3+ data sources which includes a sqlsrv data source.
I'm getting the same error on the server using the migrations plugin:
http://cakedc.com/eng/pierre_martin/2010/02/05/cakephp-migrations-plugin-easily-version-and-deploy-whole-applications
it works locally, and the site itself can access the MS SQL DB without problems. It's just the command line cake migration commands that give this error. I'm pretty much stuck with whatever the PHP configuration is on the server. It really would be handy to use this plugin, but I'm not sure where to start looking or tweaking to track this down, but I wonder if it's the same underlying issue?

Categories