Since “lost connection” is a highly unusual occurrence, yet one which seems to happen to you regularly-enough to mention, I suggest that you need to get to the bottom of this. Perhaps, for instance, you are trying to send too much BLOB-data at one time and you are overflowing some buffer and in response MySQL is cutting you off. Like I said, you need to dig deep enough to find out why this is happening to you and to your program, before you start to try to develop an appropriate solution. Oh, you might manage to find “a Band-Aid® That Seems To Work,” but it is a faux quest. Forsooth, if you have not unearthed the root cause, your headaches and heartburn will not end.