Category Archives: Architecture

Rounding Up 2008

I started this year by taking over the lead of a big project for the Belgian government. The project is an e-learning environment created mainly with Flex, WebORB, .NET, MSMQ and MSSQLServer 2005. Daily hundreds of candidates go to Brussels were they are being tested by our software. Typically some 300 candidates start off at the same time to take a test.

So what have I learned this year (some of the things ;-) ):

Database

  1. Indexes are very important because they can speed up things a lot. However they can also be misused!!! So read about them.
  2. Stored Procedures made it easy to quickly write logic (transaction scripts). However it’s hard to maintain these Stored Procedures and Cache dependencies don’t work with complex Stored Procedures.
  3. MetaData was a very usefull mechanism to add new logic in Flex without having to change table designs.
  4. We also used MetaData for tags which wasn’t the best choice.

.NET

  1. I didn’t have a lot of experience in the beginning of this year so things kind of moved in the direction of Transaction Scripts. They aren’t a bad choice but as the project gets complexer, transaction scripts are difficult to maintain.
  2. SubSonic (o/r mapping tool) did it job very good but I’m not very pleased with the lack of documentation, tutorials and clear explanations. I will certainly look into nHibernate and Entity Framework and see how they work with some of the patterns described in ‘Patterns of Enterprise Application Architecture’ by Fowler. In the future the project will certainly need to be scalable so things will have to change.
  3. Visual Studio is a great IDE.

IIS and ASP.NET

  1. By default IIS recycles application pools every 29 hours (will come back to that later because this caused a lot of problems).
  2. HttpHandlers are great.
  3. Cache is a very important feature if you want to speed up your application.

Flex

  1. Very good choice for our client side development. Application development however is far more complex than writing web pages in .NET. If you need your code to be maintainable you need to know about design patterns, frameworks, refactoring, architecture, … Those things are mainly things you learn after having used them a couple of years.
  2. We now work with modules but in the future we will have to check out those shared libraries as well. Our application is now 2mb big so some kind of intelligent caching will be needed.
  3. Prana is great to configure your application externally. Check it out!
  4. The Flexbuilder Eclipse Plugin is a waste of memory and I hope Adobe tries to improve this in the future. Building our project takes far too long.
  5. Resource bundles could be made easier.
  6. Designing a Flex application is a hard thing because you really need a designer who knows some basic things about Flex.
  7. Implementing Pessimistic Concurrency with Messaging was a very hard one this year!
  8. Looking forward to create a desktop application version of our project.

WebORB

  1. Great product. FluorineFX is the open source alternative, but WebORB has a lot more features. Without a support plan however some things are really hard to debug.
  2. Authentication and Authorization is worth to take a look at, but it’s important to fully understand it.
  3. WebORB messaging integration with MSMQ is great to let other applications know what’s happening.

Other tools that have been very usefull are CvsDude, Trac, Mylyn, SubVersion, Charles, SQL Compare, SQL Data Compare, ant, cruisecontrol, Linq, SilverLight (very promising), Spring,…

Ciao!

Stress Testing with WebORB

Yesterday and today I worked on a performance issue where 350 candidates had to wait more than a minute before their data was loaded. The 350 candidates start their exam simultaenously so the server is heavily loaded at that time. I had a couple of ideas in mind like caching certain things but how was I going to test this. I haven’t got 350 computers at my desk, I just have one laptop (and a development server).

A first tool I got from an answer on StackOverflow. AB is a tool for benchmarking apache server, but it can be used to benchmark any url(in my case local IIS). To do this I created an aspx page that loaded the same data that was loaded by one candidate. I then used AB to call that page multiple times. With this tool I already got a good idea how good my caching was working but I didn’t know if WebORB was also causing performance problems.

Since the last update we put on the development server of the customer, candidates had to wait much longer to get started. My guess was that it had to do with WebORB authentication. I now had to find a way to test my services by using the WebORB gateway. Asynchronous Unit Testing with FlexUnit had to be one of the answers. So I got started and two hours later I had my test. I did however had to try three different things.

In the first try I created a Test and added it a hundred times to the TestSuite. This wasn’t so usefull because the next Test starts only if the previous had ended. I needed simulteanously remote calls, parallel, not serial.

