EDG_WLL_IL_EVENTS_WAITING, /**< Interlogger still has events pending delivery. */
        EDG_WLL_ERROR_COMPARE_EVENTS, /**< Two compared events differ. */
        EDG_WLL_ERROR_DB_TRANS_DEADLOCK,        /**< Deadlock detected during DB operation. */
+       EDG_WLL_ERROR_DB_LOST_CONNECTION,       /**< Lost connection to DB */
 } edg_wll_ErrorCode;
 
 /**
 
        if (ctx->dbctx) {
                code = glite_lbu_DBError(ctx->dbctx, NULL, &ed);
                if (code == EDEADLOCK) code = EDG_WLL_ERROR_DB_TRANS_DEADLOCK;
+               if (code == ERESTART) code = EDG_WLL_ERROR_DB_LOST_CONNECTION;
                edg_wll_SetError(ctx, code, ed);
                free(ed);
        } else {
        char *errd;
 
        ret = edg_wll_Error(ctx,NULL,NULL);
+
        if (ret == EDG_WLL_ERROR_DB_TRANS_DEADLOCK) {
                if (debug)
                        printf("[%d]: DB deadlock detected. Rolling back transaction and retrying... \n",getpid());
                edg_wll_Rollback(ctx);
                edg_wll_ResetError(ctx);
                return 1;
+       }
+       if (ret == EDG_WLL_ERROR_DB_LOST_CONNECTION) {
+               if (debug)
+                       printf("[%d]: Lost connection to DB. "
+                               "Rolling back transaction and retrying... \n",getpid());
+               else 
+                       syslog(LOG_INFO,"[%d]: Lost connection to DB. "
+                               "Rolling back transaction and retrying... \n",getpid());
+
+               edg_wll_Rollback(ctx);
+               edg_wll_ResetError(ctx);
+               return 1;
        } else if (ret==0) {
                edg_wll_Commit(ctx); /* errors propagated further */
                return 0;
 
                int code;
                char *desc;
        } err;
+       int in_transaction;     /* this flag is set whenever we are in DB transaction */
 };
 
 
        if (USE_TRANS(ctx)) {
                if (glite_lbu_ExecSQL(ctx, "SET AUTOCOMMIT=0", NULL) < 0) goto err;
                if (glite_lbu_ExecSQL(ctx, "BEGIN", NULL) < 0) goto err;
+               ctx->in_transaction = 1;
        }
 err:
        return STATUS(ctx);
        if (USE_TRANS(ctx)) {
                if (glite_lbu_ExecSQL(ctx, "COMMIT", NULL) < 0) goto err;
                if (glite_lbu_ExecSQL(ctx, "SET AUTOCOMMIT=1", NULL) < 0) goto err;
+               ctx->in_transaction = 0;
        }
 err:
        return STATUS(ctx);
        if (USE_TRANS(ctx)) { 
                if (glite_lbu_ExecSQL(ctx, "ROLLBACK", NULL) < 0) goto err;
                if (glite_lbu_ExecSQL(ctx, "SET AUTOCOMMIT=1", NULL) < 0) goto err;
+               ctx->in_transaction = 0;
        }
 err:
        return STATUS(ctx);
                                        break;
                                case CR_SERVER_LOST:
                                case CR_SERVER_GONE_ERROR:
-                                       if (retry_nr <= 0) 
+                                       if (ctx->in_transaction) {
+                                               ERR(ctx, ERESTART, db_handle.mysql_error(ctx->mysql));
+                                               return -1;
+                                       }
+                                       else if (retry_nr <= 0) 
                                                do_reconnect = 1;
                                        break;
                                case ER_LOCK_DEADLOCK: