TA instrumentation can be done by applying
Db4oTool utility to the ready .NET assemblies:
Db4oTool -ta assembly
Let's look at a simple example. We will use SensorPanel class from Activation example:
SensorPanelTA.cs /**//* Copyright (C) 2004 - 2007 Versant Inc. http://www.db4o.com */ using Db4objects.Db4o; using Db4objects.Db4o.Activation; using Db4objects.Db4o.TA; namespace Db4ojects.Db4odoc.TAExamples { public class SensorPanelTA /**//*must implement Activatable for TA*/ { private object _sensor; private SensorPanelTA _next; public SensorPanelTA() { // default constructor for instantiation } public SensorPanelTA(int value) { _sensor = value; } public SensorPanelTA Next { get { return _next; } } public object Sensor { get { return _sensor; } } public SensorPanelTA CreateList(int length) { return CreateList(length, 1); } public SensorPanelTA CreateList(int length, int first) { int val = first; SensorPanelTA root = NewElement(first); SensorPanelTA list = root; while (--length > 0) { list._next = NewElement(++val); list = list.Next; } return root; } protected SensorPanelTA NewElement(int value) { return new SensorPanelTA(value); } public override string ToString() { return "Sensor #" + Sensor; } } }
SensorPanelTA.vb ' Copyright (C) 2004 - 2007 Versant Inc. http://www.db4o.com Imports Db4objects.Db4o Imports Db4objects.Db4o.Activation Imports Db4objects.Db4o.TA Namespace Db4ojects.Db4odoc.TAExamples Public Class SensorPanelTA Private _sensor As Object Private _next As SensorPanelTA Public Sub New() End Sub Public Sub New(ByVal value As Integer) _sensor = value End Sub Public ReadOnly Property NextSensor() As SensorPanelTA Get Return _next End Get End Property Public ReadOnly Property Sensor() As Object Get Return _sensor End Get End Property Public Function CreateList(ByVal length As Integer) As SensorPanelTA Return CreateList(length, 1) End Function Public Function _ CreateList(ByVal length As Integer, ByVal first As Integer) _ As SensorPanelTA Dim val As Integer = first Dim root As SensorPanelTA = NewElement(first) Dim list As SensorPanelTA = root While System.Threading.Interlocked.Decrement(length) > 0 list._next = NewElement(System.Threading.Interlocked.Increment(val)) list = list.NextSensor End While Return root End Function Protected Function NewElement(ByVal value As Integer) As SensorPanelTA Return New SensorPanelTA(value) End Function Public Overloads Overrides Function ToString() As String If Sensor Is Nothing Then Return "" Else Return "Sensor #" + Sensor.ToString() End If End Function End Class End Namespace
In your code you will need to add Transparent Activation
support to the configuration:
TAExample.cs: ConfigureTA private static IConfiguration ConfigureTA() { IConfiguration configuration = Db4oFactory.NewConfiguration(); // set normal activation to 0 configuration.ActivationDepth(0); // add TA support configuration.Add(new TransparentActivationSupport()); return configuration; }
TAExample.cs: StoreSensorPanel private static void StoreSensorPanel() { File.Delete(Db4oFileName); IObjectContainer container = Database(Db4oFactory.NewConfiguration()); if (container != null) { try { // create a linked list with length 10 SensorPanelTA list = new SensorPanelTA().CreateList(10); container.Store(list); } finally { CloseDatabase(); } } }
TAExample.cs: TestActivation private static void TestActivation() { StoreSensorPanel(); IConfiguration configuration = ConfigureTA(); IObjectContainer container = Database(configuration); if (container != null) { try { System.Console.WriteLine("Zero activation depth"); IObjectSet result = container.QueryByExample(new SensorPanelTA(1)); ListResult(result); if (result.Size() > 0) { SensorPanelTA sensor = (SensorPanelTA)result[0]; // the object is a linked list, so each call to next() // will need to activate a new object SensorPanelTA next = sensor.Next; while (next != null) { System.Console.WriteLine(next); next = next.Next; } } } finally { CloseDatabase(); } } }
TAExample.vb: ConfigureTA Private Shared Function ConfigureTA() As IConfiguration Dim configuration As IConfiguration = Db4oFactory.NewConfiguration ' set normal activation to 0 configuration.ActivationDepth(0) ' add TA support configuration.Add(New TransparentActivationSupport) Return configuration End Function
TAExample.vb: StoreSensorPanel Private Shared Sub StoreSensorPanel() File.Delete(Db4oFileName) Dim container As IObjectContainer = Database(Db4oFactory.NewConfiguration) If Not (container Is Nothing) Then Try ' create a linked list with length 10 Dim list As SensorPanelTA = (New SensorPanelTA).CreateList(10) container.Store(list) Finally CloseDatabase() End Try End If End Sub
TAExample.vb: TestActivation Private Shared Sub TestActivation() StoreSensorPanel() Dim configuration As IConfiguration = ConfigureTA() Dim container As IObjectContainer = Database(configuration) If Not (container Is Nothing) Then Try System.Console.WriteLine("Zero activation depth") Dim result As IObjectSet = container.QueryByExample(New SensorPanelTA(1)) ListResult(result) If result.Size > 0 Then Dim sensor As SensorPanelTA = CType(result(0), SensorPanelTA) ' the object is a linked list, so each call to next() ' will need to activate a new object Dim nextSensor As SensorPanelTA = sensor.NextSensor While Not (nextSensor Is Nothing) System.Console.WriteLine(nextSensor.ToString()) nextSensor = nextSensor.NextSensor End While End If Finally CloseDatabase() End Try End If End Sub
Compile and run the application. Now, you can add TA support by using the following command-line:
Db4oTool -ta TAExamples.exe
use -vv option for verbose output:
Db4oTool -ta -vv TAExamples.exe
You can also apply type filter to TA enable only selected types:
Db4oTool.exe -vv -ta -by-name:S* TAExamples.exe
Db4oTool uses .NET regex to parse the -by-name parameter, in the example above all types starting with "S" will be TA enabled.
Run TA enabled assembly and compare results to the previous run.