In the second try I created a Test that does a remote call a hundred times and is added only once to the TestSuite. This second try looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package {
	import flexunit.framework.Assert;
	import flexunit.framework.TestCase;
 
	import mx.rpc.AsyncToken;
	import mx.rpc.Fault;
	import mx.rpc.IResponder;
	import mx.rpc.Responder;
	import mx.rpc.remoting.mxml.RemoteObject;
 
	public class LoadExamSessionTest extends TestCase  {
 
		private var remoteObject:RemoteObject;
 
		override public function setUp():void {
			remoteObject = new RemoteObject("GenericDestination");
			remoteObject.source = "Edu3.ApplicationTestServices.LoadExamSessionBenchMarkTestService";
			remoteObject.endpoint = "http://localhost/edumatic3/local/weborb.aspx";
			// This is needed because the weborb authentication is enabled on the server side.
			remoteObject.setCredentials("credentials", "credentials");
 
		}
 
		public function testLoadExamSessionTest():void {
			for(var i:int ; i < 200 ; i++){			
				loadExamSession();
			}
		}
 
		public function loadExamSession():void{
			var token:AsyncToken = remoteObject.LoadTest();
			var responder:IResponder = new Responder
			(
				addAsync(onLoadExamSessionResult, 1000000), 
				null
			);
			token.addResponder(responder);
		}
 
		public function onLoadExamSessionResult(data:Object):void {
			assertNotNull(data.result);
		}
	}
}

This worked like a charm, but Authentication on the server side is only done once with the frist call because I’m using the same Remote Object every time. You should also check Charles to see what happens because it’s very interesting. Apparently the there are only two calls to weborb.aspx. The first call is the one that will trigger my custom authentication handler, the next call has packages 199 calls to the service… I won’t put a screenshot here because testing it is so much cooler. You can actually see in Charles that the server has started 199 threads and you can follow how much of the amf package is downloaded.

But this wasn’t what I needed because all 350 candidates are authenticated seperately. To simulate this I just create a new remote object for each call. Class now looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package {
	import flexunit.framework.Assert;
	import flexunit.framework.TestCase;
 
	import mx.rpc.AsyncToken;
	import mx.rpc.Fault;
	import mx.rpc.IResponder;
	import mx.rpc.Responder;
	import mx.rpc.remoting.mxml.RemoteObject;
 
	public class LoadExamSessionTest extends TestCase  {
 
		private var remoteObject:RemoteObject;
 
		override public function setUp():void {
		}
 
		public function testLoadExamSessionTest():void {
			for(var i:int ; i < 200 ; i++)
			{			
				loadExamSession();
			}
		}
 
		public function loadExamSession():void{
			remoteObject = new RemoteObject("GenericDestination");
			remoteObject.source = "Edu3.ApplicationTestServices.LoadExamSessionBenchMarkTestService";
			remoteObject.endpoint = "http://localhost/edumatic3/local/weborb.aspx";
			// This is needed because the weborb authentication is still enabled on the server side.
			remoteObject.setCredentials("credentials", "credentials");
 
			var token:AsyncToken = remoteObject.LoadTest();
			var responder:IResponder = new Responder
			(
				addAsync(onLoadExamSessionResult, 1000000), 
				null
			);
			token.addResponder(responder);
		}
 
		public function onLoadExamSessionResult(data:Object):void {
			assertNotNull(data.result);
		}
	}
}

Now you’ll see 200 calls to weborb.aspx in Charles and my custom WebORB authentication handler will be called 200 times. This is somewhat the customers case I wanted to simulate.

What did I find out, well, for 200 remote calls, the third scenario (200 times weborb authentication) takes double as long as the second scenario (once weborb authentication).

The Caching on the server times now speeds up the loading of the data five times!

ps: I do need to dig in a little deeper in the addAsync from FlexUnit because when I add another addAsync for my fault event in the Reponder, things get messy… Will keep you posted on this.

Ciao! Lieven Cardoen aka Johlero

IIS Recycling

Last week I got a call from a big customer saying that the 100 of the 200 candidates had just received an error log window in our Flex Application. I knew that this could happen someday, but what had caused this error log…?

After a lot of searching in the logs of the two servers at the customer, I found out that at a certain time on one of the two servers all sessions and application pool were ended. Why? Well, after again a lot of searching I found out that IIS recycles an application pool every 29 hours… Man, why is this default enabled???

Apparently a lot of these recycle events are nog logged by default. Only the 29 hour recycle event apparently is logged by default. As for now I haven’t found this log yet, so I will have to enable all those logs to find the actual reason why the application pool recycled…

That’s done by this command on IIS6:

cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs Set w3svc/AppPools/LogEventOnRecycle 255

On IIS7 you can enable them in the Advanced Settings of the application pool.

The problem with our Flex Application was that it uses WebORB Authentication. This authentication is done by the first remote call after which the server side session remembers the credentials. After the recycle the sessions were gone and 100 candidates received a WebORB Security error… We have already solved this problem by re-authenticating when this happens but I’m also really hoping to find out why the application pool recycled in the first place… Any suggestions are welcome… (memory?, too mucht connections?, database problem?, …).

Ciao, Lieven Cardoen aka Johlero

Data Object dependencies

I’ve seen that working with stored procedures in MSSQL2005 sometimes raises a lot of questions in me in how to maintain all those stored procedures. I was wondering if there was a way of checking Data Object Dependencies, such as stored procedures that depend on a table.

I’ve gotten some good answers on StackOverflow.

Apparently there is a database engine stored procedure and two dynamic management functions you can use to do this like:

  1. sp_depends: Displays information about database object dependencies, such as the views and procedures that depend on a table or view, and the tables and views that are depended on by the view or procedure. References to objects outside the current database are not reported. –> will be removed in future versions of mssqlserver
  2. sys.dm_sql_referencing_entities: Returns one row for each entity in the current database that references another user-defined entity by name. A dependency between two entities is created when one entity, called the referenced entity, appears by name in a persisted SQL expression of another entity, called the referencing entity. For example, if a user-defined type (UDT) is specified as the referenced entity, this function returns each user-defined entity that reference that type by name in its definition. The function does not return entities in other databases that may reference the specified entity. This function must be executed in the context of the master database to return a server-level DDL trigger as a referencing entity.
  3. sys.dm_sql_referenced_entities: Returns one row for each user-defined entity referenced by name in the definition of the specified referencing entity. A dependency between two entities is created when one user-defined entity, called the referenced entity, appears by name in a persisted SQL expression of another user-defined entity, called the referencing entity. For example, if a stored procedure is the specified referencing entity, this function returns all user-defined entities that are referenced in the stored procedure such as tables, views, user-defined types (UDTs), or other stored procedures.

Also very usefull is SysComments :

SELECT DISTINCT Object_Name(ID)
FROM SysComments
WHERE text LIKE '%Table%'
AND text LIKE '%Column%'

Apparently this SysComments table is a sql server 200 system table included for backward compatibility. Instead you should be using sys.sql_modules, which is a view object.

Writing tests however seems to me the most reliable way of ensuring that nothing was broken when changing an object in the database.

Lieven Cardoen aka Johlero

Prana Array and Method-Invocation

In the next example I extended the previous example with declaring an array and invoking a method on an instance of an object.

Download Project

Example

In the ModelLocator I added two variables which will be manipulated by the prana container.

1
2
public var users:Array;
public var typedUsers:TypedCollection;

Users is an Array, typedUsers is a TypedCollection of User objects (see the Constructor).

There’s also a function to add an instance of a User to the typedUsers collection.

1
2
3
4
public function addUser(user:User):void
{
	this.typedUsers.addItem(user);	
}

In the PranaExample.mxml (application starting point) I added two List controls with dataproviders users and typedUsers.

1
2
3
4
5
6
7
8
<mx:List dataProvider="{JohleroModelLocator.getInstance().users}" 
	labelField="name"
	rowCount="{JohleroModelLocator.getInstance().users.length}"
/>
<mx:List dataProvider="{JohleroModelLocator.getInstance().typedUsers}" 
	labelField="name"
	rowCount="{JohleroModelLocator.getInstance().typedUsers.length}"
/>

The two variables (users and typedUsers) are being populated by the Prana Framework like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<object id="johleroModelLocator" class="johlero.model.JohleroModelLocator" factory-method="getInstance">		
	...
   	<!-- Example declaring an array -->
   	<property name="users">
		<array>
			<value>
				<object class="johlero.model.User">
					<constructor-arg value="Johlero"/>
				</object>
			</value>
			<value>
				<object class="johlero.model.User">
					<constructor-arg value="John"/>
				</object>
			</value>
			<value>
				<object class="johlero.model.User">
					<constructor-arg value="Mark"/>
				</object>
			</value>
		</array>
	</property>	
 
	<!-- Example using method-invocation -->
	<method-invocation name="addUser">
		<arg>
			<object class="johlero.model.User">
				<constructor-arg value="Johlero"/>
			</object>
		</arg>
	</method-invocation>
