I'm not sure if you found the solution in the mean time, but I recently came into the same problem on a client's server.
What happened was indeed the same, the cron job core_email_queue_send_all was sending the same emails multiple times and each time the same exception you found was added into the exception log.
The cron job was sending the same emails multiple times because the processed_at
field did not get saved in the core_email_queue
table for the corresponding messages.
I've added some logs into the code and looked into how the query for saving the core_email_queue
message was built, and why it was missing the SET part of it (which should have contained the columns to be updated):
UPDATE `core_email_queue` SET WHERE (message_id='3')'
In Magento, before building the database queries, the columns used in the query get checked against the column descriptions for the respective table inside the Mage_Core_Model_Resource_Abstract::_prepareDataForTable
method by calling:
$fields = $this->_getWriteAdapter()->describeTable($table);
In order not to execute the DESCRIBE query each time, Magento caches the DDL info for the tables. Inside the Varien_Db_Adapter_Pdo_Mysql::describeTable
method the cache is first checked:
public function describeTable($tableName, $schemaName = null)
{
$cacheKey = $this->_getTableName($tableName, $schemaName);
$ddl = $this->loadDdlCache($cacheKey, self::DDL_DESCRIBE);
if ($ddl === false) {
$ddl = array_map(
array(
$this,
'decorateTableInfo'
),
parent::describeTable($tableName, $schemaName)
);
/**
* Remove bug in some MySQL versions, when int-column without default value is described as:
* having default empty string value
*/
$affected = array('tinyint', 'smallint', 'mediumint', 'int', 'bigint');
foreach ($ddl as $key => $columnData) {
if (($columnData['DEFAULT'] === '') && (array_search($columnData['DATA_TYPE'], $affected) !== FALSE)) {
$ddl[$key]['DEFAULT'] = null;
}
}
$this->saveDdlCache($cacheKey, self::DDL_DESCRIBE, $ddl);
}
return $ddl;
}
I found that the columns received from the cache for the core_email_queue
table, were not the ones expected, instead sometimes they were: data
, lifetime
, expire
and priority
.
This pointed to a cache problem and I found that Zend_Cache_Core
was saving data into the DDL cache files, sometimes by calling Zend_Cache_Backend_File::save()
directly and sometimes by calling Zend_Cache_Backend_TwoLevels::save()
.
The two levels cache from Zend uses the _prepareData
method to build a serialized array to store the data and metadata information:
private function _prepareData($data, $lifetime, $priority)
{
$lt = $lifetime;
if ($lt === null) {
$lt = 9999999999;
}
return serialize(array(
'data' => $data,
'lifetime' => $lifetime,
'expire' => time() + $lt,
'priority' => $priority
));
}
Finally, the issue was that the cron (which sent the emails) was called from the command line. Comparing a request from the browser with a command line request, I found that Mage_Core_Model_Cache::getBackendOptions
was returning different options. This server was set up to use APC cache, however when the cron was running ini_get(‘apc.enabled’)
was false.
On this server there were 2 php configuration files fpm/php.ini where apc.enabled was 1, and cli/php.ini where apc.enabled was 0.
The Magento instance that was run from the command line was not able, thus, to use the APC cache, so it did not use a two-level cache, which led to it not knowing how to correctly read the data from the cache files.
The fpm Magento instance used APC and the two level cache and was saving DDL data into the var/cache folder enclosed in an array with the data
, lifetime
, expire
and priority
keys.
When the cron ran and read the DDL cache file, it used the data found there and basically considered for each table, that the columns are data
, lifetime
, expire
and priority
.
Changing apc.enabled to 1 in the cli/php.ini configuration file did the trick and solved the issue.
If you are interested on reading more about how I debugged this issue you can have a look over the more detailed explication I wrote for a blog post: http://ayg.ro/magento-cron-twolevel-cache-issue-pdoexception-sqlstate-42s22-and-sqlstate-42000
Best Answer
This issue could be related to the new Magento Email Queue system, that leaves orphan records on the Recipients table.
If this is your issue, I've sent a fix on this post: https://magento.stackexchange.com/a/87299/23057