Showing posts with label complex. Show all posts
Showing posts with label complex. Show all posts

Sunday, March 25, 2012

Cleaning up after myself

How should I go about releasing any objects I've created? I've got a very complex CLR stored proc that creates a dataset, many sqlcommand objects, an adapter, etc, etc. It's used to evaluate high school transcripts for completion of the core subjects. It's a very complex SP that takes a single SSN as a parameter.

Right now it works as expected when I call it from a query window in SQL Mgmt Studio, I can stack 39 calls in a row like:

exec usp_Evaluate_eTranscript '011...'
exec usp_Evaluate_eTranscript '012...'
...
exec usp_Evaluate_eTranscript '039...'

And if I build a little test harness in VB.Net calling it in a populated dataset For ... Next loop, it works just fine for the 39 valid values I have.

But, when I build a cursor in a query window in MgmtStudio I get some strange behavior, that just doesn't really make sense. I'm getting a foreign key violation and I'm not even trying to insert any rows, just updating existing ones.

So, the only possible cause that I haven't eliminated yet is that some objects are still hanging around when the cursor tries to call the SP again.

It's declared:

<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub usp_Evaluate_eTranscript(ByVal SSN As String)

And exits with the standard End Sub.

Is there some way I can help the garbage collector along? Kick it into action? Maybe some

Set sqlcommand = Nothing or
sqlcommand.Dispose

dataset.Clear or
dataset.Dispose or somesuch thing?

I'm closing the context connection immediately after each time I use it (I have to so the next object can work).

? You don't have to worry about releasing objects. That's what the garbage collector is for... But it sounds like you're not properly closing your connections. I recommend wrapping all connections in a "Using" block: Using conn As New SqlConnection(connection_string) '... do what you need to do End Using This will automatically call the Dispose() method on the connection when your code is done with it, which will close the connection immediately -- instead of waiting for the GC to come along and finalize the object... And it frees you from having to remember to call Close() all the time. -- Adam MachanicPro SQL Server 2005, available nowhttp://www..apress.com/book/bookDisplay.html?bID=457-- ""R3dD0g"@.discussions.microsoft.com" <"=?UTF-8?B?UjNkRDBn?="@.discussions.microsoft.com> wrote in message news:f9ac67cf-3bcc-4519-86c7-1b44c648ec1f@.discussions.microsoft.com... How should I go about releasing any objects I've created? I've got a very complex CLR stored proc that creates a dataset, many sqlcommand objects, an adapter, etc, etc. It's used to evaluate high school transcripts for completion of the core subjects. It's a very complex SP that takes a single SSN as a parameter. Right now it works as expected when I call it from a query window in SQL Mgmt Studio, I can stack 39 calls in a row like: exec usp_Evaluate_eTranscript '011...'exec usp_Evaluate_eTranscript '012...'...exec usp_Evaluate_eTranscript '039...' And if I build a little test harness in VB.Net calling it in a populated dataset For ... Next loop, it works just fine for the 39 valid values I have. But, when I build a cursor in a query window in MgmtStudio I get some strange behavior, that just doesn't really make sense. I'm getting a foreign key violation and I'm not even trying to insert any rows, just updating existing ones. So, the only possible cause that I haven't eliminated yet is that some objects are still hanging around when the cursor tries to call the SP again. It's declared: <Microsoft.SqlServer.Server.SqlProcedure()> _Public Shared Sub usp_Evaluate_eTranscript(ByVal SSN As String) And exits with the standard End Sub. Is there some way I can help the garbage collector along? Kick it into action? Maybe some Set sqlcommand = Nothing orsqlcommand.Dispose dataset.Clear ordataset.Dispose or somesuch thing? I'm closing the context connection immediately after each time I use it (I have to so the next object can work).|||

I am wrapping all the DB calls inside a Using conn as New SQLConnection ... End Using, additionally, I'm specifically opening and closing the connection inside the using. When I forgot to close it there were problems - I'm hitting the database in about 8 different ways and times.

|||

Avoid calling the Dispose() method.

There seems to be a lot of confusion about the SqlConnection.Close() and SqlConnection.Dispose() methods.

For SqlConnection objects, 'closing' a connection simply returns it to the connection pool - the connection is not really closed. This behavior is desirable. Opening and closing connections is a processor intensive task, which is why connection pooling is so important for writing scalable applications. If you call Dispose() on a connection object, it is first returned to the connection pool. This is because the Dispose() method includes a call to the Close() method. Once the connection object is back in the pool, it releases all unmanaged resources (i.e., the database handle). By releasing its database handle, the connection is rendered useless, and is simply kicked out of the connection pool.

