I am connecting to neo4j the normal way and i can run queries no problem.
During testing, i wrote a query that should fail (due to uniqueness constraint), the query does fail as expected and i catch the exception.
The problem is when i try to execute the next query in the queue, it just hangs (longer than timeout).
I don't suppose that is normal behavior.
try{
$result = $neo->run ($query);
}
catch (Exception $e) {
// handle it
}
// all good so far
// now we attempt:
try{
$result = $neo->run ($next_query);
}
catch (Exception $e) {
// handle it
}
// hangs longer than timeout
if i remove the failed query from the queue, everything completes
So it seems that the exception thrown by the php-client breaks the connection to neo4j.
If i modify the code to the following, it all works fine.
try{
$result = $neo->run ($query);
}
catch (Exception $e) {
// handle it
connect_to_neo()
}
// all good so far
try{
$result = $neo->run ($next_query);
}
catch (Exception $e) {
// handle it
}
// all good, $next_query gets executed
I do not think that an exception that breaks the connection is desired behavior. Will raise the issue on github.
Related
I have code like this
try {
$pdo->beginTransaction();
$stmt = $pdo->prepare($query1);
$stmt->execute($x);
} catch (Exception $e) {
$pdo->rollBack();
throw->$e;
}
if (condition) {
exit();
}
$x['column1'] = 'string1';
$x['column2'] = 'string2';
$x['column3'] = 'string3';
try {
$stmt = $pdo->prepare($query2);
$stmt->execute($x);
$pdo->commit();
} catch (Exception $e) {
$pdo->rollBack();
throw->$e;
}
If the if condition succeeded and the code did exit()
Is everything related to the $pdo is safe too or do I add before exit() a $pdo->rollBack();?
Technically you don't.
PHP will close the database connection on exit.
Database will roll back all active transactions on close.
However, it is quite unlikely that such a case will ever happen in your code because right now it is wrong. You have to wrap the entire transaction in a try catch, not just database operations. Otherwise, if an exception will be thrown in your "condition" part, it will break a transaction but won't be caught.
Besides, using exit is a bad practice by itself and amidst a transaction a tenfold.
But if you really really need that (in reality you don't) then do something like
try {
$pdo->beginTransaction();
$stmt = $pdo->prepare($query1);
$stmt->execute($x);
if (condition) {
throw new Exception("Stopped on condition");
}
$x['column1'] = 'string1';
$x['column2'] = 'string2';
$x['column3'] = 'string3';
$stmt = $pdo->prepare($query2);
$stmt->execute($x);
$pdo->commit();
} catch (Throwable $e) {
$pdo->rollBack();
throw->$e;
}
From the docs:
When the script ends or when a connection is about to be closed, if you have an outstanding transaction, PDO will automatically roll it back.
https://www.php.net/manual/en/pdo.transactions.php
In my opinion, it's better to run rollback to make sure to other devs that it's not code garbage, someon think about it.
I am reading some extra information from Redis and the desired behaviour is to skip connection error silently, if any:
try {
$r = new Redis();
$r->connect("127.0.0.1", "6379");
} catch (Error $e) {
;
} catch (Throwable $e) {
;
}
If Redis fails, monitoring system will show alert to right people to fix it.
Unfortunatelly the code above still causes Yii to fail and produce HTTP 500:
2018/04/09 12:28:04 [error] [php] Redis::connect(): connect() failed: Connection refused
What am I doing wrong?
You need to catch the Exception thrown...
try {
$r = new Redis();
$r->connect("127.0.0.1", "6379");
} catch (\Exception $e) {
;
}
I think you can catch the very specific exception of Predis\Connection\ConnectionException if you need to.
I have a PHP psql query. I am doing try catch to prevent Duplicates. When I ran the script I can see it's inserting something into my DB. But when I check my DB it's empty.
foreach($data as $n){
$query = $psql->pdo_prepared("INSERT INTO students(id,email,address,phone)VALUES".myFunction($array);
}
and I have a PHP class to handle the exception:
public function pdo_prepared($query,array){
try{
// some logic
}
catch(EXCEPTION $e){
//empty
}
}
The reason why I am doing try catch is to catch duplicates and ignore it. If I throw an exception in my catch block my insert statement won't execute because my current data contains duplicates.
I am using RabbitMQ with PHP. While consuming messages from RabbitMQ, we have magic in this piece of code:
while (count($callbacks)) {
try {
$conn->wait();
} catch (Exception $e) {
//Log the message
}
}
This is working as infinite loop to receive messages as expected but what happening is if we lost connection to the RabbitMQ/RabbitMQ not up its going to catch block and returns nothing and printing bulk log messages. Is there any better way we can check for connection for RabbitMQ and stops the script? How can we achieve this? Any suggestions?
while (count($callbacks)) {
try {
$conn->wait();
} catch (Exception $e) {
//Log the message
break;
}
}
I am trying to implement transactions to Kohana but it seems to be not so easy as in Spring/Java.
So far I found this code to try but I don't know how to replace the part (no errors)
DB::query('START TRANSACTION');
// sql queries with query builder..
if (no errors)
DB::query('COMMIT');
else
DB::query('ROLLBACK');
How do I make the if clause?
Normally transactions are handled as such:
DB::query('START TRANSACTION');
try {
//do something
DB::query('COMMIT');
} catch (Exception $e) {
DB::query('ROLLBACK');
}
What this means is if everything succeeds within the try block, great. If any part of it fails then it won't reach the commit and will jump to the catch block, which contains the rollback. You can add more error handling within the catch if you wish, even throw a new exception of your own or throw the same exception you caught.
Just wrap everything in a try/catch block:
DB::query('START TRANSACTION');
try {
// sql queries with query builder..
DB::query('COMMIT');
} catch (Database_Exception $e) {
DB::query('ROLLBACK');
}
DB errors are converted to exceptions:
DB::query('START TRANSACTION');
try {
// sql queries with query builder..
DB::query('COMMIT');
}
catch($e)
{
$this->template->body = new View('db_error');
$this->template->body->error = 'An error occurred ...';
DB::query('ROLLBACK');
}
If you're using Kohana 3:
$db = Database::instance();
$db->begin();
try
{
// Do your queries here
$db->commit();
}
catch (Database_Exception $e)
{
$db->rollback();
}