Showing posts with label appear. Show all posts
Showing posts with label appear. Show all posts

Friday, March 30, 2012

More on mysterious data loss in mssql2k - a possible solution?

Hi All,

Further to my previous long-winded question about a situation in which
we appear to be mysteriously losing data from our mssql2k server.

We discovered an update statement, in the stored procedure we believe
is at fault, after which no error check was being performed.

Under certain conditions, this update is fired against the same record
in the same table as the immediately preceding update statement within
the transaction. We are now suspecting that under some circumstances,
these two updates get into a locking conflict that is eventually
forcing the transaction to be rolled back.

However, I'm still left with three questions.

1) Where an update in a transaction gets locked, and an error isn't
tested immediately afterwards (ie no 'IF @.@.Error<>0' test is made),
would the transaction proceed as normal?

2) Most critically, would statements in the stored procedure that
appear after the COMMIT TRAN statement also be executed, even if an
unresolved lock existed within the transaction?

3) Assuming that (2) does happen, would a SELECT made on another
connection with a 'WITH(NOLOCK)' locking hint be able to see the
changes made in the locked transaction even if the server is set to
READ COMMITTED, and the SELECT takes place some time after the COMMIT
TRAN is issued? More to the point, given (2), how long would the
locked transaction survive before being rolled back after the COMMIT
TRAN has been issued? Is it possible that the COMMIT TRAN takes place,
the transaction is flagged for potential rollback while a lock
resolution is attempted, the stored procedure exists as though
everything was fine, a subsequent SELECT (ie performed as one of the
next operations in the same application) using WITH(NOLOCK) 'sees' the
changes made by the transaction, reinforcing the impression that the
transaction succeeded, and then at some point thereafter the lock is
determined to be unresolvable and the transaction is rolled back,
making it seem as though the data disappeared, even though it had been
SELECTable via a different connection to the server?

Thanks, by the way, to Simon and Erland for your advice on my previous
questions about this problem.

Much warmth,

M WellsM Wells (planetthoughtful@.gmail.com) writes:
> We discovered an update statement, in the stored procedure we believe
> is at fault, after which no error check was being performed.

Short recap: when an error happens in a stored prcoedures, one three
things can happen depending on the error:

1) The statement is terminated, and @.@.error is set. The transaction is
not rolled back.
2) The execution of the stored procedure is terminated and @.@.error is
set. No rollback occurs.
3) The entire batch is aborted. Transactions is rolled back.

More details on http://www.sommarskog.se/error-handling-I.html.

> Under certain conditions, this update is fired against the same record
> in the same table as the immediately preceding update statement within
> the transaction. We are now suspecting that under some circumstances,
> these two updates get into a locking conflict that is eventually
> forcing the transaction to be rolled back.

No, unless you engage in wierd arrangments with linked servers that
results in loopback connections, two statements in the same stored
procedure cannot get in conflicts with each other. What happens if
you have:

BEGIN TRANSACTION

UPDATE tbl1 ...

UPDATE tbl2 ...

UPDATE tbl3 ...

COMMIT TRANSACTION

and the UPDATE on tbl2 fails with a statement-terminating error (for
instance PK violation, NOT NULL violation), and there is no error-handling,
is that the effect of the updates on tbl1 and tbl3 will be persisted
when the transaction is committed.

> 1) Where an update in a transaction gets locked, and an error isn't
> tested immediately afterwards (ie no 'IF @.@.Error<>0' test is made),
> would the transaction proceed as normal?

Yes. (But you can't really say that an "update gets locked".)

> 2) Most critically, would statements in the stored procedure that
> appear after the COMMIT TRAN statement also be executed, even if an
> unresolved lock existed within the transaction?

Yes. (Save again that there are no "unresolved locks". All locks are
released as the transaction commits.)

> 3) Assuming that (2) does happen, would a SELECT made on another
> connection with a 'WITH(NOLOCK)' locking hint be able to see the
> changes made in the locked transaction even if the server is set to
> READ COMMITTED, and the SELECT takes place some time after the COMMIT
> TRAN is issued?

If you read data WITH (NOLOCK), you may be reading dirty data. It doesn't
matter what settings you have elsewhere. Query hints wins over anything
else. If you confirmation page reads with NOLOCK, you absolutely must
change that. Else you are not confirming data.

> More to the point, given (2), how long would the
> locked transaction survive before being rolled back after the COMMIT
> TRAN has been issued?

Assuming that there are no nested transactions, therre is no transaction
and no locks around after COMMIT.