</object>

As you can see the users array is just populated by an array tag with multiple value tags.
To populate the typedUsers TypedCollection, we need to call the addUser function on the JohleroModelLocater. The arg tag defines the argument that will be passed to the addUser function.

In the lib there’s a prana-main.swc. This swc is not the same as the swc of the latest production release from prana. There was a bug and it’s fixed in this swc. You can allways checkout the prana repository from https://prana.svn.sourceforge.net/svnroot/prana/prana-main/trunk and add a reference to your project. Like that you’ll allways have the latest code available (with its risks).

That’s it. See the example and project to see it work!

Ciao! Lieven Cardoen aka Johlero

External Configuration Using Prana Framework – Simple Example Extended

In this post I somewhat extended the previous example to show you how to work with a properties.txt file and interface-based programming.

Download Flex Project

Example (right click swf to see source)

The application-context-johlero.xml looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0"?>
<objects xmlns="http://www.pranaframework.org/objects"
	 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 	xsi:schemaLocation="http://www.pranaframework.org/objects http://www.pranaframework.org/schema/objects/prana-objects-0.6.xsd"
>
	<property file="application-context-johlero.properties.txt" />
 
	<!-- Create an instance of a class implementing ICalculationFactory interface         -->
	<!-- You can switch between MultiplyFactory and DivisionFactory to see the difference -->
	<!-- object id="multiplyFactory" class="johlero.factory.MultiplyFactory"/-->
	<object id="divisionFactory" class="johlero.factory.DivisionFactory"/>
 
	<!-- ================================================ -->
	<!-- Singleton -->
	<!-- Prana will call getInstance() function on instance of johlero.model.JohleroModelLocator -->
	<!-- Next Prana will set the four variables -->
	<!-- ================================================ -->	
	<object id="johleroModelLocator" class="johlero.model.JohleroModelLocator" factory-method="getInstance">		
		<property name="selected" value="true" />
		<property name="title" value="Johlero Prana Framework Example" />
		<property name="fontColor" value="0xFC0C06"/>
		<property name="fontSize" value="40"/>
 
		<!-- the ref attribute is a reference to previous defined instance -->
		<property name="calculationFactory" ref="divisionFactory"/>
 
		<!-- Next property is assigned a value that is defined in  -->
		<!-- application-context-johlero.properties.txt            -->
		<property name="gatewayUrl" value="${gatewayUrl}"/>
 
		<!-- Next Commented lines are another way of assigning an object to  -->
		<!-- the calculationFactory property                                 -->
		<!--<property name="calculationFactory">-->
        	<!--<object class="johlero.factory.DivisionFactory"/>-->
    	<!--</property>-->
	</object>
</objects>

There are two new things here. First is the reference to a property file application-context-johlero.properties.txt (containing key value pairs) and the assignment of an instance of a class to a property. Notice that the property calculationFactory in JohleroModelLocator can be set to an instance of DivisionFactory or MultiplyFactory in two ways.

In my experience using a property file is usefull to seperate program specific configuration from environment specific configuration. In my example a have a gatewayUrl which is defined in the property file because this gatewayUrl will probably change when deploying on another server.

To learn some things about interface based programming I suggest you read a book about Design Patterns (Head First, Gang of Four) and OOP (Head First) but you can see that Prana is a great way of keeping your application kind of stupid. The application doesn’t know if a division of multiplying will be executed. Only at runtime an instance of DivisionFactory or MultiplyFactory is injected in the application. Because the application doesn’t have a reference to DivisionFactory or MultiplyFactory (only a reference to ICalculationFactory exists) you need to add these Classes to your application. If you don’t do this, Prana will not be able to create an instance of these classes at runtime.

That’s why you’ll see in the PranaExample2.mxml:

1
2
3
4
{
	MultiplyFactory,
	DivisionFactory
}

Well, I think this is about all the explanation you’ll need. The example speaks for itself.

Ciao!

Law Of Demeter

If you want to walk your dog, you don’t command your dog’s legs to walk. Instead you command the dog and trust that it takes care of its legs itself.

Only talk to your immediate friends.

In modern program languages that use dot as field identifier, use only one dot.

Law of Demeter for functions requires that a method M of an object O may only invoke the methods of the following kinds of objects:

  1. O itself
  2. M‘s parameters
  3. any objects created/instantiated within M
  4. O‘s direct component objects

