in reply to log to DB connection issue & optimization

Normally such a thing only happens when a connection isn't used for a long time. But you are calling ->execute for each row in the DB, so I'm a bit puzzled as to why this should happen in your case. Are you sure that the DB engine doesn't die for some obscure reason? (maybe no space left on device).

BTW please don't comment out the use strict; line - it's your most valuable ally when hunting errors.

Second BTW: you don't need all of those intermediate values, $sth->execute(@result) should work just fine.