How should write static methods in a persistent class?

Working:

[NonTransactional]
    public static void CreateInstance()
    {
      using (Session.Open(Program.domain))
      using (TransactionScope transactionScope = Transaction.Open())
      {
        MyEntity entity = new MyEntity { Text = "1" };
      }
    }

Not working

[ActivateSession(true)]
    public static void CreateInstance2()
    {
      MyEntity entity = new MyEntity { Text = "2" };
    }

This throws an invalid operation exception : "Active Session is required for this operation. Use Session.Open(...) to open it."

Is there an attribute I'm missing?


Updated at 19.05.2010 8:05:01

To sum up : like discussed in the other post on TransactionalAttributes, static methods automatically erroneously have automatic Transactional attribute in 4.3. This behavior will change in next version, but for now you must apply [NonTransactional] to static methods and initialize session and transaction yourself.

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

asked May 18 '10 at 15:30

olorin's gravatar image

olorin
358848792

We'll check this ASAP.

(May 18 '10 at 15:30) Dmitri Maximov Dmitri%20Maximov's gravatar image

One Answer:

Alex (Xtensive) wrote:

Static methods can't be transactional:

"Transactional" means:

  • We can find a Session to activate. Currently we do this via ISessionBound interface, so we need an instance of ISessionBound for this. Generally, it can be passed by varous ways to a method, but we support just one: via "this" :) I.e. Session activation works only for ISessionBound instances.

  • We can create a transaction. So there sound be an active Session. If there is no active Session (and we didn't activate it on the previous step), we can't do this.

As you see, static methods don't allow us to implement the first part.

To workaround the issue, you can manually activate a Session and create a transaction there. Or forward all the job so some well-known Session-level service using Session.Services.Demand<t>(), etc.


psulek wrote:

To sum up : like discussed in the other post on TransactionalAttributes, static methods automatically erroneously have automatic Transactional attribute in 4.3. This behavior will change in next version, but for now you must apply [NonTransactional] to static methods and initialize session and transaction yourself.

Sounds strange to me that when i mark static method with attribute [NonTransactional], that i want NO transaction at all within this method, but your suggestion is to initialize session and "create trasnsaction", insane :-) This will be fixed soon, yes?

answered May 18 '10 at 19:11

Editor's gravatar image

Editor
46153156157

Definitely - we're working on this.

(May 18 '10 at 19:11) Alex Yakunin Alex%20Yakunin'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

Subscription:

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

Tags:

×573

Asked: May 18 '10 at 15:30

Seen: 2,124 times

Last updated: May 18 '10 at 15:30

powered by OSQA