That summarizes the whole Law Of Demeter, but maybe some more information will enlighten you more… :-)

The purpose of the Law Of Demeter is to minimize coupling between modules. It reduces the size of the response set in the calling class and this tends to reduce errors (a response set is the number of functions directly invoked by methods of the class).

An example in a real programming language makes it all clear for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Demeter
{
  private AClass a;
 
  private int func() { return 1; }
 
  public Demeter(BClass b)
  {
    CClass c = new CClass();
 
    /**
    * The law of demeter for functions states that any method
    * of an object should call only methods belonging to:
    */
 
    //Itself
    int f = func();
 
    //Any parameters that were passed in to the method
    b.invert();
 
    //Any objects it created
    a = new AClass();
    a.setActive();
 
    //Any directly held component objects
    c.print();
  }
}

Advantages:

  • It’ll make your code more maintainable and adaptable. Since coupling is minimized, object containers can be changed without reworking their callers.

Disadvantages:

  • It sometimes requiers to write a great deal of small wrapper methods to propagate method calls to the components or subcontractors. As a result, the class’s interface becomes rather bulky and performance can decrease. Reversing the Law of Demeter and tightly coupling modules may give you performance gain.

Mapping to Relational Databases – Behaviour Problem

The Behavioral Problem

There are the structural aspects (how tables relate to objects) but there’s also a behavioral problem which is harder to solve. How will you get the objects to load and save themselves in the database? If you load a lot of objects into memory, you will have to make sure that the database you are working with stays consistent and synchronized with your memory model. This is not an easy job because for instance sometimes you need to create a row first before you can modify another (because you need the id of the created row). Concurrency is also a big issue here!

The Unit of Work pattern can solve both of these problems. It keeps track of all objects read from the database together with all objects that are modified. It also knows how to make updates to database. The programmer then tells the Unit of Work to commit. It is the controller of the database mapping.

An Identity Map is needed to keep a record of every row you read. If you read some data, you can check this Identity Map to know if you don’t allready have it.

When using a Domain Model, usually you will load linked objects together. Without Lazy Load however, this would mean that enormous object graphs would be loaded out of the database. Lazy Load relies on having a placeholder for a reference to an object. If you try to follow the placeholder, the real object will get pulled from the database. A good example here is when in Flex we have to load big trees. If you have a tree with 100.000 folders, loading them all together would take a while. If we load a folder, we give back its children but not the children of the children. So a depth 1 is returned.

In a previous project created with Flex/Fluorine Gateway/.NET C#/SQL2005 I just save the whole model. Having to program a Unit of Work keeping track of changes would have taken me too long. I realize that if the project would be used by thousand concurrent users, changes would have to be made. For now, it isn’t a problem, but I can imagine that working out a Unit of Work would not be an easy job. In the current project a similar problem arises when editing exams. An exam consists of some components that contain items (exercises). A Unit of Work would have to keep track of deleting items, moving items, changing items, changing components, … Now we just save the whole Exam object (this means mapping it to DTO’s in Flex, sending it to the service layer, converting it to Table Data Gateway, deleting the Exam and saving the Table Data Gateway). A fast solution, but it actually takes 30 seconds to do it… No need to say we’ll have to change this in the future.

Mapping to Relational Databases – Patterns

A big part in the role of the data source layer is talking to a database, most of the times a relational database. I have done some reading about how to implement this data source layer because it can become pretty messy in big projects. It’s also hard to refactor once a choice of pattern is made so it’s important to make the right choice from the beginning. For my current project I’ll look into some patterns, ideas, new technologies and write down a resume here.

Architectural patterns

They define how the domain logic talks to the database. In my experience there should be data access layer (.NET, Java, …) developers and database developers. Most of the times a programmer also does the database side as a result of which they don’t become database experts. When you have data access layer developers and database developers, you will want to seperate SQL from the code, so that a database developer can have a look at the SQL. In this way, a Database administrator can understand how best to tune it and how to arrange indexes.

We had a performance problem with lazy loading a tree. It went so slow that usability was down to nothing. The trees are saved as left right trees in the database (preorder tree traversal algorithm). Normally the advantage of these trees are that loading the whole tree goes very fast. However, for the lazy loading, we only need the children of the node we are loading, and this took a very long time. After searching a bit on indexes in SQL, I managed to improve the performance to a level that usability is really very good now. Lazy loading the trees is now a pleasure.

