diff --git a/camel/camel-folder-summary.c b/camel/camel-folder-summary.c
index b97887f..45c820f 100644
--- a/camel/camel-folder-summary.c
+++ b/camel/camel-folder-summary.c
@@ -1663,9 +1663,11 @@ preview_update (CamelSession *session,
 	}
 
 	camel_folder_lock (folder, CAMEL_FOLDER_REC_LOCK);
+	camel_folder_summary_lock (folder->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 	camel_db_begin_transaction (parent_store->cdb_w, NULL);
 	g_hash_table_foreach (hash, (GHFunc) msg_update_preview, folder);
 	camel_db_end_transaction (parent_store->cdb_w, NULL);
+	camel_folder_summary_unlock (folder->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 	camel_folder_unlock (folder, CAMEL_FOLDER_REC_LOCK);
 	camel_folder_free_uids (folder, uids_uncached);
 	camel_folder_summary_free_hashtable (hash);
@@ -1793,7 +1795,9 @@ camel_folder_summary_load_from_db (CamelFolderSummary *s,
 	if (local_error != NULL && local_error->message != NULL &&
 		strstr (local_error->message, "no such table") != NULL) {
 		/* create table the first time it is accessed and missing */
+		camel_folder_summary_lock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 		ret = camel_db_prepare_message_info_table (cdb, full_name, error);
+		camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 	} else if (local_error != NULL)
 		g_propagate_error (error, local_error);
 
@@ -2010,9 +2014,11 @@ camel_folder_summary_migrate_infos (CamelFolderSummary *s)
 		return -1;
 	}
 
+	camel_folder_summary_lock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 	camel_db_begin_transaction (cdb, NULL);
 	ret = camel_db_write_folder_info_record (cdb, record, NULL);
 	camel_db_end_transaction (cdb, NULL);
+	camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 
 	g_free (record->folder_name);
 	g_free (record->bdata);
@@ -2141,11 +2147,12 @@ save_message_infos_to_db (CamelFolderSummary *s,
 	parent_store = camel_folder_get_parent_store (s->folder);
 	cdb = parent_store->cdb_w;
 
-	if (camel_db_prepare_message_info_table (cdb, full_name, error) != 0)
+	camel_folder_summary_lock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
+	if (camel_db_prepare_message_info_table (cdb, full_name, error) != 0) {
+		camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 		return -1;
+	}
 
-	camel_folder_summary_lock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
-	
 	/* Push MessageInfo-es */
 	camel_db_begin_transaction (cdb, NULL);
 	g_hash_table_foreach (s->loaded_infos, save_to_db_cb, &args);
@@ -2224,13 +2231,18 @@ camel_folder_summary_save_to_db (CamelFolderSummary *s,
 
 		full_name = camel_folder_get_full_name (s->folder);
 		g_warning ("Fixing up a broken summary migration on %s\n", full_name);
-		
+
+		camel_folder_summary_lock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
+
 		/* Begin everything again. */
 		camel_db_begin_transaction (cdb, NULL);
 		camel_db_reset_folder_version (cdb, full_name, 0, NULL);
 		camel_db_end_transaction (cdb, NULL);
 
 		ret = save_message_infos_to_db (s, FALSE, error);
+
+		camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
+
 		if (ret != 0) {
 			s->flags |= CAMEL_SUMMARY_DIRTY;
 			return -1;
@@ -2243,6 +2255,7 @@ camel_folder_summary_save_to_db (CamelFolderSummary *s,
 		return -1;
 	}
 
+	camel_folder_summary_lock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 	camel_db_begin_transaction (cdb, NULL);
 	ret = camel_db_write_folder_info_record (cdb, record, error);
 	g_free (record->folder_name);
@@ -2250,12 +2263,14 @@ camel_folder_summary_save_to_db (CamelFolderSummary *s,
 	g_free (record);
 
 	if (ret != 0) {
-		camel_db_abort_transaction (cdb, NULL);
 		s->flags |= CAMEL_SUMMARY_DIRTY;
+		camel_db_abort_transaction (cdb, NULL);
+		camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 		return -1;
 	}
 
 	camel_db_end_transaction (cdb, NULL);
+	camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 
 	return ret;
 }
@@ -2284,6 +2299,7 @@ camel_folder_summary_header_save_to_db (CamelFolderSummary *s,
 		return -1;
 	}
 
+	camel_folder_summary_lock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 	camel_db_begin_transaction (cdb, NULL);
 	ret = camel_db_write_folder_info_record (cdb, record, error);
 	g_free (record->folder_name);
@@ -2292,10 +2308,12 @@ camel_folder_summary_header_save_to_db (CamelFolderSummary *s,
 
 	if (ret != 0) {
 		camel_db_abort_transaction (cdb, NULL);
+		camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 		return -1;
 	}
 
 	camel_db_end_transaction (cdb, NULL);
+	camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 
 	return ret;
 }
@@ -2842,9 +2860,9 @@ camel_folder_summary_clear_db (CamelFolderSummary *s)
 	g_hash_table_destroy (s->loaded_infos);
 	s->loaded_infos = g_hash_table_new (g_str_hash, g_str_equal);
 
-	camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
-
 	camel_db_clear_folder_summary (cdb, folder_name, NULL);
+
+	camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 }
 
 /* This function returns 0 on success. So the caller should not bother,
@@ -2893,13 +2911,16 @@ camel_folder_summary_remove (CamelFolderSummary *s, CamelMessageInfo *info)
 
 	s->flags |= CAMEL_SUMMARY_DIRTY;
 	s->meta_summary->msg_expunged = TRUE;
-	camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 
 	full_name = camel_folder_get_full_name (s->folder);
 	parent_store = camel_folder_get_parent_store (s->folder);
 
-	if (!ret && camel_db_delete_uid (parent_store->cdb_w, full_name, camel_message_info_uid (info), NULL) != 0)
+	if (!ret && camel_db_delete_uid (parent_store->cdb_w, full_name, camel_message_info_uid (info), NULL) != 0) {
+		camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 		return;
+	}
+
+	camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 
 	if (found)
 		camel_message_info_free (info);
@@ -2935,15 +2956,16 @@ camel_folder_summary_remove_uid (CamelFolderSummary *s, const gchar *uid)
 		/* Info isn't loaded into the memory. We must just remove the UID*/
 		ret = summary_remove_uid (s, uid);
 		camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_REF_LOCK);
-		camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 
 		full_name = camel_folder_get_full_name (s->folder);
 		parent_store = camel_folder_get_parent_store (s->folder);
 		if (!ret && camel_db_delete_uid (parent_store->cdb_w, full_name, tmpid, NULL) != 0) {
 			g_free (tmpid);
+			camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 			return;
 		}
 		g_free (tmpid);
+		camel_folder_summary_unlock (s, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 	}
 }
 
diff --git a/camel/camel-vee-folder.c b/camel/camel-vee-folder.c
index aef735b..fa58d9e 100644
--- a/camel/camel-vee-folder.c
+++ b/camel/camel-vee-folder.c
@@ -404,6 +404,7 @@ folder_changed_change (CamelSession *session,
 
 	if (matches_changed || matches_added || changes->uid_removed->len||present) {
 		parent_store = camel_folder_get_parent_store (folder);
+		camel_folder_summary_lock (folder->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 		camel_db_begin_transaction (parent_store->cdb_w, NULL);
 	}
 
@@ -545,6 +546,7 @@ folder_changed_change (CamelSession *session,
 	if (matches_changed || matches_added || changes->uid_removed->len || present) {
 		parent_store = camel_folder_get_parent_store (folder);
 		camel_db_end_transaction (parent_store->cdb_w, NULL);
+		camel_folder_summary_unlock (folder->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 	}
 	camel_vee_folder_unlock (vf, CAMEL_VEE_FOLDER_SUMMARY_LOCK);
 
@@ -947,10 +949,12 @@ vee_folder_dispose (GObject *object)
 			folder = CAMEL_FOLDER (vf);
 			parent_store = camel_folder_get_parent_store (folder);
 			record = summary_header_to_db (folder->summary, NULL);
-			
+
+			camel_folder_summary_lock (folder->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 			camel_db_begin_transaction (parent_store->cdb_w, NULL);
 			camel_db_write_folder_info_record (parent_store->cdb_w, record, NULL);
 			camel_db_end_transaction (parent_store->cdb_w, NULL);
+			camel_folder_summary_unlock (folder->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 
 			g_free (record->folder_name);
 			g_free (record);
@@ -1796,6 +1800,7 @@ vee_folder_rebuild_folder (CamelVeeFolder *vee_folder,
 		full_name = camel_folder_get_full_name (folder);
 		parent_store = camel_folder_get_parent_store (folder);
 
+		camel_folder_summary_lock (folder->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 		camel_db_begin_transaction (parent_store->cdb_w, NULL);
 
 		while ((uid = g_queue_pop_head (u.message_uids)) != NULL) {
@@ -1805,6 +1810,7 @@ vee_folder_rebuild_folder (CamelVeeFolder *vee_folder,
 		}
 
 		camel_db_end_transaction (parent_store->cdb_w, NULL);
+		camel_folder_summary_unlock (folder->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 
 		g_queue_free (u.message_uids);
 		u.message_uids = NULL;
@@ -2422,9 +2428,11 @@ camel_vee_folder_sync_headers (CamelFolder *vf,
 	/* Save the counts to DB */
 	record = summary_header_to_db (vf->summary, error);
 	parent_store = camel_folder_get_parent_store (vf);
+	camel_folder_summary_lock (vf->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 	camel_db_begin_transaction (parent_store->cdb_w, NULL);
 	camel_db_write_folder_info_record (parent_store->cdb_w, record, error);
 	camel_db_end_transaction (parent_store->cdb_w, NULL);
+	camel_folder_summary_unlock (vf->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 
 	g_free (record->folder_name);
 	g_free (record);