But if the COMMIT for some reason is not executed, for instance because
the query is cancelled, or the error terminates the stored procedure,
so that the transaction lives on, then the transaction will continue to
live until the process disconnects, at which points a fat rollback will
set in. (Actually with connection pooling it's takes a little while more
before it happens.)

> Is it possible that the COMMIT TRAN takes place,
> the transaction is flagged for potential rollback while a lock
> resolution is attempted, the stored procedure exists as though
> everything was fine, a subsequent SELECT (ie performed as one of the
> next operations in the same application) using WITH(NOLOCK) 'sees' the
> changes made by the transaction, reinforcing the impression that the
> transaction succeeded, and then at some point thereafter the lock is
> determined to be unresolvable and the transaction is rolled back,

Again, there is no lock to resolve, nor that can be deemd to be
unresolved. I'm sorry to nag this point.

Judging from your description, you have two problems: 1) You read
with NOLOCK. 2) You fail to commit your transaction in some situations.
The missing @.@.error check is probably not the main problem about the false
confirmations. More likely you fail to handle procedure-aborting errors or
command timeouts, since these are the cases where you most easily can
go wrong about this.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Friday, March 23, 2012

Months appear in alphabetical order in Excel 2003 pivot table

I have a SSAS 2005 cube attached to a pivot table in Excel 2003. When the months are dragged to the column area, they appear in alphabetical order (Apr, Aug, etc) instead of Jan, Feb, Mar.

The same cube displays the months properly in Visual Studio.

I know I can re-arrange the columns manual but that sort of ruins the point of OLAP if you have to do that every time.

Any pointers would be appreciated.

Make sure you have set the OrderBy property to Key (assuming you have a numeric key column for the month) and set the Type property to Months on the attribute and Time on the dimension. Setting the Type properties will also enable date range filters in Excel 2007 on an attribute of type Date.|||

Thanks for the response.

Unfortunately, all of the properties were already set as you ask.

One thing I am unclear on is the meaning of the term "key" in the OrderBy property. How do I know if the attribute has a numeric key?

Also, this is Excel 2003, not 2007.

|||

The answer is to create another attribute containing the values 1 - 12 (representing the various months) and set the Month variable OrderBy property to AttributeKey and OrderByAttribute to contain the name of the new attribute (you will have to relate the new attribute to the month attribute by expanding the month attribute and dragging the new attribute to <new attribute relationship> under the month attribute.

Works like a charm but I don't see why you should have to do it when Month has already be defined as a time type of attribute.

|||

john shahan wrote:

Works like a charm but I don't see why you should have to do it when Month has already be defined as a time type of attribute.

It sounds like you are using the string of the month name as the attribute key, as such SSAS is just sorting them alphabetically. If you had set your key column to be an actual date or a number it would have sorted using that. (the name and key don't have to be the same column). SSAS works with multiple languages so it will not automatically true to parse month names and sort them in a date order.

Months appear in alphabetical order in Excel 2003 pivot table

I have a SSAS 2005 cube attached to a pivot table in Excel 2003. When the months are dragged to the column area, they appear in alphabetical order (Apr, Aug, etc) instead of Jan, Feb, Mar.

The same cube displays the months properly in Visual Studio.

I know I can re-arrange the columns manual but that sort of ruins the point of OLAP if you have to do that every time.

Any pointers would be appreciated.

Make sure you have set the OrderBy property to Key (assuming you have a numeric key column for the month) and set the Type property to Months on the attribute and Time on the dimension. Setting the Type properties will also enable date range filters in Excel 2007 on an attribute of type Date.|||

Thanks for the response.

Unfortunately, all of the properties were already set as you ask.

One thing I am unclear on is the meaning of the term "key" in the OrderBy property. How do I know if the attribute has a numeric key?

Also, this is Excel 2003, not 2007.

|||

The answer is to create another attribute containing the values 1 - 12 (representing the various months) and set the Month variable OrderBy property to AttributeKey and OrderByAttribute to contain the name of the new attribute (you will have to relate the new attribute to the month attribute by expanding the month attribute and dragging the new attribute to <new attribute relationship> under the month attribute.

Works like a charm but I don't see why you should have to do it when Month has already be defined as a time type of attribute.

|||

john shahan wrote:

Works like a charm but I don't see why you should have to do it when Month has already be defined as a time type of attribute.

It sounds like you are using the string of the month name as the attribute key, as such SSAS is just sorting them alphabetically. If you had set your key column to be an actual date or a number it would have sorted using that. (the name and key don't have to be the same column). SSAS works with multiple languages so it will not automatically true to parse month names and sort them in a date order.