2 Replies - 7764 Views - Last Post: 08 July 2011 - 06:04 AM Rate Topic: -----

#1 satis   User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

PDO persistent connections and warnings

Posted 08 July 2011 - 05:30 AM

I'm using a PDO wrapper class to handle my DB transactions. I instantiate it once and then pass references to any class that requires a db connection. I've been using the PDO::ATTR_PERSISTENT attribute like so:

	public function __construct(){
		$this->error = Array();
		$this->lastQueryFailed = false;
		try{
			$this->db = new PDO('mysql:host=' .DBSERVER .';dbname=' .DB, DBUSERNAME, DBPASSWORD, array(PDO::ATTR_PERSISTENT => true));
			$this->state = true;
		}
		catch(PDOException $e){
			$this->error[] = $e;
			$this->state = false;
		}
	}



The problem is that I get warnings occasionally telling me that "Warning: PDO::__construct(): MySQL server has gone away in..." Following the warning, I still have an active DB connection. It appears my persistent connection was dropped and PDO reconnected while throwing this warning.

I can't have warnings like this pop up in my app, since it includes AJAX portions and throwing a warning like that in an AJAX request would malform the XML. Plus it looks horrible.

So, should I:
1. Suppress warnings on the whole line with @ (I hate doing that since I might miss something important).
2. Stop using persistent connections (performance hit?).
3. Dial down warning levels in php.ini (even worse than option 1 imo).
4. Or some fourth option I haven't considered?

Is This A Good Question/Topic? 0
  • +

Replies To: PDO persistent connections and warnings

#2 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: PDO persistent connections and warnings

Posted 08 July 2011 - 05:47 AM

Persistent connections are connections to your database that doesn't close after your script exits. Therefore if page1 initiated a persistent connection then page2 will use the exact same connection, if it's connecting to the same database that is.

You should never suppress warning or errors because they're there to help you. Handle them.

Should you stop using persistent connections? If your server or host is capable of handling a good amount of simultaneous connections then yes.

To get rid of this error you can either change MySQL's timeout value if you have access to it, catch the errors and continue like nothing happened (not recommended) or stop using persistent connections.

This post has been edited by codeprada: 08 July 2011 - 05:47 AM

Was This Post Helpful? 1
  • +
  • -

#3 satis   User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Re: PDO persistent connections and warnings

Posted 08 July 2011 - 06:04 AM

Cool, that's got me on the right track. I was able to set an error handler around the connection attempt so I can catch the warning and handle it silently. If something other than my warning pops up, I can do something else.

With some quick googling, I was able to figure out how to use set_error_handler to call a method of the current object. Thanks for the advice.

	public function __construct(){
		$this->error = Array();
		$this->lastQueryFailed = false;
		try{
			set_error_handler(array($this, 'errHandler'));
			$this->db = new PDO('mysql:host=' .DBSERVER .';dbname=' .DB, DBUSERNAME, DBPASSWORD, array(PDO::ATTR_PERSISTENT => true));
			restore_error_handler();
			$this->state = true;
		}
		catch(PDOException $e){
			$this->error[] = $e;
			$this->state = false;
		}
	}
	public function errHandler($errno, $errstr){
		//do stuff
	}

This post has been edited by satis: 08 July 2011 - 06:05 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1