[longomatch] Handle database errors



commit 6e227c8825771a59b1ff99078dd4e47ad562a74a
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Wed Jun 26 14:28:01 2013 +0200

    Handle database errors

 LongoMatch.Core/Common/Exceptions.cs     |   41 ++++++++++++++++++++++++++++++
 LongoMatch.Core/LongoMatch.Core.mdp      |    1 +
 LongoMatch.Core/Makefile.am              |    1 +
 LongoMatch.Services/Services/DataBase.cs |   41 +++++++++++++++++------------
 LongoMatch/Main.cs                       |   12 ++++++++-
 5 files changed, 78 insertions(+), 18 deletions(-)
---
diff --git a/LongoMatch.Core/Common/Exceptions.cs b/LongoMatch.Core/Common/Exceptions.cs
new file mode 100644
index 0000000..29e851f
--- /dev/null
+++ b/LongoMatch.Core/Common/Exceptions.cs
@@ -0,0 +1,41 @@
+//
+//  Copyright (C) 2013 Andoni Morales Alastruey
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+using System;
+using Mono.Unix;
+
+namespace LongoMatch.Common
+{
+       public class DBLockedException: Exception
+       {
+               public DBLockedException (Exception innerException):
+                       base (Catalog.GetString("Database locked:" + innerException.Message),
+                             innerException)
+               {
+               }
+       }
+       
+       public class UnknownDBErrorException: Exception
+       {
+               public UnknownDBErrorException (Exception innerException):
+                       base (Catalog.GetString("Unknown database error:" + innerException),
+                             innerException)
+               {
+               }
+       }
+}
+
diff --git a/LongoMatch.Core/LongoMatch.Core.mdp b/LongoMatch.Core/LongoMatch.Core.mdp
index 19bca28..e2d0055 100644
--- a/LongoMatch.Core/LongoMatch.Core.mdp
+++ b/LongoMatch.Core/LongoMatch.Core.mdp
@@ -106,6 +106,7 @@
     <File subtype="Code" buildaction="Compile" name="Interfaces/IDataBaseManager.cs" />
     <File subtype="Code" buildaction="Compile" name="Common/EncodingQuality.cs" />
     <File subtype="Code" buildaction="Compile" name="Common/Gettext.cs" />
+    <File subtype="Code" buildaction="Compile" name="Common/Exceptions.cs" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="System, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089" />
diff --git a/LongoMatch.Core/Makefile.am b/LongoMatch.Core/Makefile.am
index 255f4ee..33c6d7c 100644
--- a/LongoMatch.Core/Makefile.am
+++ b/LongoMatch.Core/Makefile.am
@@ -15,6 +15,7 @@ SOURCES = \
        Common/EncodingSettings.cs \
        Common/EncodingQuality.cs \
        Common/Enums.cs \
+       Common/Exceptions.cs \
        Common/Gettext.cs \
        Common/Image.cs \
        Common/Job.cs \
diff --git a/LongoMatch.Services/Services/DataBase.cs b/LongoMatch.Services/Services/DataBase.cs
index f369594..cc0608c 100644
--- a/LongoMatch.Services/Services/DataBase.cs
+++ b/LongoMatch.Services/Services/DataBase.cs
@@ -24,6 +24,7 @@ using System.IO;
 using System.Linq;
 using Db4objects.Db4o;
 using Db4objects.Db4o.Query;
+using Db4objects.Db4o.Ext;
 
 using LongoMatch.Common;
 using LongoMatch.Interfaces;
@@ -136,25 +137,31 @@ namespace LongoMatch.DB
                public bool Load() {
                        bool ret = false;
                        
-                       Log.Debug ("Loading database file: " + DBFile);
-                       /* Create a new DB if it doesn't exists yet */
-                       if(!System.IO.File.Exists(DBFile)) {
-                               Log.Debug ("File doesn't exists, creating a new one");
-                               CreateNewDB();
-                               ret = true;
-                       }
-                       
-                       GetDBVersion();
-                       GetBackupDate();
-                       CheckDB();
                        try {
-                               BackupDB();
-                       } catch (Exception e) {
-                               Log.Error("Error creating database backup");
-                               Log.Exception(e);
+                               Log.Debug ("Loading database file: " + DBFile);
+                               /* Create a new DB if it doesn't exists yet */
+                               if(!System.IO.File.Exists(DBFile)) {
+                                       Log.Debug ("File doesn't exists, creating a new one");
+                                       CreateNewDB();
+                                       ret = true;
+                               }
+                               
+                               GetDBVersion();
+                               GetBackupDate();
+                               CheckDB();
+                               try {
+                                       BackupDB();
+                               } catch (Exception e) {
+                                       Log.Error("Error creating database backup");
+                                       Log.Exception(e);
+                               }
+                               count = GetAllProjects().Count;
+                               ListObjects();
+                       } catch (DatabaseFileLockedException locked) {
+                               throw new DBLockedException (locked);
+                       } catch (Db4oFatalException ex) {
+                               throw new UnknownDBErrorException (ex);
                        }
-                       count = GetAllProjects().Count;
-                       ListObjects();
                        return ret;
                }
                
diff --git a/LongoMatch/Main.cs b/LongoMatch/Main.cs
index 5ca9758..9509a82 100644
--- a/LongoMatch/Main.cs
+++ b/LongoMatch/Main.cs
@@ -65,7 +65,17 @@ namespace LongoMatch
                            IMultimediaToolkit multimediaToolkit = new MultimediaFactory();
                            manager.LoadExportProjectAddins(guiToolkit.MainWindow);
                            manager.LoadImportProjectAddins(guiToolkit.MainWindow);
-                               Core.Start(guiToolkit, multimediaToolkit);
+                           try {
+                                       Core.Start(guiToolkit, multimediaToolkit);
+                           } catch (DBLockedException locked) {
+                                       string msg = Catalog.GetString ("The database seems to be locked by 
another instance and " +
+                                                                       "the application will closed.\n\n 
Click \"No\" if you are " +
+                                                                       "completely sure this is the only 
instance running and you want to " +
+                                                                       "continue at your own risk."); 
+                                       if (MessagesHelpers.QuestionMessage (null, msg)) {
+                                               return;
+                                       }
+                           }
                                Application.Run();
                        } catch(Exception ex) {
                                ProcessExecutionError(ex);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]