Showing posts with label transactions. Show all posts
Showing posts with label transactions. Show all posts

Monday, March 19, 2012

CLR Triggers, Linq, and Transactions

I'm trying convert my working CLR Trigger to a Linq-based implementation, but I don't seem to be able to get Linq to cooperate with the Trigger's context transaction (a local SubordinateTransaction with IsolationLevel="ReadCommitted").

For brevity's sake I've boiled down my scenario to a bare minimum: A SQL table [Test] with three columns (int Manual, int Automatic, IDENTITY int ID) needs any external updates on its [Manual] column to be applied to its [Automatic] column (with an additional sign change) through a Trigger.

A fairly direct translation of my non-Linq CLR Trigger to Linq yields the following code (the [Trigger_TestLinQs] SQL table has been modified to point to the Trigger's "INSERTED" table, [TestLinQs] points to the actual SQL table):

Code Snippet

[SqlTrigger(Name = "LinQTrigger", Target = "dbo.TestLinQ", Event = "FOR INSERT, UPDATE")]
public static void LinQTrigger()
{
SqlTriggerContext triggerContext = SqlContext.TriggerContext;
if (triggerContext.TriggerAction == TriggerAction.Insert || triggerContext.TriggerAction == TriggerAction.Update) {
//using (TransactionScope scope = new TransactionScope(Transaction.Current)) { /* err 6549 */
using (SqlConnection connection = new SqlConnection("context connection=true")) {
connection.Open();
using (MyDataContext context = new MyDataContext(connection)) {
List<int[]> updates = new List<int[]>();

foreach (Trigger_TestLinQ test in (from t in context.Trigger_TestLinQs select t)) { /* err 6522 */
if (test.Manual != null && triggerContext.IsUpdatedColumn(0))
updates.Add(new int[] { test.ID, -test.Manual.Value });
}

foreach (int[] update in updates) {
dbo.TestLinQ test = (from t in context.TestLinQs where t.ID == update[0] select t).Single<dbo.TestLinQ>();
test.Automatic = update[1];
}

context.SubmitChanges();
}
}
// scope.Complete();
//}
}
}


Running this on SQL Server 2005 / .NET 3.5 beta2 produces an Error 6522 "Cannot enlist in the transaction because a local transaction is in progress on the connection." in the line executing the Linq query because the Linq DataContext apparently insists on opening a transaction.
The recommended pattern for DataContext query operations - running them inside a TransactionScope as indicated by the commented-out sections - unfortunately leads to a different Error 6549 in the TransactionScope constructor complaining about a missing MSDTC. And there shouldn't be a need for a distributed transaction here, should there?
Using different TransactionScope constructors (TransactionScopeOptions.Required, TransactionOptions(){IsolationLevel=ReadCommitted}) did not make any difference, either.

What am I doing wrong here? Is there any way for the Linq DataContext to attach to the CLR Trigger's ambient transaction?
Interesting scenario! I won't ask why you'd want to use LINQ in this way :-)

Anyway, first of all I'm a little bit surprised that LINQ works at all within SQLCLR. I thought it would not work with a Context Connection at all.

It looks like, at the moment, that LINQ does not work very well with transactions when running inside SQLCLR (in fact it looks it doesn't work at all). So at the moment, I don't think there is anything you can do.

Niels
|||Hi nielsb,

thanks for destroying my last hope :-).

You're probably right in that presently LinQ DataContexts don't fully - or maybe even not at all - interoperate with a Context Connection.
I'm wondering why that is the case, however, since it's basically just a case of O/R mapping - which can apparently be coaxed to work with dynamic SQL Tables - and of attaching to a local System.Transactions.Transaction.

Interestingly enough you can make the LinQ DataContext work for the SELECT statement embedded in the trigger by explicitely opening a new Transaction on the SqlConnection. You can't effectively perform any updates, though, because the DataContext's change tracking mechanism doesn't kick in.

