Cache and SqlCacheDependency (ASP.NET MVC) - caching

We need to return subset of records and for that we use the following command:
using (SqlCommand command = new SqlCommand(
"SELECT ID, Name, Flag, IsDefault FROM (SELECT ROW_NUMBER() OVER (ORDER BY #OrderBy DESC) as Row, ID, Name, Flag, IsDefault FROM dbo.Languages) results WHERE Row BETWEEN ((#Page - 1) * #ItemsPerPage + 1) AND (#Page * #ItemsPerPage)",
connection))
I set a SqlCacheDependency declared like this:
SqlCacheDependency cacheDependency = new SqlCacheDependency(command);
But immediately after I run the command.ExecuteReader() instruction, the hasChanged base property of the SqlCacheDependency object becomes true although I did not change the result of the query in any way! And, because of this, the result of this query is not kept in cache.
HttpRuntime.Cache.Insert( cacheKey, list, cacheDependency, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(AppConfiguration.CacheExpiration.VeryLowActivity));
Is it because the command has 2 SELECT statements? Is it ROW_NUMBER()? If yes, is there any other way to paginate results?
Please help! After too many hours, a little will be greatly appreciated! Thank you

Running into the same issue and finding the same answers online without any help, I was reasearching the xml invalid subsicription response from profiler.
I found an example on msdn support site that had a slightly different order of code. When I tried it I realized the problem - Don't open your connection object until after you've created the command object and the cache dependency object. Here is the order you must follow and all will be good:
Be sure to enable notifications (SqlCahceDependencyAdmin) and run SqlDependency.Start first
Create the connection object
Create the command object and assign command text, type, and connection object (any combination of constructors, setting properties, or using CreateCommand).
Create the sql cache dependency object
Open the connection object
Execute the query
Add item to cache using dependency.
If you follow this order, and follow all other requirements on your select statement, don't have any permissions issues, this will work!
I believe the issue has to do with how the .NET framework manages the connection, specifically what settings are set. I tried overriding this in my sql command test but it never worked. This is only a guess - what I do know is changing the order immediately solved the issue.
I was able to piece it together from the following to msdn posts.
This post was one of the more common causes of the invalid subscription, and shows how the .Net client sets the properties that are in contrast to what notification requires.
https://social.msdn.microsoft.com/Forums/en-US/cf3853f3-0ea1-41b9-987e-9922e5766066/changing-default-set-options-forced-by-net?forum=adodotnetdataproviders
Then this post was from a user who, like me, had reduced his code to the simplest format. My original code pattern was similar to his.
https://social.technet.microsoft.com/Forums/windows/en-US/5a29d49b-8c2c-4fe8-b8de-d632a3f60f68/subscriptions-always-invalid-usual-suspects-checked-no-joy?forum=sqlservicebroker
Then I found this post, also a very simple reduction of the problem, only his was a simple issue - needing 2 part name for tables. In his case the suggestion resolved the issue. After looking at his code I noticed the main difference was waiting to open the connection object until AFTER the command object AND the dependency object were created. My only assumption is under the hood (I have not yet started reflector to check so only an assumption) the Connection object is opened differently, or order of events and command happen differently, because of this association.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/bc9ca094-a989-4403-82c6-7f608ed462ce/sql-server-not-creating-subscription-for-simple-select-query-when-using-sqlcachedependency?forum=sqlservicebroker
I hope this helps someone else in a similar issue.

