Unique Constraints Example

Let's look at a simple example with 2 clients adding objects of the same class to the database. First of all, let's create an appropriate configuration:

UniqueConstraintExample.cs: Configure
1private static IConfiguration Configure(){ 2 IConfiguration configuration = Db4oFactory.NewConfiguration(); 3 configuration.ObjectClass(typeof(Pilot)).ObjectField("_name").Indexed(true); 4 configuration.Add(new UniqueFieldValueConstraint(typeof(Pilot), "_name")); 5 return configuration; 6 }

UniqueConstraintExample.vb: Configure
1Private Shared Function Configure() As IConfiguration 2 Dim configuration As IConfiguration = Db4oFactory.NewConfiguration 3 configuration.ObjectClass(GetType(Pilot)).ObjectField("_name").Indexed(True) 4 configuration.Add(New UniqueFieldValueConstraint(GetType(Pilot), "_name")) 5 Return configuration 6 End Function

Configuration returned by configure method will be passed to the server.

UniqueConstraintExample.cs: StoreObjects
01private static void StoreObjects(){ 02 File.Delete(FileName); 03 IObjectServer server = Db4oFactory.OpenServer(Configure(), FileName, 0); 04 Pilot pilot1 = null; 05 Pilot pilot2 = null; 06 try { 07 IObjectContainer client1 = server.OpenClient(); 08 try { 09 // creating and storing pilot1 to the database 10 pilot1 = new Pilot("Rubens Barichello",99); 11 client1.Set(pilot1); 12 IObjectContainer client2 = server.OpenClient(); 13 try { 14 // creating and storing pilot2 to the database 15 pilot2 = new Pilot("Rubens Barichello",100); 16 client2.Set(pilot2); 17 // commit the changes 18 client2.Commit(); 19 } catch (UniqueFieldValueConstraintViolationException ex){ 20 System.Console.WriteLine("Unique constraint violation in client2 saving: " + pilot2); 21 client2.Rollback(); 22 } finally { 23 client2.Close(); 24 } 25 // Pilot Rubens Barichello is already in the database, 26 // commit will fail 27 client1.Commit(); 28 } catch (UniqueFieldValueConstraintViolationException ex){ 29 System.Console.WriteLine("Unique constraint violation in client1 saving: " + pilot1); 30 client1.Rollback(); 31 } finally { 32 client1.Close(); 33 } 34 } finally { 35 server.Close(); 36 } 37 }

UniqueConstraintExample.vb: StoreObjects
01Private Shared Sub StoreObjects() 02 File.Delete(FILENAME) 03 Dim server As IObjectServer = Db4oFactory.OpenServer(Configure, FILENAME, 0) 04 Dim pilot1 As Pilot = Nothing 05 Dim pilot2 As Pilot = Nothing 06 Try 07 Dim client1 As IObjectContainer = server.OpenClient 08 Try 09 ' creating and storing pilot1 to the database 10 pilot1 = New Pilot("Rubens Barichello", 99) 11 client1.Set(pilot1) 12 Dim client2 As IObjectContainer = server.OpenClient 13 Try 14 ' creating and storing pilot2 to the database 15 pilot2 = New Pilot("Rubens Barichello", 100) 16 client2.Set(pilot2) 17 ' end commit the changes 18 client2.Commit() 19 Catch ex As UniqueFieldValueConstraintViolationException 20 System.Console.WriteLine("Unique constraint violation in client2 saving: " + pilot2.ToString()) 21 client2.Rollback() 22 Finally 23 client2.Close() 24 End Try 25 'Pilot Rubens Barichello is already in the database, 26 ' commit will fail 27 client1.Commit() 28 Catch ex As UniqueFieldValueConstraintViolationException 29 System.Console.WriteLine("Unique constraint violation in client1 saving: " + pilot1.ToString()) 30 client1.Rollback() 31 Finally 32 client1.Close() 33 End Try 34 Finally 35 server.Close() 36 End Try 37 End Sub

Running this example you will get an exception trying to commit client1 as the changes made by client2 containing a Pilot object with the same name are already committed to the database.