Calling Dispose() effectively destroys the purpose of connection pooling, which is to promote connection reuse. It is hard to imagine a valid reason for calling Dispose() at all. If you really don't want to use connection pooling, you can simply disable it in your connection string (easier to turn it back on from here if you change your mind later). If you want to cleanup after you're done using a connection, simply call the Close() method. The connection is returned to the connection pool, and will be available for reuse next time you application calls the Open() method.

In general, if an object provides both Open() and Close() methods, then it should be obvious the designers intended the pair to be used used together. If you 'open' an object, then logically you would want to 'close' it after you're done.

I hope this helps clear some of the confusion surrounding Close() and Dispose(). For more information, please consult the .NET Framework documentation.

|||

CyberGuru wrote:

Calling Dispose() effectively destroys the purpose of connection pooling

What make you think that a disposed connection does not remain available in the connection pool? All the available documentation would suggest that it does (e.g.:http://msdn2.microsoft.com/en-us/library/8xx3tyca.aspx), and all tests I've run would seem to indicate the same. Do you have any evidence to the contrary?

CyberGuru wrote:

In general, if an object provides both Open() and Close() methods, then it should be obvious the designers intended the pair to be used used together. If you 'open' an object, then logically you would want to 'close' it after you're done.

In most cases, "Close" methods simply provide a facade for disposition. They're generally meant to make folks who like Open/Close symmetry feel comfortable, not to provide clean-up mechanism that differs from Dispose. Designing a Close method that is meant to be called instead of the Dispose method for a class that implements IDisposable would probably be considered a design error by most folks, particularly given that it interferes with use of the using statement.

sqlsql

Wednesday, March 7, 2012

Checkpoints

Hi,
i have a complex database. If a user (database is set to single user)
make changes, and the user want do redo these changes, i need a kind of
manual setted checkpoints. Such a checkpoint is set before changes
made.
The idea is, to set a checkpoint, (store all checkpoints), and when its
necassary you can revert to a checkpoint.
Is this possible or is there another technique to realize this?
Thank you in advance
AlexSQL Server has "database snapshot" that you could potentially use. You can r
estore from a database
snapshot. But there can only exist *one* snapshot of a database if you want
to restore from a (that)
snapshot.
But perhaps it is much cleaner to begin a transaction and rollback?
There's no built-in scheme where many users has many snapshots and you want
to revert to some
certain snapshot for one of the users without affecting the other users (unl
ess each user have their
own database). How would SQL Server understand what data that belongs to use
r A and revert only that
data? Not to speak about relationships between tables.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"alex" <earthquake.de@.freenet.de> wrote in message
news:1157460616.313916.180170@.d34g2000cwd.googlegroups.com...
> Hi,
> i have a complex database. If a user (database is set to single user)
> make changes, and the user want do redo these changes, i need a kind of
> manual setted checkpoints. Such a checkpoint is set before changes
> made.
> The idea is, to set a checkpoint, (store all checkpoints), and when its
> necassary you can revert to a checkpoint.
> Is this possible or is there another technique to realize this?
> Thank you in advance
> Alex
>|||Every change which is made, is encapsulated by an transaction and in
errorcase undone by the rollback or if success with commit.
So i want to set a checkpoint at start of editing the tables, and if
the user want to
undo his changes (created new datarows etc.) so the user can go to the
checkpoint, created at start editing.
So it is a single user instance all changes since last checkpoint
should be reverted.
A case of "higher level transaction"
Alex
Tibor Karaszi schrieb:
[vbcol=seagreen]
> SQL Server has "database snapshot" that you could potentially use. You can
restore from a database
> snapshot. But there can only exist *one* snapshot of a database if you wan
t to restore from a (that)
> snapshot.
> But perhaps it is much cleaner to begin a transaction and rollback?
> There's no built-in scheme where many users has many snapshots and you wan
t to revert to some
> certain snapshot for one of the users without affecting the other users (u
nless each user have their
> own database). How would SQL Server understand what data that belongs to u
ser A and revert only that
> data? Not to speak about relationships between tables.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "alex" <earthquake.de@.freenet.de> wrote in message
> news:1157460616.313916.180170@.d34g2000cwd.googlegroups.com...|||<DIV>"Tibor Karaszi"
<tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in message
news:e7Mln5O0GHA.772@.TK2MSFTNGP05.phx.gbl...</DIV>> SQL Server has "database
snapshot" that you could potentially use. You can restore from a database[vbcol=seagreen]
> snapshot. But there can only exist *one* snapshot of a database if you
> want to restore from a (that) snapshot.
> But perhaps it is much cleaner to begin a transaction and rollback?
> There's no built-in scheme where many users has many snapshots and you
> want to revert to some certain snapshot for one of the users without
> affecting the other users (unless each user have their own database). How
> would SQL Server understand what data that belongs to user A and revert
> only that data? Not to speak about relationships between tables.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "alex" <earthquake.de@.freenet.de> wrote in message
> news:1157460616.313916.180170@.d34g2000cwd.googlegroups.com...
A "marked transaction" can be your checkpoint. They are all stored in
msdb..logmarkhistory. You can restore a database backup and apply the
transaction log up through a marked transaction.
Using Marked Transactions (Full Recovery Model)
http://msdn2.microsoft.com/en-us/library/ms187014.aspx
David|||Often, the quality of the responses received is related to our ability to
'bounce' ideas off of each other. In the future, to prevent folks from
wasting time on already answered questions, please:
Don't post to multiple newsgroups. Choose the one that best fits your
question and post there. Only post to another newsgroup if you get no answer
in a day or two (or if you accidentally posted to the wrong newsgroup), and
indicate that you've already posted elsewhere.
If you really think that a question belongs into more than one newsgroup,
then use your newsreader's capability of multi-posting, i.e., posting one
occurrence of a message into several newsgroups at once. If you multi-post
appropriately, answers 'should' appear in all the newsgroups. Folks
responding in different newsgroups will see responses from each other, even
if the responses were posted in a different newsgroup.
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"alex" <earthquake.de@.freenet.de> wrote in message
news:1157460616.313916.180170@.d34g2000cwd.googlegroups.com...
> Hi,
> i have a complex database. If a user (database is set to single user)
> make changes, and the user want do redo these changes, i need a kind of
> manual setted checkpoints. Such a checkpoint is set before changes
> made.
> The idea is, to set a checkpoint, (store all checkpoints), and when its
> necassary you can revert to a checkpoint.
> Is this possible or is there another technique to realize this?
> Thank you in advance
> Alex
>

Checkpoints

Hi,
i have a complex database. If a user (database is set to single user)
make changes, and the user want do redo these changes, i need a kind of
manual setted checkpoints. Such a checkpoint is set before changes
made.
The idea is, to set a checkpoint, (store all checkpoints), and when its
necassary you can revert to a checkpoint.
Is this possible or is there another technique to realize this?
Thank you in advance
AlexSQL Server has "database snapshot" that you could potentially use. You can restore from a database
snapshot. But there can only exist *one* snapshot of a database if you want to restore from a (that)
snapshot.
But perhaps it is much cleaner to begin a transaction and rollback?
There's no built-in scheme where many users has many snapshots and you want to revert to some
certain snapshot for one of the users without affecting the other users (unless each user have their
own database). How would SQL Server understand what data that belongs to user A and revert only that
data? Not to speak about relationships between tables.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"alex" <earthquake.de@.freenet.de> wrote in message
news:1157460616.313916.180170@.d34g2000cwd.googlegroups.com...
> Hi,
> i have a complex database. If a user (database is set to single user)
> make changes, and the user want do redo these changes, i need a kind of
> manual setted checkpoints. Such a checkpoint is set before changes
> made.
> The idea is, to set a checkpoint, (store all checkpoints), and when its
> necassary you can revert to a checkpoint.
> Is this possible or is there another technique to realize this?
> Thank you in advance
> Alex
>|||Every change which is made, is encapsulated by an transaction and in
errorcase undone by the rollback or if success with commit.
So i want to set a checkpoint at start of editing the tables, and if
the user want to
undo his changes (created new datarows etc.) so the user can go to the
checkpoint, created at start editing.
So it is a single user instance all changes since last checkpoint
should be reverted.
A case of "higher level transaction"
Alex
Tibor Karaszi schrieb:
> SQL Server has "database snapshot" that you could potentially use. You can restore from a database
> snapshot. But there can only exist *one* snapshot of a database if you want to restore from a (that)
> snapshot.
> But perhaps it is much cleaner to begin a transaction and rollback?
> There's no built-in scheme where many users has many snapshots and you want to revert to some
> certain snapshot for one of the users without affecting the other users (unless each user have their
> own database). How would SQL Server understand what data that belongs to user A and revert only that
> data? Not to speak about relationships between tables.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "alex" <earthquake.de@.freenet.de> wrote in message
> news:1157460616.313916.180170@.d34g2000cwd.googlegroups.com...
> > Hi,
> > i have a complex database. If a user (database is set to single user)
> > make changes, and the user want do redo these changes, i need a kind of
> >
> > manual setted checkpoints. Such a checkpoint is set before changes
> > made.
> >
> > The idea is, to set a checkpoint, (store all checkpoints), and when its
> > necassary you can revert to a checkpoint.
> > Is this possible or is there another technique to realize this?
> >
> > Thank you in advance
> > Alex
> >|||<DIV>"Tibor Karaszi"
<tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in message
news:e7Mln5O0GHA.772@.TK2MSFTNGP05.phx.gbl...</DIV>> SQL Server has "database
snapshot" that you could potentially use. You can restore from a database
> snapshot. But there can only exist *one* snapshot of a database if you
> want to restore from a (that) snapshot.
> But perhaps it is much cleaner to begin a transaction and rollback?
> There's no built-in scheme where many users has many snapshots and you
> want to revert to some certain snapshot for one of the users without
> affecting the other users (unless each user have their own database). How
> would SQL Server understand what data that belongs to user A and revert
> only that data? Not to speak about relationships between tables.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "alex" <earthquake.de@.freenet.de> wrote in message
> news:1157460616.313916.180170@.d34g2000cwd.googlegroups.com...
>> Hi,
>> i have a complex database. If a user (database is set to single user)
>> make changes, and the user want do redo these changes, i need a kind of
>> manual setted checkpoints. Such a checkpoint is set before changes
>> made.
>> The idea is, to set a checkpoint, (store all checkpoints), and when its
>> necassary you can revert to a checkpoint.
>> Is this possible or is there another technique to realize this?
A "marked transaction" can be your checkpoint. They are all stored in
msdb..logmarkhistory. You can restore a database backup and apply the
transaction log up through a marked transaction.
Using Marked Transactions (Full Recovery Model)
http://msdn2.microsoft.com/en-us/library/ms187014.aspx
David|||Often, the quality of the responses received is related to our ability to
'bounce' ideas off of each other. In the future, to prevent folks from
wasting time on already answered questions, please:
Don't post to multiple newsgroups. Choose the one that best fits your
question and post there. Only post to another newsgroup if you get no answer
in a day or two (or if you accidentally posted to the wrong newsgroup), and
indicate that you've already posted elsewhere.
If you really think that a question belongs into more than one newsgroup,
then use your newsreader's capability of multi-posting, i.e., posting one
occurrence of a message into several newsgroups at once. If you multi-post
appropriately, answers 'should' appear in all the newsgroups. Folks
responding in different newsgroups will see responses from each other, even
if the responses were posted in a different newsgroup.
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"alex" <earthquake.de@.freenet.de> wrote in message
news:1157460616.313916.180170@.d34g2000cwd.googlegroups.com...
> Hi,
> i have a complex database. If a user (database is set to single user)
> make changes, and the user want do redo these changes, i need a kind of
> manual setted checkpoints. Such a checkpoint is set before changes
> made.
> The idea is, to set a checkpoint, (store all checkpoints), and when its
> necassary you can revert to a checkpoint.
> Is this possible or is there another technique to realize this?
> Thank you in advance
> Alex
>

Sunday, February 19, 2012

Checking KPI value for divide-by-zero

We've got some potentially complex KPI value definitions, which can involve division of two cell values. I want to put in a check to make sure we don't do a divide-by-0. If I do, is there any performance implication due to the calculation of the denomerator happening twice (once in the first IIf clause, again in the actual value). For example, if I have a value that's essentially:

[Measure1] / (Aggregate([A Set], [Measure 2])

and I want to make it

IIf( (Aggregate([A Set], [Measure 2]) > 0, [Measure1] / (Aggregate([A Set], [Measure 2]), Null)

Is there a performance hit for doing this? Does the aggregate happen twice? Is there a smarter way of doing this check?

Yes, Aggregate will happen twice. The best way to handle it is

CREATE HIDDEN AggASet = Aggregate([A Set], [Measure 2])

and then you can say

IIf( AggASet > 0, [Measure1] / AggASet, Null)

|||Thanks Mosha. But can I specify a composite expression like that in a KPI value expression?|||No - the CREATE HIDDEN statement should be somewhere inside the MDX Script.|||Shoot, that's what I was afraid you were going to say. The problem is that these KPIs are being generated by a GUI tool, and so far we've done everything inside the KPI definition itself, without having to muck with the MDX script. Changing that is going to involve some major code changes, sadly.

Is there any other way to handle this condition gracefully?|||

Your choices as I see them are:

1. Do nothing - take perf hit, and hope that in the next version query optimizer will automatically detect such expressions and will do the "right thing"

2. Change your logic to add hidden calculated measures to MDX Script

3. Don't check for 0 in denominator and let users see 1.#INF when such division occurs

4. Create another KPI which will hold the Aggregate expression and reference its through KPIValue function from your KPI expression. This way you won't need to change MDX Script. I am not sure, however, if it is possible to create hidden KPIs. So the drawback is that you will get an extra 'junk' KPI.

5. Something else ?

HTH,

Mosha (http://www.mosha.com/msolap)