How Can I / Should I Reuse SqlCacheDependency? Thanks
May 1, 2006
I can create a SqlCacheDependency, and link it to a cached item in httpcontext cache. When something change, it will remove the cached item from the cache. I think I have to redo the process when that happens - prepare sql command, create SqlCacheDependency and insert the item into cache. Now I only need a notification from my SQL when something changes in one of my table, I don;t need read anything from db, and I think I should find a way to not recreate the SqlCacheDependency object everytime?
I've been having trouble getting the SqlCacheDependency to work without polling (which is set up through SqlCacheDependencyAdmin or aspnet_regsql). If you check out the documentation it says things like: Microsoft:
I have a SQL 2005 database and am running the 2.0 framework. I cannot seem to enable SQL caching on the db. I'm using the command line tool as follows: C:WINDOWSMicrosoft.NETFrameworkv2.0.50727>aspnet_regsql -S 99.99.999.999 -Uwebteam -P sa -t tblname -d dbname -et Enabling the table for SQL cache dependency. .An error has happened. Details of the exception:The database 'dbname' is not enabled for SQL cache notification. To enable a database for SQL cache notification, please use the System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications method, or the command line toolaspnet_regsql. To use the tool, please run 'aspnet_regsql.exe -?' for more information. C:WINDOWSMicrosoft.NETFrameworkv2.0.50727>
I've replaced the ip address, dbname and tblname with ficticious names for obvious reasons here in the post. What command can I enter to enable the database for caching?
I have implemented a caching strategy using the sqlcachedependency and sql server 2005 backend using the broker service.This works fine and well when i am connecting to the SQL Server 2005 under service account that is in the role db_owner. In a production enivironment i am reluctant to do this so i created another service account that only has execute permissions on the stored procedures.When i use this limited service account for my ASP.net web application, the broker service does not send any messages to the web app to invalid the cache. When checking the event log and SQL profiler i get errors all relating to the user not having access to the SqlQueryNotificationService queue. So i did a lot of googling and tried running the grant scripts below with no luck using this limited service account. Keep in mind everything works fine if i use an account with db_owner priviledges. These are the grants i have tried based on numerious articles GRANT CREATE PROCEDURE TO three_d_ss_loginGRANT CREATE QUEUE TO three_d_ss_loginGRANT CREATE SERVICE TO three_d_ss_loginGRANT SUBSCRIBE QUERY NOTIFICATIONS TO three_d_ss_loginGRANT RECEIVE ON QueryNotificationErrorsQueue TO three_d_ss_loginGRANT REFERENCES ON CONTRACT::[http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification] TO three_d_ss_login These are the grants i have tried that does not work GRANT SEND ON SERVICE::SqlQueryNotificationService TO three_d_ss_loginGRANT RECEIVE on SqlQueryNotificationService_DefaultQueue to three_d_ss_loginCan some one suggest what i need to do to get sqlcacheddependencies to work with a sql2005 backend under a limited priviledged service account? ThanksJim
Hi, I'm trying to set up a SqlCacheDependency using the Query notifications of SQL Server 2005. I haven't even got to the point of testing the notifications part. My problem is that my DataTable is not even getting stored in the cache when I insert it. The cache seems to be getting invalided as soon as I add the DataTable. Here is my code: (am trying to get a simple example working first)protected void Page_Load(object sender, EventArgs e) { DataTable results = (DataTable)HttpRuntime.Cache.Get("supplyFunctions"); if (results == null) { Response.Write("Cache Invalidated, hitting DB. TIME: " + DateTime.Now.ToString()); results = getSupplyFunctions(); } else { Response.Write("got from Cache, TIME: " + DateTime.Now); } GridView1.DataSource = results; GridView1.DataBind(); }private DataTable getSupplyFunctions() { DataTable results = new DataTable(); using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()) ) { using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "schema.myStoredProc"; command.CommandType = CommandType.StoredProcedure; SqlCacheDependency dependency = new SqlCacheDependency(command); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataSet dataset = new DataSet(); adapter.Fill(dataset); results = dataset.Tables[0]; HttpRuntime.Cache.Insert("supplyFunctions", results, dependency); }
} return results; } Also, whats weird is that when I keep refreshing the page, after like 5 to 10 minutes it works, and starts caching the data. This disturbs me-- I would like to know what is going on. I am pretty sure my stored procedure doesn't break the rules of the query notifications. Can someone help me out?!??! Thanks!
Hi all,I've been able to successfully use SQLCacheDependency and my test application is using only one database table to test SQLCacheDependency. Now, I was reading "Programming Microsoft ASP.NET 2.0" by Dino Esposito and I've couple of questions.a)-SQLCache will expire on Insert, Update and Delete queries on the database table? Am I right? b)-Right now I'm using SQLCacheDependency on one table, can I use it on more than one table ? If I can't , can you please give me some ideas on how to use SQLCacheDependency on more than one table? c)-Lastly, If large number of clients are using my application, what about the performance gains/losses if I use SQLCacheDependency? I will really appreciate your help. Thanks,Oltmans
Hi All,I've successfully implemented an example of SQLCacheDependency on one database table. I was reading "Programming Microsoft ASP.NET 2.0" by Dino Esposito and I've couple of questions. I will really appreciate your help.a)-Can I use SQLCacheDependency on more than one database tables? If I can't then I will appreciate if you can give me couple of hints as to how to do this.b)-As I understand SQLCache on a table will expire if any Insert and Update SQL operations happen on that table.c)-Our software is being used by many clients. Do you think SQLCacheDependency will have some sort of performance hits?I will really appreciate any help.Thanks,Oltmans
I am using SqlCacheDependency to monitor a database table for changes. Unfortunately, it is polling the table too often and is causing performance issues. It seems to poll at least once a second. So now I'm trying to find a way to adjust it so that it polls about once every 10 seconds.The code that uses the SqlCache Dependency is located in a Class Library Project that is used by my web application project. The code is as follows:// in the class constructorSqlDependency.Start("...some connection string");// in the body of a methodusing (SqlConnection connection = new SqlConnection("...same connection..."){ connection.Open(); SqlCommand command = connection.CreateCommand(); command.CommandText = query; SqlDependency dependency = new SqlDependency(command); dependency.OnChange += OnDepChange; SqlDataReader data = command.ExecuteReader(); // code that uses the data goes here}public void OnDepChange(object sender, SqlNotificationEventArgs e){ DoSomething();}I did a little searching on the web and noticed that some examples showed that you could adjust the pollTime in the web.config file:<sqlCacheDependency enabled="true" pollTime="30000"> <databases> <add name="Pubs" connectionStringName="PubsConn" /> </databases></sqlCacheDependency>However, as I stated earlier, the code that uses the SqlCacheDependency is located in a Class Library, so this configuration doesn't help me. Is there a way to adjust the pollTime without getting my connection string from the web.config?
I have some problem with ASP.NET cache, I found other people has similar problem, but I didn't find real solution. The one bother me most is the SQLCacheDependency doesn't work stable. I insert object in cache and has SQLCacheDependency linked. After a period of time, it stopped working. That means the the object is still in cache, but change on db side doesn't remove the cache entry. I am not sure if it is ASP side or SQL side, I feel it is ASP side. I am using 2.0 + SQL 2005. Once the db command notification stop working, you have to restart IIS or clear all items in cache since you don't kno which one is changed. Any suggestion?
I've set up a SqlCacheDependency in my Asp.Net application, but the dependency invalidates the cache immediately every time the page is hit.
I think the problem may be with my SQL Query, but it seems to me to meet the requirements on the Special Considerations When Using Query Notifications page on MSDN. Could someone take a look at this query and tell me if I've done something wrong? Thank you.
Here's my query:
Code Snippet
USE [chameleon]
GO
SET ANSI_NULLS ON
GO
SET ANSI_PADDING ON
GO
SET ANSI_WARNINGS ON
GO
SET CONCAT_NULL_YIELDS_NULL ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NUMERIC_ROUNDABORT OFF
GO
SET ARITHABORT ON
GO
ALTER PROCEDURE [dbo].[usp_customers_by_site_id]
@site_id INT
AS
SELECT
customers.customer_id,
customers.name,
customers.po_prefix,
customers.dt_created,
customers.created_by AS auid,
customers.po_required
FROM dbo.customers
WHERE customers.site_id = @site_id
AND customers.is_active = 1
and here's the code in my Asp.Net site where I try to use the SqlCacheDependency:
Code Snippet public List<Customer> GetCustomersBySite(int siteID) { List<Customer> customers = new List<Customer>();
if (HttpRuntime.Cache["CustomersBySite" + siteID] != null){ customers = (List<Customer>)HttpRuntime.Cache["CustomersBySite" + siteID]; } else { using (SqlCommand command = new SqlCommand("usp_customers_by_site_id", Connection)){ command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@site_id", SqlDbType.Int).Value = GetDBValue(siteID, false); SqlCacheDependency dependency = new SqlCacheDependency(command);
I am using a grid to display the data on my webpage, and I have 3 projects in my solution (UI, BLL, DAL). When I load the webpage I am creating the instance of a class written n my BLL project to populate the data, and in BLL I'm creating the object for the class in DAL and returning the datatable.
Now when I refresh the page using F5, the SQLDependency is working fine. But, when I use the Paging or Sorting option's on my grid I see in SQL Profiler that the query is posted back to the SQLServer to get the data.
If this is the senario then how can I make use of the cache object in .NET 2.0.
Dear Folks, I'm getting the error with application like this..............
"the transaction log for database "mydatabase" is full.....to find out why space in the log can not be reused, see the log_reuse_wait_desc column in sys databases. "
I've checked with the column, there are totally 3 options.(I've checked with all other databases) 1)checkpoint, 2)log_backup, 3)nothing
what can i choose for this error to be suppressed? actually there was log_backup option.
Decided to try testing SqlCacheDependency and got the following.... thoughts???After using the following code...void Application_Start(object sender, EventArgs e) { System.Data.SqlClient.SqlDependency.Start (ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString); }//-------------------------------- ... then more code SqlCommand sqlCommand = new SqlCommand("select ApplicationName from dbo.aspnet_Applications", sqlConnection); SqlCacheDependency sqlCacheDependency = new SqlCacheDependency(sqlCommand);//-------------------------------- ... then more codeMembershipUser membershipUser = Membership.CreateUser(defaultUserNameTextbox.Text, defaultUserPasswordTextbox.Text); if (membershipUser != null) {//----------------------------- ... more codethe above code no longer works giving the below exception : using a plain database generated using the following statement
I have SQL Server 2005 and I am using .Net 2.0. Is there a way to cache a query, basically I am going to do a SELECT * on a table that is called frequently. I then want to query this DataTable that is in cache. I want to invalidate the cache when the table changes. Everything I have read tells me to use the output caching directive. But, much of what I am doing doesn't end up in the output. Is there a way to invalidate the cache the same way without output caching and using the Service Broker?
I have tried two ways for executing a query and creating a dependency on it, one using plain Sql commands, the other using Enterprise Library wrapped commands.
I keep getting:
Warning: Fatal error 682 occurred at Oct 12 2007 11:01AM. Note the error and time, and contact your system administrator.
string xml = cmd.ExecuteScalar() as string;
When I execute it. Does anyone know what could cause this.
I have read both of these posts and have not yet been able to investigate the SQL machine's event viewer though:
using (SqlConnection conn = new SqlConnection(connString)) { SqlCommand cmd = new SqlCommand( "SELECT Value FROM dbo.ContentXml WHERE [Key] = '" + key + "'", conn);
if (createDependency) { System.Web.Caching.SqlCacheDependency dependency = new SqlCacheDependency(cmd);
result.Dependency = dependency; }
conn.Open();
string xml = cmd.ExecuteScalar() as string;
conn.Close();
result.Content = xml; }
return result;
//SqlCommand cmd = (SqlCommand) // Global.Current.Database.GetSqlStringCommand( // //@"SELECT Value FROM dbo.ContentXml WHERE [Key] = @Key"); // @"SELECT Value FROM dbo.ContentXml WHERE [Key] = '" + key + "'");
I've the following simple code bound to a button on a Web page
string time = (string)Cache["KEY"]; if (time == null) {
SqlConnection sqlConnection = new SqlConnection(@"Server=BIZYUSUFSQL2005;Database=Deneme;User Id=sa;Password=;"); SqlCommand command = new SqlCommand(@"select KOLON1 from dbo.CACHE", sqlConnection); sqlConnection.Open();
SqlCacheDependency dependency = new SqlCacheDependency(command); time = System.DateTime.Now.ToString(); Cache.Insert("KEY", time, dependency); command.ExecuteNonQuery(); sqlConnection.Close(); } return time; This code has to return the time value from cache. And when a record is inserted into the CACHE table, the cache item has to be invalidated and the new time value has to be returned. The code works properly for 2-3 minutes. But when there is no activity for 5 minutes, the cache invalidation does not work anymore.
It's a pretty basic question but I haven't been able to find any examples out there. I dimmed a dataadapter and would like to reuse later in my code (line 3 in the code below). What is the correct syntax to do this? Dim da As New SqlDataAdapter("SELECT * FROM myTable", conn)da.Fill(myDataTable)da.______ ("SELECT * FROM myTable2", conn)da.Fill(myDataTable2)
The SqlParameter with ParameterName '@pk' is already contained by another SqlParameterCollection
If I could work out what code to post I would, but I can say that I am managing my Sql data in my code by caching small arrays of SqlParameter objects as pulled from the database. If I need to update the DB I change the cached SqlParameter and re-insert it. If I perform a SELECT I check the cache first to see if I already have the SqlParameter. However, currently, I am experiencing the above error when performing a select, then later an update, followed by another update. Would I be correct in saying that a SqlParameter object can only be used once for a database operation and should be discarded? Would I be correct if I said that the SqlCommand object should be discarded? I am barking up the wrong tree entirely?
do we have to create connection and assign it to command object before creating SqlCacheDepency? chek the code below. SqlServer is not invalidating cache when there is update. When i create Dependency using command object, at that time command object does not have connection. It will assign connection to command object in Excute method SqlCommand cmd = new SqlCommand(commandText);cmd.CommandType = CommandType.StoredProcedure;SqlCacheDependency cacheItem = new SqlCacheDependency(cmd);DataSet result;result = DBHelper.Execute(cmd);HttpRuntime.Cache.Insert(cacheItemName, result, cacheItem);
What is the best code pratice to use do the following code,
SELECT fo.no as LNum, fo.name as LName, sum(CASE fo.docnome WHEN "In" THEN fo.etotal ELSE 0 END) as In1, sum(CASE fo.docnome WHEN "In2" THEN fo.etotal ELSE 0 END) as In2, sum(In1+In2)/10 as inDec, from fo group by fo.no,fo.name order by fo.name
instead of
SELECT fo.no as LNum, fo.name as LName, sum(CASE fo.docnome WHEN "In" THEN fo.etotal ELSE 0 END) as In1, sum(CASE fo.docnome WHEN "In2" THEN fo.etotal ELSE 0 END) as In2, ((sum(CASE fo.docnome WHEN "In" THEN fo.etotal ELSE 0 END))+sum(CASE fo.docnome WHEN "In2" THEN fo.etotal ELSE 0 END)))/10 as inDec, from fo group by fo.no,fo.name order by fo.name
I cant use functions and procedures. Is there any better and cleaner way to code this, reusing the calculated values?
I wonder if there is a solution for this in SQL 2000 (or do I have towait for SQL 2005)?I am currently in the middle of developing a 'Yahoo' style portal whichwill be rolled out in about 20 or so countries. I have set up in SQLServer one database per country. All the portals have the samefunctionality - but show different data.Is it possible to have a single database which holds storedproceedures, functions and views and have the individual countrydatabases use these?Note: I want to avoid using EXEC sp_executesql.I look forward to some good news on this! Thank you in advance.Dadou.
Hi. I'm starting to try out LINQ to SQL, and so I'm using SQL Express (on Vista) to experiment. My problem is that once I create and delete a database, I can never use the name again. If, after deleting the old database, I try to create another one with the same name (say Acct1), I get "Create failed for Database 'Acct1'. An exception occurred while executing a Transact-SQL statement or batch. The logical file name "Acct1" is already in use. Choose a different name. Error: 1828"
I am trying to create the database in SQL Server Management Studio Express. That database name does not appear in the list of databases: there is only AdventureWorks and the system databases. The .MDF and .LDF files have been deleted. Not just sent to the recycle bin, but permanently deleted.
I have already used up Acct1, Acct2, and Acct3, just to try out different scenarios. Each time, I delete the old database before trying to create a new one with the same name, but I am forced to always supply a new, different name. I have checked the directory with hidden files and system files showing to be sure there is no old file lurking there somewhere.
Is there a way to delete these old "logical file names"? I can't even find any reference to their existence except for the message that says they are already in use.
with c1 As ( select 1 As '1' , 2 As '2' , 3 As '3' ) , c2 As ( select 4 As '4', 5 As '5', 6 As '6' union all select 1 , 2 ,3 from c1 -- >>>>> select from c1 here ) select * from c2 union all select * from c1 -- >>>>>> and select from c1 here
According to the query above , I try to reuse the subquery by put the subquery into 'with cte' name (c1) then i select this 2 times .
if I do this way , how many time this subquery (c1) execute ? if 1 time then this is the right way to reuse this subquery . if 2 times , it is not then what should i do to reuse this subquery ?
I have read the articles posted online concerning different dialog reuse strategies. Most of them create a new table in the sender to hold dialog ids. I was wondering what is wrong, if anything, with the following approach:
Code Block declare @dlg uniqueidentifier select top 1 @dlg = conversation_handle from sys.conversation_endpoints where state IN ('CO') if @dlg is null begin begin dialog conversation @dlg from service [tcp://SFT3DEVSQL01:4022/TyMetrix360Audit/DataSender] to service '//TyMetrix360Audit/DataWriter','386DDD04-7E55-466A-BE83-37EFC20910B9' on contract [//TyMetrix360Audit/Contract] with encryption = off; end ;send on conversation @dlg message type [//TyMetrix360Audit/Message] (@msg)
Here I simply select a conversation handle directly from the sys.conversation_endpoints table. Can anyone see any issues with this approach?
I have been working with SQL for quiet a while but think this perhaps is a very basic question that has always escaped me:
At my work I was exposed to both, MS SQL Server 2000 and Sybase Adaptive Server Anywhere/Sybase SQL Anywhere.
Under Sybase I was able to use aliases in other calculations and filters but i have never been able to do the same with SQL.
Example: In Sybase I can write this:
Select Price * Units as Cost Cost * SalesTax as TotalTaxFrom Invoice Where TotalTax > 3.5 However if i want to do this in MS SQL 2000 i have to go trough
Select Price * Units as Cost Price * Units * SalesTax as TotalTax From Invoice Where (Price * Units * SalesTax) > 3.5
In the long run this is costing me a lot of code redundancy, not to mention a debugging nightmare. Is there a way to replicate this alias usage in MS SQL Server?
I am working on a project developing a fairly large number of reports with a team of developers. Many of these reports have common elements and code, such as common headers with user-selectable colors. Additionally, many of the common parts of the reports are at mockup stage currently, and many features will have to be added to the reports as time goes on.
We're attempting to create a generalized framework that will minimize the duplication of effort as we develop these reports, and as we go back and modify or fix them later.
What is the best way to approach this?
My first attempt was to create a report template and base all the reports off of the same template. That was fine for the first pass, but as we need to make changes later, they will not be propagated to the already existing reports.
My second attempt was to have each componant of that template reference a subreport, so that changes to the actual report template will be minimized as we go forward. This works great for minimizeing work, but it appears that you lose many features with the use of subreports, and there seems to be a pretty serious performance impact as well. I have posted about one such issue here: Pagination
If anyone has pointers about how to go about this, and where I should start, they would be greatly apreciated!
Q1: Can I just open these protocols I need (see web links below) and then close them down again after SQLCacheDependency has been enabled? Note: The website (asp.net 2.0/SQL server Express) operates OK right now with asp.net - but it does not have SQLCacheDependency enabled. Problem: When I try to do this I keep on getting this error message: "An error has occurred when establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections." None of these 3 commands have worked (all give the same message above): aspnet_regsql.exe -U user1 -P pwd1 -S server1/SQLEXPRESS -d myDBName -ed aspnet_regsql.exe -E -S server1/SQLEXPRESS -d myDBName -ed aspnet_regsql.exe -E -S .SqlExpress -d myDBName -ed I am trying to run these commands via a MSTSC connection. Q2: Is that (terminal server) the problem (the "remote connection") complained of above ? This is the command that worked on my local dev machine: aspnet_regsql.exe -E -S .SqlExpress -d myDBName -ed Q3: Must I really go through the procedure "How to: Configure Express to accept remote connections" at: http://support.microsoft.com/default.aspx?scid=kb;EN-US;914277http://blogs.msdn.com/sqlexpress/archive/2005/05/05/415084.aspx I don't really want to have SQL server accepting any connections as the last time it was listening on these ports we had so many attacks being made that it was filling up the logs! What I can't understand is that every night the database is backed up with the following command line code (operating as a Scheduled Task): sqlcmd -s server1/SQLEXPRESS -i MW4BAK.SQL -E Q4: So if the server is happy receiving commands from 'sqlcmd.exe' why does it have such problems with 'aspnet_regsql.exe' Q5: If I grouped those statements I need to setup SQLCacheDependency under a Scheduled Task would they work (as there would presumably be no remote connection then) or is it something in the nature of the connection established which allows sqlcmd.exe to work but stops aspnet_regsql.exe?
SQL Server 2005 / .NET 2.0 Web App - SqlCacheDependency is not invalidated and I see an error sql log that occurs after the specified table changes:
2006-09-22 12:18:51.84 spid21s The query notification dialog on conversation handle '{8D7EF5BC-544A-DB11-B6E0-000F1FD735BF}.' closed due to the following error: '<?xml version="1.0"?><Error xmlns="http://schemas.microsoft.com/SQL/ServiceBroker/Error"><Code>-8490</Code><Description>Cannot find the remote service 'SqlQueryNotificationService-d61fa581-0567-43ba-991f-37499e87cbf6' because it does not exist.</Description></Error>'.
I've read all applicable articles that I can find and applied hotfix kb916002. The sqldependency has never worked, it is not a stability issue as described in kb916002. In a trace I can see the sqldependency starts correctly. I have a console app that works fine with SqlDependancy calling same database and login creds. I don't know what to think. Calling sys.dm_qn_subscriptions show no subscriptions when this app starts as does the console app. Please advise as it looks like a broker issue.
I have a table in which there is a column of type identity(1,1),in the same table i have bulk data, and i have deleted a row from my table, so now i want to reuse that same Id again, how to do that.
I have some common functions that i use in several script tasks. How du i store a function globaly so that i can use it from different projects and still only have to edit it one place ?
I have several stage to star (i.e. moving data from a staging table through the key lookups into a fact table) ETL transformations in a single SSIS package. Each fact table has a different set of measures but the identical foreign key set, e.g. ConsultantKey, SubsidiaryKey, ContestKey, ContestParamKey and MonthKey.
Currently I have to replicate the key lookup (Surrogate Key Pipeline, or SKP) for each data flow. If I could cache each dimension one time in the package and reuse it for each stage to fact it would be much more efficient.
Is there a way for me to reuse a common data flow?