3

I'm trying to do the most simple thing and apply a transaction to the CreatePortal method.

If I'm using TransactionScope - It promotes to DTC for some unknown reason - BAD.

using (var ts = new TransactionScope())
{
    var portalController = new PortalController();

    var portalId =
        portalController.CreatePortal(
                    "TESTTESTTEST",
                    string.Empty,
                    string.Empty,
                    "TESTTESTTEST_" + new Random().Next(999999),
                    UserController.GeneratePassword(),
                    "TESTTESTTEST_" + new Random().Next(999999) + "@something.com",
                    string.Empty,
                    string.Empty,
                    Globals.ApplicationMapPath + "/Portals/_default/",
                    "Default Website.template",
                    "Portals/TESTTEST",
                    "TESTTESTTEST",
                    string.Empty,
                    string.Empty,
                    false);

    ts.Complete();
}

If I'm using DataProvider.Instance().GetTransaction and performs RollbackTransaction at the end - IT DOES NOT ROLLBACK, which means that the transaction didn't even work.

var t = Data.DataProvider.Instance().GetTransaction();

var portalController = new PortalController();

var portalId =
    portalController.CreatePortal(
                "TESTTESTTEST",
                string.Empty,
                string.Empty,
                "TESTTESTTEST_" + new Random().Next(999999),
                UserController.GeneratePassword(),
                "TESTTESTTEST_" + new Random().Next(999999) + "@something.com",
                string.Empty,
                string.Empty,
                Globals.ApplicationMapPath + "/Portals/_default/",
                "Default Website.template",
                "Portals/TESTTEST",
                "TESTTESTTEST",
                string.Empty,
                string.Empty,
                false);

Data.DataProvider.Instance().RollbackTransaction(t);

So, How to use transactions in DotNetNuke?

Eran Betzalel
  • 4,105
  • 3
  • 38
  • 66

2 Answers2

1

When you create a transaction through the GetTransaction method, the DataProvider just creates a new connection and gives you the transaction. You would then need to manually use that transaction to perform whatever action you're going to take against the database. There isn't a way to pass that transaction so that it gets used by, for example, CreatePortal, or any other built-in DNN function. That functionality appears to be just for any additional database access you might make.

In terms of how to wrap a call from the DNN core in a transaction, I don't think you can. The cleanest solution I know to recommend (which, unfortunately, still isn't very clean) is to manually call the stored procedure, using a transaction, rather than going through the controller class.

What's your use case, maybe I can recommend a solution that solves the problem some other way...

bdukes
  • 152,002
  • 23
  • 148
  • 175
  • My main problem is using DNN entities and my LINQ entities inside a single transaction. The problem shown above is just an obstacle toward the solution of my bigger problem. – Eran Betzalel Sep 26 '09 at 03:36
  • Anyway, how come DNN 5.X does not support `TransactionScope`?! – Eran Betzalel Sep 26 '09 at 03:37
  • @Eran - I think transaction scope just wasnt conceved of at the time dnn was first built - I for one would love to see DNN support transaction scope but i imagine that its a matter of manpower on the part of the core team to get it into a future version - of course you could always write your own data provider and support it that way - we sort of did in a strange way – braindice Dec 01 '10 at 21:57
1

I was able to use Transactions in DNN with the help of this article.

It doesn't apply to your case, but will help others trying to leverage transactions in DNN.

roman m
  • 26,012
  • 31
  • 101
  • 133