Transaction in additional service

Hi,

I have added a new service (e.g. BatchService) to my existing DA Server.
Operations in this service should process data using a LinqLocalDataAdapter .
How can I manage transactions in this service?

Hello

What exactly you mean by ‘managing transactions’?

In the usual data service you would do something like this:

Connection.BeginTransaction();

Connection.CommitTransaction();

In a service that is not derived form DataAbstractService, there is no Connection object available. Even if it is derived from DataAbstractService it does have a connection, but the database access is running in a transaction of the DataService specified in LinqLocalDataAdapter’s ServiceName property.

Is there a way to have a simple service (not derived form DataAbstractService) with a LinqLocalDataAdapter and get acceess to the Connection of the LinqLocalDataAdapter’s DataService?

Hello

It is possible to use the local data adapter using a pre-instantiated service instance, so it is possible to explicitly control the transaction scope. The code could look like

    
var service = new DataService();
try
{
    service.Activate(this.SessionID);
    service.Connection.BeginTransaction();
    var adapter = new LinqLocalDataAdapter(service);
    
    // do the work
    
    service.Connection.CommitTransaction();
}
finally
{
    service.Deactivate(this.SessionID);
    service = null;
}

Hello Anton,

thanks for your reply.

What I did not really like with it is to always create a new instance of the DataService.
So I started some more research in the DA source code and found the IDataAbstractLocalServiceAccess interface. When setting the LinqLocalDataAdapter’s ServiceName property I would expect that the ServiceInstance property already returns an instance of the DataService. So I tried the following solution, which seems to be working correctly.

        var Service = (DataAbstractService)linqLocalDataAdapter.ServiceInstance;
        Service.Connection.BeginTransaction();
        try
        {
            .. so some data processing
            Service.Connection.CommitTransaction();
        }
        catch (Exception)
        {
            Service.Connection.RollbackTransaction();
            throw;
        }

What is your opinion about this? Can it be done this way?

Thanks,
Georg

Hello

Yes this approach will work as well. In this case service instance is instantiated and activated during 1st access to the .ServiceInstance property.