Archive for category Persistence

Liferay with Oracle XE

Well, that’s not a rocket science, but just a few simple tips for installing liferay 6 to work with oracle XE.

First you need to

Tune up your Oracle XE instanse:

use command-line:

type

sqlplus system

enter password

SQL> alter system set sessions=250 scope=spfile;
SQL> alter system set processes=200 scope=spfile;

SQL> shutdown immediate // or restart the database using any other ways you know: from the Oracle XE menu, from system services or whatever

This info is taken from here:

http://kishantha.blogspot.com/2009/03/increase-sessions-and-processes-of.html

Then create a user lportal with a passwork lportal and give this user DBA rights and all permissions.

Setup Oracle as your Liferay Database:

in ..\liferay-portal-6.0.5\tomcat-6.0.26\webapps\ROOT\WEB-INF\classes create file portal-ext.properties:

#
# Oracle
#
jdbc.default.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.default.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.default.username=lportal
jdbc.default.password=lportal

Find ojdbc14.jar in your Oracle XE folder and copy it to ..\liferay-portal-6.0.5\tomcat-6.0.26\webapps\ROOT\WEB-INF\lib

More detailed info here:
http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Database%20Configuration

Leave a comment

XP Forum in JHB

Yesterday we had an interesting event in Wits university  –  a presentation about implementing XP in customer contracts. Looks like making a contract can be a real challenge, but that’s the only way to go. The suggested model for XP: fixed cost contract. The idea is to disclose your costs and expected profit to the customer, set a target time and define the scope on a very-high level. The high-level definition of the scope allows both parties to negotiate on the specific module (requires lots of trust).  Disclosing your costs&profits is supposed to improve the trust relationship. The negotiation on modules is certainly something not clear enough, especially if the client in the beginning has no idea of what they want (which is often the case).

Otherwise, I feel quite scared, – probably writing software on demand is very risky. I’m rather looking at SaaS.

, ,

Leave a comment

Using a Database Over a Webservice

Download source code – 780 kB

Introduction

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.

Leave a comment

Marshal By Value for Persistence

I was recently writing about how to use Marshal By Value with db4o and was very surprised how little information could I found on this actually simple subject.

So, to contribute to the global network – here is another Marshal By Value example, this time with xml.

Download RemotingXml.zip – 11.3 KB

Introduction

RemotingXml project was
developed with 2 purposes in mind:

  • show how to use marshal
    by value technology;
  • show how to implement a
    remote storage.

RemotingXml project consists
of a client and a server. The client creates and works with objects and sends them
to the server using marshal-by-value technology to be stored to XML files. The client
is also able to request the objects from the XML files.

This project might be helpful to those, who want to implement some kind of remote
persistence solution. In this example, XML files are used as a very simple and comprehensive
example, however the project can be easily used as a template to build a database
or file-based remoting storage solution.

Background

Though marshal-by-reference
remoting is widely covered on the net, marshal-by-value still tends to create some
confusion. This project is created to clear up this confusion and show an example
of how marshal-by-value objects can be used.

Solution Organization

RemotingXml consists of
3 projects:

  • Server: contains the code
    for a remote server. Server project is responsible for creating and starting remoting
    services.
  • Client: contains the code
    for creating persistent classes, sending them for serialization to the server and
    requesting the server for objects from the XML files.
  • PersistentClasses project
    is a library containing the definitions for classes used on the client and on the
    server. These include: class to be serialized, list wrapper for the serialized class
    and marshal-by-reference class used for persistent classes transport.

Persistent Classes

Persistent classes are the
classes that will be created on the client and serialized on the server. Any class
can be persisted if it either has Serializable attribute
or implements ISerializable interface. In this example,
House and Address classes are used.

Address class is saved as
a field in the House class:

[Serializable]
public class House
{
private Address _address;

private string _owner;
….
}

In order to save a list of houses we will need to create a special List implementation:

[Serializable]

[XmlInclude(typeof(House)), XmlInclude(typeof(Address))]

public class HouseList : List

{

}

This implementation is required for 2 purposes:
An object passed to a remote server should be serializable
The object need to provide the details of included classes for correct serialization.

