Using a Database Over a Webservice
May 28, 2007 at 6:24 pm | In .NET Remoting, Marshal By Value, Persistence, db4o | 12 CommentsIntroduction
RemotingService project was developed as a simple proof of concept (the idea taken
from Database as a Webservice). It is just one more way to realize a remote persistence.
In fact, putting any database on a web-server and providing a remote user interface to it, will realize the same idea from a user point of view. The interesting point of this solution is in the fact that no extra interface is developed, actually database interface is just transferred over the network.
This realization is possible due to the simple object-oriented interface provided by db4o database.
A possible implementation can be in providing personal web-based databases for remote
customers. Assuming that each user will use only one and his own database, this solution seems to be viable, providing the data is personally encrypted and unique database names are used.
Background
This article was inspired by Krispy Blog.
Solution Organization
RemotingXml consists of 4 projects:
- Db4objects.Db4o: db4o open-source database. Please, refer to db4o.license.html for licensing information. The code was slightly modified, which will be discussed later.
- Server: contains the code for a remote server. Server project is responsible for creating and starting remoting services.
- RemotingExample: contains the code for creating persistent objects, sending them for serialization to the server and requesting the server for objects from the database.
- RemotingClasses project is a library containing the definitions for classes to be
persisted.
Remoting Classes
RemotingClasses project contains only one test class, which will be used on the client to be stored on the server. The class must have Serializable attribute to be transferrable over the network:
[Serializable]public class TestValue
{
…
}
db4o Database Amendments
In order to send the persistent classes from the client to the server we will need a Marshal-By-Reference object. This can be any class providing access to an ObjectContainer (can be modified ObjectContainer itself). The requirements to the transport object:
- the transport object should open a database file on request (filename should be provided somehow);
- the transport object must have zero-parameters constructor.
With the first implementation I tried to modify the ObjectContainer interface and the implementing classes. However it resulted in quite a lot of modifications as the hierarchy is
deep. Moreover, I could not find a logical place to pass a filename to the object container (default implementation requires it in the constructor, and our rules need a parameterless constructor).
In the implementation, provided in this example, I use a version of Db4oFactory class, which can work over the network and can provide ObjectContainers for client requests:
using System;
using System.IO;
using Db4objects.Db4o;
using Db4objects.Db4o.Config;
using Db4objects.Db4o.Ext;
using Db4objects.Db4o.Foundation;
using Db4objects.Db4o.Foundation.Network;
using Db4objects.Db4o.Internal;
using Db4objects.Db4o.Internal.CS;
using Db4objects.Db4o.Reflect;
namespace Db4objects.Db4o
{
///
/// This class simply duplicates Db4oFactory with
/// instance methods, thus allowing Marshalling to
/// access them
///
public class Db4oRemoteFactory: MarshalByRefObject
{
internal static readonly Config4Impl i_config = new Config4Impl();
public IConfiguration NewConfiguration()
{
return Db4oFactory.NewConfiguration();
}
public IConfiguration CloneConfiguration()
{
return (Config4Impl)((IDeepClone)Db4oFactory.Configure()).DeepClone(null);
}
public IObjectContainer OpenFile(string databaseFileName)
{
return OpenFile(CloneConfiguration(), databaseFileName);
}
public IObjectContainer OpenFile(IConfiguration config, string databaseFileName
)
{
return ObjectContainerFactory.OpenObjectContainer(config, databaseFileName);
}
}
}
This implementation is convenient for clients working separately. However, it is not suitable for concurrent collaborative work on the same database file.
Another important change to the db4o sources: some of the internal classes representing persistent objects were marked [Serializable] to be suitable for over-network transmission.
Server
The Server project is presented by a single Program class, which role is to start Db4oRemoteFactory service:
class Program
{
static void Main(string[] args)
{
// Setting up tcp channel
BinaryClientFormatterSinkProvider clientProvider = null;
BinaryServerFormatterSinkProvider serverProvider =
new BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
IDictionary props = new Hashtable();
/*
* Client and server must use the SAME port
* */
props["port"] = 65101;
props["typeFilterLevel"] = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
TcpChannel chan =
new TcpChannel(props, clientProvider, serverProvider);
ChannelServices.RegisterChannel(chan);
Type TestFactory = Type.GetType("Db4objects.Db4o.Db4oRemoteFactory, Db4objects.Db4o");
RemotingConfiguration.RegisterWellKnownServiceType(
TestFactory,
"TestFactoryEndPoint",
WellKnownObjectMode.Singleton
);
Console.WriteLine("TestFactory is ready.");
// Keep the server running until the user presses
// the Enter key.
Console.WriteLine("Services are running. Press Enter to end...");
Console.ReadLine();
}
// end Main
}
Client
Client project contains Program class.
In the constructor a connection to the Db4oRemoteFactory service is established:
public Program()
{
string url;
// Setup a client channel to our services.
url = @"tcp://LocalHost:65101/";
BinaryClientFormatterSinkProvider clientProvider =
new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider serverProvider =
new BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 0;
props["name"] = System.Guid.NewGuid().ToString();
props["typeFilterLevel"] = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
TcpChannel chan =
new TcpChannel(props, clientProvider, serverProvider);
ChannelServices.RegisterChannel(chan);
// Set an access to the remote proxy
factory = (Db4oRemoteFactory)RemotingServices.Connect(
typeof(Db4oRemoteFactory), url + "TestFactoryEndPoint"
);
}
// end Program
The RunTest method creates a test object, sends it to the server using factory and retrieves it from the database on the server:
private void RunTest()
{
// Remote persistent class
TestValue testValue;
testValue = new TestValue();
// Modify local TestValue object
testValue.ChangeData("Test value");
// Pass testValue to the server through testServer
// and store the testValue object
IObjectContainer testServer = factory.OpenFile("test.db4o");
testServer.Set(testValue);
// Test what is stored to the database
IList result = testServer.Get(typeof(TestValue));
//TestValue test;
foreach (TestValue test in result) {
System.Console.WriteLine(test);
}
}
// end RunTest
Points of Interest
- The most interesting TO-DO on this project is, of course, its performance. How much information is transferred over the network for a single request? How is it comparable to the client-server version of db4o?
- Are there any advantages over client-server version of db4o?
- This implementation can only work over tcp-channel. Is it possible to realize the same functionality over html-channel?
- How to realize concurrency control?
History
2007-05-28 First version.
12 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.
This is very impressive. I am so glad you took up the challenge! I’m going to play with it a bit this week and I’ll get back to you on ideas about performance etc.
Comment by kuccello — May 28, 2007 #
Thanks, kuccello. It would be really interesting if you can measure the performance and provide your feedback!
Comment by tetyana — May 28, 2007 #
I cant seem to download the source? I have a DB4O account but when I try to download from your link it says I do not have permission. Can you ping me at kuccello at gmail ? thanks
Comment by kuccello — May 28, 2007 #
It should work now
Comment by tetyana — May 29, 2007 #
I’ve got strange problem. Just after the moment when database file is open I’m getting “A socket operation was attempted to an unreachable host” exception.
I checked the tcp connections manually and it was ok, I guess the problem is with the line:
IList result = testServer.Get(typeof(TestValue));
any suggestions?
Comment by alrc — June 17, 2007 #
Are you using client and server on the same machine (as in example) or on different ones?
I did not get this problem myself, but what I can suggest to do:
- if you are using client and server on the same machine – try to install loopback network adapter.
- try to check your HOSTS file
- flush your dns
Do you have DHCP?
Did you try to specify IP addresses instead of names, i.e. url = @”tcp://127.0.0.1:65101/”;
?
Comment by tetyana — June 18, 2007 #
Follow these guidelines and you will build that new home with little, or no, problems. roofing can help…
Comment by algolosop — November 19, 2007 #
very interesting, but I don’t agree with you
Idetrorce
Comment by Idetrorce — December 15, 2007 #
On which point don’t you agree?
Comment by Tetyana — December 15, 2007 #
tarinfrulli un pchino?
Comment by tarflinti — September 29, 2008 #
if you would be on java you could do it over http using hessian/burlap or httpInvoker
Comment by karussell — April 28, 2009 #
Hello!
How day?
Have you seen it before
Vida Guerra,Vida Guerra naked,Vida Guerra nude,Vida Guerra pics,Vida Guerra playboy,Vida Guerra playboy pics,Vida Guerra ass,Vida Guerra pictures,Vida Guerra wallpaper,new Vida Guerra hot
Click Here:Vida Guerra Pics
Click Here:Vida Guerra Naked
Click Here:Vida Guerra Pics
Click Here:Vida Guerra Nude
Click Here:Vida Guerra Naked
Click Here:Vida Guerra Naked
Click Here:Vida Guerra Naked
Click Here:Vida Guerra Naked
Click Here:Vida Guerra Naked
Click Here:Vida Guerra Naked
Click Here:Vida Guerra Naked
Click Here:Vida Guerra Naked
Traci Bingham,Traci Bingham nude,pics of Traci Bingham naked,Traci Bingham naked,hot Traci Bingham,traci bingham nude naked pics,traci bingham free nude pics,hot traci bingham celebrity,traci bingham topless
Click Here:Traci Bingham Nude
Click Here:Traci Bingham Nude
Click Here:Traci Bingham Nude
Click Here:Traci Bingham Nude
Click Here:Traci Bingham Naked
Click Here:Traci Bingham Naked
Click Here:Traci Bingham Nude
Click Here:Traci Bingham Nude
Click Here:Traci Bingham Nude
Click Here:Traci Bingham Nude
Click Here:Traci Bingham Nude
Stacy Keibler,Stacy Keibler nude,Stacy Keibler hot,Stacy Keibler naked,superiorpics Stacy Keibler 2008 2009,Stacy Keibler ass,Stacy Keibler maxim,Stacy Keibler playboy,Stacy Keibler pics
Click Here:Stacy Keibler Hot
Click Here:Stacy Keibler Nude
Click Here:Stacy Keibler Nude
Click Here:Stacy Keibler Nude
Click Here:Stacy Keibler Naked
Click Here:Stacy Keibler Hot
Click Here:Stacy Keibler Nude
Click Here:Stacy Keibler Nude
Click Here:Stacy Keibler Nude
Click Here:Stacy Keibler Nude
Sophie Monk,Sophie Monk nude,Sophie Monk naked,Sophie Monk pics,Sophie Monk hot,Sophie Monk come my way mp3,Sophie Monk sex tape,Sophie Monk nude naked sex tape vide
Click Here:Sophie Monk Pics
Click Here:Sophie Monk Nude
Click Here:Sophie Monk Naked
Click Here:Sophie Monk Pics
Click Here:Sophie Monk Naked
Click Here:Sophie Monk Nude
Click Here:Sophie Monk Nude
Click Here:Sophie Monk Nude
Click Here:Sophie Monk Nude
Click Here:Sophie Monk Nude
Click Here:Sophie Monk Nude
Jenni Rivera,Jenni Rivera Sex Video,Video Porno De Jenni Rivera,Jenni Rivera Sex Tape,Jenni Rivera Video,Video De Jenni Rivera,Jenni Rivera Video Porno,Video Porno Jenni Rivera,Jenni Rivera Desnuda
Click Here:jenni rivera sex video
Click Here:jenni rivera sex video
Click Here:jenni rivera sex video
Click Here:jenni rivera sex video
Click Here:jenni rivera sex video
Click Here:jenni rivera sex video
Click Here:jenni rivera sex video
Click Here:jenni rivera sex video
Click Here:jenni rivera sex video
Click Here:jenni rivera sex video nude naked
Rachel Uchitel,Rachel Uchitel Nude,Rachel Uchitel Photos,Rachel Uchitel Pictures,Rachel Uchitel Pics,Rachel Uchitel,Tiger Woods Rachel Uchite
Click Here:rachel uchitel photos
Click Here:rachel uchitel photos
Click Here:rachel uchitel nude
Click Here:rachel uchitel nude
Click Here:rachel uchitel nude
Click Here:rachel uchitel nude
Click Here:rachel uchitel nude
Click Here:rachel uchitel nude
Comment by moimadrop — December 6, 2009 #