As to why I'd want to use LinQ in this context - it's the logical next step once you've been lured away from the glory of T-SQL into the murky depths of SQL Server CLR integration. :-]

Tuesday, February 14, 2012

client freezes

NEWBIE...We run a custom application that sends
transactions to an SQL server over a WAN. On busy days, at
peak periods, many of the client work stations lock up and
freeze. If we leave it alone, the clients will resume
activity after 15-20 minutes. If we re-set the SQL
database, it unlocks all of the client pc's and resumes.
It seems like there is too much information being sent to
SQL and it cant handle the volume'
What could cause this and is there a way to adjust?Tom
Blocking, locks,bottlenecks.Have you looked at Performance Monitor?
CPU,Memory usage.
"Tom" <tomcdp@.aol.com> wrote in message
news:a32b01c4794c$8a8e3f30$a501280a@.phx.gbl...
> NEWBIE...We run a custom application that sends
> transactions to an SQL server over a WAN. On busy days, at
> peak periods, many of the client work stations lock up and
> freeze. If we leave it alone, the clients will resume
> activity after 15-20 minutes. If we re-set the SQL
> database, it unlocks all of the client pc's and resumes.
> It seems like there is too much information being sent to
> SQL and it cant handle the volume'
> What could cause this and is there a way to adjust?|||What do you mean by lock up and freeze? Are you saying the entire PC locks
up, or just a SQL Server application running on the client. I can't think of
a server side reason that would cause the entire client PC to lock up like
this. Server side blocking, waits, etc... would not be likley to do it.
If the entire PC is freezing, can you check the CPU utilization as it's
happening? Is it 100%?
--
Brian
"Tom" <tomcdp@.aol.com> wrote in message
news:a32b01c4794c$8a8e3f30$a501280a@.phx.gbl...
> NEWBIE...We run a custom application that sends
> transactions to an SQL server over a WAN. On busy days, at
> peak periods, many of the client work stations lock up and
> freeze. If we leave it alone, the clients will resume
> activity after 15-20 minutes. If we re-set the SQL
> database, it unlocks all of the client pc's and resumes.
> It seems like there is too much information being sent to
> SQL and it cant handle the volume'
> What could cause this and is there a way to adjust?|||"Tom" <tomcdp@.aol.com> wrote in message
news:a32b01c4794c$8a8e3f30$a501280a@.phx.gbl...
> NEWBIE...We run a custom application that sends
> transactions to an SQL server over a WAN. On busy days, at
> peak periods, many of the client work stations lock up and
> freeze. If we leave it alone, the clients will resume
> activity after 15-20 minutes. If we re-set the SQL
> database, it unlocks all of the client pc's and resumes.
> It seems like there is too much information being sent to
> SQL and it cant handle the volume'
> What could cause this and is there a way to adjust?
Do you have database "auto grow" enabled? If so, is the db pausing to expand
the size of either the data or log portion? I've seen this setting cause
some odd client side behavior...
Steve

Sunday, February 12, 2012

Clicking on aggregations and displaying transactions?

Greetings SSAS Gurus,

Please please help?

How would I set up my SSAS cube so that when the user clicks on an aggregate, that the transactions that comprise that aggregate, are displayed?

Also, how do I control what is displayed, both in terms of field labels (e.g. labeling the data "Account Balance" instead of "acct_bal_amt", and data values (e.g. seeing "$1,234.56" instead of "1234.56").

Any guidance would be appreciated, even if at a minimum, it consisted of advising me what the topic is called in SSAS so that I can (try:) ) and do the research myself?

In anticipation and appreciation

email: gwithers at jhancock dot com

Greg Withers

In SSAS this concept is called DrillThrough. Depending on whether you use client which support Drillthrough actions (such as Excel 2007) - then it is trivial to define it in the cube. If you are writing the MDX query generation yourself, then take a look at DRILLTHROUGH statement.|||Mosha - Thanks for pointing me in the right direction. I personally prefer being taught to fish, rather than being fed and still not knowing how :)