diff --git a/job.cpp b/job.cpp
index e4d7784..9d02977 100644
--- a/job.cpp
+++ b/job.cpp
@@ -354,11 +354,13 @@ JobThread::JobThread(const wxString &jid)
 	jobid = jid;
 
 	DBconn *threadConn = DBconn::Get(DBconn::GetBasicConnectString(), serviceDBname);
-	job = new Job(threadConn, jobid);
-
-	if (job->Runnable())
-		runnable = true;
+	if (threadConn)
+	{
+		job = new Job(threadConn, jobid);
 
+		if (job->Runnable())
+			runnable = true;
+	}
 }
 
 
diff --git a/pgAgent.cpp b/pgAgent.cpp
index ce15e08..ff9d069 100644
--- a/pgAgent.cpp
+++ b/pgAgent.cpp
@@ -109,8 +109,23 @@ int MainRestartLoop(DBconn *serviceConn)
 					jt->Run();
 					foundJobToExecute = true;
 				}
+				else
+				{
+					// Failed to launch the thread. Insert an entry with
+					// "internal error" status in the joblog table, to leave
+					// a trace of fact that we tried to launch the job.
+					DBresult *res = serviceConn->Execute(
+						wxT("INSERT INTO pgagent.pga_joblog(jlgid, jlgjobid, jlgstatus) ")
+						wxT("VALUES (nextval('pgagent.pga_joblog_jlgid_seq'), ") + jobid + wxT(", 'i')"));
+					if (res)
+						delete res;
+
+					// A thread object that's started will destroy itself when
+					// it's finished, but one that never starts we'll have to
+					// destory ourselves.
+					delete jt;
+				}
 				res->MoveNext();
-
 			}
 
 			delete res;
