I have a problem wit one place in my code where a change to a persistent objects attribute is not being saved. The code below is running in its own thread, the queries are working, but the line "subscription.LastEvent = newEvent" is not saving the lastEvent to the database. It starts off as null, in the debugger I can see the "newEvent" being assigned to "subscription.LastEvent", but next time the queries run (I have looked in the database) it is null.

I have tried surrounding it with a using (var transactionScope = Transaction.Open()) { subscription.LastEvent = newEvent; transactionScope.Complete(); } but that did not make any difference.

I assign the LastEvent in other palaces and it works, only in this case it doe not.

void ProcessEvents(object sender, DoWorkEventArgs e) {
            try {
                BackgroundWorker worker = sender as BackgroundWorker;
                DateTime lastEventProcessed = LastEventTimeStamp;
                while (true) {
                    if (worker.CancellationPending) {
                        e.Cancel = true;
                    using (Session.Open(BrokerDomain)) {
                        var subscriptions = from subscription in Query.All<Subscription>() where subscription.ApplicationName == ApplicationName select subscription;
                        foreach (Subscription subscription in subscriptions) {
                            IQueryable<Event> outstandingEvents = null;
                            if (subscription.LastEvent == null) {
                                outstandingEvents = from ev in Query.All<Event>() where ev.Topic == subscription.Topic orderby ev.TimeStamp ascending select ev;
                            } else {
                                outstandingEvents = from ev in Query.All<Event>() where ev.Topic == subscription.Topic && ev.TimeStamp > subscription.LastEvent.TimeStamp orderby ev.TimeStamp ascending select ev;
                            foreach (Event newEvent in outstandingEvents) {
                                eBroker.Fire(newEvent.Topic.Name, null, HandlerRestriction.None, this, null);
                                subscription.LastEvent = newEvent;
                e.Cancel = true;
            } catch (Exception ex) {
                Console.WriteLine("Error : " + ex.ToString()+" : "+ex.StackTrace);

This thread was imported from our support forum. The original discussion may contain more detailed answer.

asked Jan 05 '10 at 14:12

Tony's gravatar image


We'll try to reproduce the issue.

For now I can only recommend to try making transaction boundaries wider. E.g. run a transaction of the whole block inside using (Session.Open(BrokerDomain)) { ... }.

(Jan 05 '10 at 14:12) Alex Yakunin Alex%20Yakunin's gravatar image

One Answer:

Try to:

  • Send us SQL Server Profiler output for your case. If Subscription seems persisted there before the subsequent query, see if there are transaction rollbacks there, and what could lead to this.

  • Call Session.Demand().Persist() after assigning the value as well. This must ensure immediate flushing of cached changes.

Well, it seems there is some bug anyway, otherwise the behavior won't change.

So I just added this to our issue tracker: http://code.google.com/p/dataobjectsdot ... ail?id=570

answered Jan 05 '10 at 15:17

Alex%20Yakunin's gravatar image

Alex Yakunin

Note that query executed in foreach loop won't be re-executed after property change, i.e. property change won't affect on subsequent items returned by it. You must re-enter foreach loop to see the changes.

(Jan 05 '10 at 15:17) Alex Yakunin Alex%20Yakunin's gravatar image

Tony wrote: Putting the transaction on the whole block, as suggested, worked (should have tried that before raising this).

Using Postgress not SQL Server.

Thanks tony

(Jan 05 '10 at 15:17) Editor Editor's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview


Once you sign in you will be able to subscribe for any updates here



Asked: Jan 05 '10 at 14:12

Seen: 1,884 times

Last updated: Jan 05 '10 at 14:12

powered by OSQA