Just a guess, but could it be because your SELECT statement doesn't have an ORDER BY clause?
If you don't specify an explicit ordering then it's possible for the query to return the results in any order each time it is run. Maybe this is causing the SqlCacheDependency object to think that the results have changed.
Try adding an ORDER BY clause:
SELECT ID, Name, Flag, IsDefault
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY #OrderBy DESC) AS Row,
ID, Name, Flag, IsDefault
FROM dbo.Languages
) AS results
WHERE Row BETWEEN ((#Page - 1) * #ItemsPerPage + 1) AND (#Page * #ItemsPerPage)
ORDER BY Row

i'm no expert on SqlCacheDependency, in fact, i found this question whilst looking for answers to my own issues with it! However, i believe the reason your SqlCacheDependency is not working is because your SQL contains a nested sub query.
Take a look at the documentation which lists what you can/can not use in your SQL: Creating a Query for Notification
"....The statement must not contain subqueries, outer joins, or self-joins....."
I also found some invaluable troubleshooting info from a guy at Redgate here: Using and Monitoring SQL 2005 Query Notification that helped me solve my own problem: By using Sql Profiler to trace the QN events he suggests, i was able to spot my connection was incorrectly using the 'SET ARITHABORT OFF' option, causing my notifications to fail.

Related

How to solve (data source adapter error: java.sql.SQLSyntaxErrorException: ORA-01795: maximum number of expressions in a list is 1000) in cognos?

data source adapter error: java.sql.SQLSyntaxErrorException: ORA-01795: maximum number of expressions in a list is 1000
I am getting this error in cognos.Basically I am working on report enhancement in cognos.I already have searched for solution on internet but provided solution is for oracle, i want solution to be in cognos as i am unable to access database or framework manager. Please let me know what should be the right approch to resolve that error.
enter image description here
I already have searched for solution on internet but provided solution is for oracle. I want the solution to be in cognos as i am unable to access database or framework manager.
Error you got is indeed raised by Oracle, but the question is whether it should be fixed in Oracle or elsewhere (Cognos?).
Generally speaking, this is what happened:
select whatever from some_table
where id in (1, 2, 3) --> this is the IN clause. In Oracle, it is limited
to 1000 elements
I guess that nobody literally types 1000+ elements into the IN list, but some dynamic piece of code might, concatenating value after value until you reach and pass the limit.
I don't know Cognos, but - if that's what really happens, a simple and effective option is to store values which are being used in IN list into a table, and then either JOIN that table to other table(s), or use it as a subquery.
For example:
select whatever from some_table
where id in (select id from a_new_table)
or
select whatever
from some_table a join a_new_table b on a.id = b.id
Presumably the filter is coming from a prompt and someone is selecting everything in the list in the prompt or close to it.
It might be a good idea to try to figure out why there are so many things being chosen. This would require investigation about the business purpose of the prompt.
It might be that our someone needs to see everything so the prompt should be set as optional, which would not generate the where predicate in the SQL.
It might be that the prompt needs to be generated on an attribute (column) which is at a higher grain of dimensional abstraction (i.e. countries rather than cities).
Simply being told fix it in Cognos is not a very helpful instruction and you need to approach the problem with a better understanding of the purpose of the report.

Lazarus DBGrid totally blank after record commit

I have been using Lazarus 2.x (free pascal) with firebird 3 (via flamerobin) and i try to commit records via TSQLConnection, TSQLQuery, TDBConnection in data module (appeals) etc.
I run the following code snippet and the records are successfully commited to firebird, but unfortunately DB connection afterwards is lost and thus there is not any record to be seen via DBGrid (not even column headers - totally blank). I have to terminate the application and reopen it to gain visual insight of my firebird via DBGrid.
Button click event
appeals.SQLTransaction1.Active:=false;
appeals.SQLQuery1.SQL.Text:='UPDATE appeals set name=:name,date_entry=:entry,date_suspended=:suspended,'+
'date_court=:court,date_judgement=:judgement where id='+IntToStr(row_num);
appeals.SQLQuery1.Params.ParamByName('name').AsString:=Trim(Edit1.Text);
appeals.SQLQuery1.Params.ParamByName('entry').AsDate:=DateTimePicker1.Date;
appeals.SQLQuery1.Params.ParamByName('suspended').AsDate:=IncDay(DateTimePicker1.Date,10);
appeals.SQLQuery1.Params.ParamByName('court').AsDate:=DateTimePicker2.Date;
appeals.SQLQuery1.Params.ParamByName('judgement').AsDate:=IncDay(DateTimePicker2.Date,20);
appeals.SQLTransaction1.StartTransaction;
appeals.SQLQuery1.ExecSQL;
appeals.SQLTransaction1.Commit;
I have also used .CommitRetaining as exactly mentioned in lazarus forum without success. Any idea what could i do in order to see my records live in DBGrid after commit.
Regards
The reason the DBgrid shows nothing is that SqlQuery1.ExecSQL does not return a result set, so SqlQuery1 cannot supply any records for the DBGrid to display. So call SqlQuery1.Open after your Commit.
However, before you can call SqlQuery1.Open, you need to give it a SELECT statement to Execute, because a SQL UPDATE statement does not return a result set, as you have already found. So, do something like:
[...]
appeals.SQLTransaction1.StartTransaction;
appeals.SQLQuery1.ExecSQL;
appeals.SQLTransaction1.Commit;
appeals.SQLQuery1.Sql.Text := 'Select * from appeals where ...';
appeals.SQLQuery1.Open;
obviously replacing '...' in the 'where' clause by something appropriate. Normally. a SELECT query should be parameterised to limit the number of rows returned and the amount of time the table is locked on the server.

BIRT Before Open value list params errors

I have a (semi) basic data cube set up with a cascading parameters of State and Area. The state select box is straight forward as is the Area; when a user selects a State, the Area options are set accordingly. However, the value for Area is a long list of strings that will be sent to the mysql select statement which will use both params (State & the list of strings from Area) several time. Its a big ugly collection of UNIONS. My problem is somewhere between the before Start and query time.
//beforeOpen script...LState & LAreas the name of the report param
this.queryText = this.queryText.replace('stateList', params["LStates"].value);
this.queryText = this.queryText.replace('areaList', params["LAreas"].value);
In my mysql statement I use them in the following way:
SELECT ..XXX..
FROM ..XXX..
WHERE ..XXX..
State.State_Location in ('stateList')
AND Range_Locator.Range in ('areaList')
UNION ALL
SELECT ..XXX..
FROM ..XXX..
WHERE ..XXX..
State.State_Location in ('stateList')
AND Range_Locator.Range in ('areaList')
The two errors I get from BIRT are:
(Pretty obvious)
Cannot get the result set. org.eclipse....SQL statement doesn ot return a ResultSet object.
(Not so obvious to me)
A BIRT exception occured. Error evaluating Javascript expression. Script engine error: Can't find method java.lang.String.replace(string.java.lang.Object[]).
There are error evaluating script "this.queryText = this.queryText.replace('stateList', params["LStates"].value);this.queryText = this.queryText.replace('areaList', params["LAreas"].value);"
Any ideas? Any help would be greatly appreciated.
It seems LStates is defined as a "multi-value" parameter, therefore params["LStates"].value returns an array of values: this is why this replace method does not work.
You should try like this:
this.queryText = this.queryText.replace('stateList', params["LStates"].value.join("','"));
I am not exactly sure what you are doing in beforeOpen script, but your SQL is looking for the string values 'stateList' & 'areaList' and is probably not happy about the (). You need to use question marks to call the parameters you define in the 'parameters' you set up in the data set design.
SELECT ..XXX..
FROM ..XXX..
WHERE ..XXX..
State.State_Location in ?
AND Range_Locator.Range in ?
UNION ALL
SELECT ..XXX..
FROM ..XXX..
WHERE ..XXX..
State.State_Location in ?
AND Range_Locator.Range in ?
I don't recall using
in ?
In any queries, I usually try and set it to use
like ?
There are number of issues in sending multiple choice parameters to SQL via BIRT. you might want to look at How do I set a parameter to a list of values in a BIRT report? There are also some security concerns. When I have to filter on multiples, I usually do it in a filter (data set design option) after the SQL has run. Thought this can be a resource problem if your SQL returns lots of values.
Thanks for the replies but since I was unable to get these options to work, I wound up restructuring the database & sql statements to run better. To address some previous suggestions:
#dom the states parameter has only one value and while using "IN ('stateList')" is not efficient (disclaimer, not my code) adding the .join(',') did throw an error specific to that; I tried and mysql/BIRT seemed to be expecting a comma separated list but got a string with a trailing comma.
#James I tried using the question mark (?) instead of the 'stateList' & 'areaList' but I think mysql/BIRT was not able to recognize which value went with which question mark...not too sure though since I was not able to debug well enough and find out exactly why. Could be a form to param mapping issue that I didn't notice. Thanks for the help.

Filtering Quotes by InventTable

I'm trying to build a report in AX 2009 (SP1, currently rollup 6) with a primary data source of the SalesQuotationLine table. Due to how our inventory is structured, I need to apply a filter that shows only certain categories of items (in this case, non-service items as defined in the InventTable). However, it seems that there is a problem in the link between the SalesQuotationLine and InventTable such that only two specific items will ever display.
We have tested this against the Sales Quotation Details screen as well, with the same results. Executing a query such as this:
...will only show quotes that have one of the specific items mentioned earlier. If we change the Item Type to something else (for example to Item), the result is an empty set. We are also getting this issue on one of our secondary test servers, which for all intents is a fresh install.
There doesn't seem to be any issues with the data mapping from the one table to the other, and we are not experiencing this issue with any other table set. Is this a real issue, or am I just missing something?
After analyzing the results from a SQL Profile run during the execution of the query, it seems the issue was a system bug. When selecting a table to join to the SalesQuotationLines, you have two options: 'Items' and 'Items (Item Number)'. Regardless of which table you select the query executes with, it joins the InventTable with the relation "SalesQuotationLines.ProjTransCode = InventTable.ItemId".
After comparing the table to other layers in the system, I found the following block of code removed from the createLine method (in the SYP layer):
if (this.ProjTransType == QuotationProjTransType::Item)
{
this.ProjTransCode = this.ItemId;
}
Since the ProjTransCode is no longer being populated, the join does not work except on certain quote lines that do have the ProjTransCode populated.
In addition, there is no directly defined relation to the InventTable - the link is only maintained via an Extended Data Type that is used on the SalesQuotationLine.ItemId field. Adding this relation in manually solved the problem.

Setting LINQ transaction a specified query (without SqlCommand)

I was having difficulty specifying the title for the question properly - essentially I am getting an error saying "ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized." for a situation similar to this:
using (db = getDbContext())
{
var results = (from t in db.table
select t.column).SingleOrDefault();
}
As the error says, all this is already wrapped in another transaction, which I am trying to use for this query as well.
How can I specify the transaction using this query format?
I've tried creating an SqlCommand("select column from table", myconnection, mytransaction),
which works, but I'd much rather use the LINQ syntax for the intellisense (amongst other) benefits, as the actual query is rather more complex
Many thanks, this has been annoying me for hours.
Alex
You can set the transaction into the context itself:
db.Transaction = theTransaction;

Resources