A good way to do this is to have classes based on the tables (one class for each table). These classes form a gateway to the table. Developers who specialize in the database have a clear place to go.

  1. Row Data Gateway: An instance for each row that’s returned by a query.
  2. Table Data Gateway: Single object for each table in the database. This provides methods to query the database that return a Record Set.

This will only work for applications where the domain model is allmost equal to the database structure, having one class for each table.

A Table Data Gateway can also be used to organise Stored Procedures. You could treat the Stored Procedures as Tables and have a Table Data Gateway to wrap the calls to the stored procedures.

Active Record is having a Row Data Gateway and then adding domain logic to the class. Again, if your Domain Model corresponds closely to the database structure, this option is usefull. However, if your Domain Model becomes more and more complicated, the Active Record approach will break down sooner or later. As you start to implement inheritance, OO patterns, stategies, … in your Domain Model, things will get out of control. Another thing is that O/R mapping tools generate classes that you will need to extend to add Domain Logic. Eventually sending these objects to Flex will get messy as you will have to exclude certain properties that do not need to be send. A solution is to isolate your Domain Model from the database by making an indirection layer responsible for the mapping between domain objects and db tables. This is called a Data Mapper and it completly isolates the two layers.

There are tools that create these patterns(O/R mapping tools). I have used MyGeneration together with Entity Spaces and SubSonic (both Table & Row Data Gateways I think). Both were very helpfull but not ideal. They are third party tools and when you change the database you need to recreate all the classes. The biggest problem is that if the database changes, compiling the project will be no problem. At runtime however problems will start and errors will follow. All the projects that I’ve done are RIA using Flex and Remote Calls to the Server. This means having DTO’s. The classes created by Entity Spaces and SubSonic are too large and complicated too send to Flex. So I had to create Mappers from Entity Spaces/SubSonic to the DTO’s, which made it complicated. I’m kind of hoping that LINQ solves this. In the current project I’m working on the complexity is even bigger. I’m using Stored Procedures to retrieve data and SubSonic to save data and sometimes retrieve data. This means I have to convert DataSets and SubSonic Objects to DTO’s before sending them to Flex. Now we are also porting the model from Flex to .NET, which means that converters also need to be able to convert datasets and subsonic objects to the Model. It gets kind of complicated and I’m searching for a good solution here.

Another thing are OO databases. I haven’t experimented with them yet, but the whole problem of mapping the database to the model would disappear with a OO database (I think). The whole mapping thing is only relevant because of the fundamental difference between objects and relations. Objects hold a reference to another object while tables hold a primary id to another record. I guess not many projects use OO databases, maybe because relational databases have been around for a long time and are proven technology.

Offline Concurrency Control

Concurrency is a big issue if you have business applications created in Flex. If two clients are editing the same data then the last client that saves the data will win.

Optimistic Offline Lock has the best liveness but you will only find out that a business transaction fails when you commit (save). So if you have been working for half an hour on some data the system will not commit the transaction and users won’t like this. Possibilites here are:

  • Letting the user save his data under another name
  • Letting the user decide whether his data should overwrite the existing data
  • Letting the user compare the two versions and resolve the problems

Optimistic Offline Lock is easy to program. If you commit data, you need to check if the data in the database has changed since you loaded the data. Providing a solution however is more difficult.

That’s why we choose for a combination of Pessimistic Offline Lock and Implicit Lock. It’s harder to program but you don’t need any of the solutions above and it’s much more userfriendly. We combined it however with Coarse-Grained Lock, managing the concurrency of a group of objects together.

If a user opens an object to edit its data, then a message is send to all other clients saying that object of type A with id x is locked. A visual notification is shown in the other clients. When the user navigates away from editing the object, a message is send saying that the object of type A with id x is unlocked. We also send the id of the user and client locking the object. A drawback is that when a client opens an object for editing and leaves his desk for instance, then the object stays locked. However, after a half hour of inactivity, the user is logged out automatically.

The server side also holds a model of objects that are locked. If a client connects, it gets all the currently locked objects from the server. If a client disconnects, then the server cleans up the locked objects from that client and notifies the other clients. When two clients request a lock for the same object, then the client that triggers first the server side function to lock an object wins.

Concurrency was one of the biggest challenges so far and I’m not quite sure if our method is the most appropriate. It’s hard to test, hard to debug and your application starts to rely for a part on being able to send and receive messages (that’s why we made it configurable). We have implemented it using WebORB and it seems pretty stable.

Cheers, Lieven Cardoen