Transport Class

In order to send the persistent
classes from the client to the server we will need a Marshal-By-Reference object:

    public class XmlHandler: MarshalByRefObject

    {

        private const string Extension = ".xml";

        // list of remote objects

        private IList _persistentObjects;        public IList PersistentObjects

        {

            get { return _persistentObjects; }

            set { _persistentObjects = value; }

        }

        // end PersistentObject

public void StoreData()

        {

            // Serialize object list to xml

            System.Console.WriteLine("persistentObjects " + _persistentObjects.Count);

            if (_persistentObjects.Count > 0)

            {

                // define the object type

                object persistentObject = _persistentObjects[0];

                XmlSerializer serializer = new XmlSerializer(_persistentObjects.GetType());

// delete the old file and create a new one

                string filename = persistentObject.GetType().Name + Extension;

                File.Delete(filename);

                StreamWriter xmlWriter = new StreamWriter(filename);

                // serialize object list

                serializer.Serialize(xmlWriter, _persistentObjects);

                xmlWriter.Close();

            }

        }

        // end StoreData

public IList RetrieveData(Type arrayType, Type objectType)

        {

            // Compose the file name

            string filename = objectType.Name + Extension;

            // deserialize

            XmlSerializer serializer = new XmlSerializer(arrayType);

            FileStream xmlFileStream = new FileStream(filename, FileMode.Open);

            IList objects = (IList)serializer.Deserialize(xmlFileStream);

            xmlFileStream.Close();

            return objects;

        }

        // end RetrieveData

    }

XmlHandler
holds a reference to the persistent objects array in _persistentObjects variable.
This variable is passed from the client.

As you can see there are methods for serializing and deselializing objects. These
methods are executed on the server.

Server

The Server project is presented
by a single XmlServer class, which role is to start XmlHandler service

    class XmlServer

    {

        static void Main(string[] args)

        {

            // Setting up http channel

            HttpChannel channel = new HttpChannel(65101);

            ChannelServices.RegisterChannel(channel);            Type XmlHandler = Type.GetType("RemotingXml.PersistentClasses.XmlHandler, PersistentClasses");

            RemotingConfiguration.RegisterWellKnownServiceType(

                    XmlHandler,

                    "XmlHandlerEndPoint",

                    WellKnownObjectMode.Singleton

                );

            Console.WriteLine("XmlHandler 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 XmlHandler service is established:

        public Program()

        {

            string url;            // Setup a client channel to our services.

            HttpChannel channel = new HttpChannel(0);

            url = @"http://LocalHost:65101/";

// Register the channel

            ChannelServices.RegisterChannel(channel, false);

// Set an access to the remote proxy

            xmlHandler = (XmlHandler)RemotingServices.Connect(

                typeof(PersistentClasses.XmlHandler), url + "XmlHandlerEndPoint"

                );

        }

        // end Program

The RunTest method creates
a list of House objects, sends them to the server using XmlHandler and retrieves
the objects from the XML files on the server:

        private void RunTest()

        {

            // an object to be stored remotely

            House house = new House(new Address("East London", "266 Oxford Street"), "Derick Hopkins");

            // Create a list to hold House instances

            HouseList persistentObjects = new HouseList();

            persistentObjects.Add(house);

            // another House for a list

            house = new House(new Address("East London", "23 Stewart Drive"), "Om Henderson");

            persistentObjects.Add(house);

            // Pass House list to the server using XmlHandler

            xmlHandler.PersistentObjects = (IList)persistentObjects;

            // Call method on the server to store houses to an XML file

            xmlHandler.StoreData();

            // Test what is stored to the database

            IList result = xmlHandler.RetrieveData(typeof(HouseList), typeof(House));

            foreach (object obj in result)

            {

                System.Console.WriteLine(obj);

            }

        }

        // end RunTest

Points of Interest

Creating this project I
had an idea in mind to make it completely generic by using generics for persistent
object collections. However, the idea was not realized due to the fact that Soap
serializer does not support generics and explicit using of binary serializers on
the client and the server still does not solve the problem.

History

2007-05-25 First version.

Leave a comment