Update db table with some schedule - c#

I am developing simple auction app with c# and SqlServer.
I have an Auction entity. For simplicity it has next structure:
AuctionId
ItemId
StartPrice
StartDate
Status
CurrentPrice
BidderId
LastBiddedat
A know that it is not in normal form, it does not matter here. Not production code.
What I need is to update Auction Status if there have not been any bids in last 2 minutes. No bids - auctions status sets to Inactive.
I can track db changes in c# code via SqlDependency and it works perfectly.
Now I need a mechanism to update db when auction time is up. I would like to do it on database layer for simplicity.
It can be done via SQL Agent, there even is an example how to do it how to schedule a job for sql query to run daily? .
Maybe you have another options?
Just curious.

Common solutions are polling or implementing some sort of scheduler. Creating a SQL Dependency won't really help as this is triggered from a database data change. Your trigger is 'the absence of a change'.
Polling from the application is generally a bad idea as this creates unwanted network traffic amongst other issues.
As you mention using the built in SQL Agent Job Scheduler to perhaps run a Stored procedure and update statuses for auctions that are 2+ minutes old and inactive is the best idea. This is part of the database and therefore will create minimal network traffic (if any). It's part of SQL Server and hence is designed to run quickly and smoothly. It's also easy to set up. This would be my recommendation.

Related

How to update the database once when the timer expires?

I am using an ASP.NET web form and I have two fields Start_date and End_date in it.
I want to update a particular field in SQLServer once when the time goes beyond the End_date (i.e. the deadline is expired).
e.g. if the End_date is on 06/09/2015, then the SQLServer field should be updated as Expired at 12.01 AM on 06/10/2015.
I am new to ASP.NET and please let me know if you have any ideas. Thank you
There are way too many ways to do such thing. I.e:
making a SQL script that does that and adding that script as scheduled job in SqlServer
or, creating a WindowsService that sleeps and wakes up every hour or six and checks for expired records
or, adding a module like Hangfire to your ASP application and adding such scheduled task there
..
Those three are probably the most often used.
Anyways, it's always about some scheduler and some job to do periodicaly. Think how to write one "run" of the job (find expired, mark/delete them), then read about what schedulers you can use, then glue it together and configure the scheduler and done.
There are several ways to do this, but if all you need to do is make database changes at a given time, then I recommend using the SQL Server Agent. You can set it up to run just once or on a recurring schedule and you can set it up to run a stored procedure or you can feed it straight SQL commands.

Multi-User Database Desktop Application Design

First, I apologize for the seemingly dumb question I'm not very strong with databases.
I'm re-designing a desktop application in C# for use over a local network that basically is a highly specialized ticket tracking system. Essentially when a user launches the application they'll be asked for their credentials to gain access to the system and then the application will query the central database for data (currently a MySQL server running on a local machine), displaying it on the screen.
My question is if four users are connected and two users enter new data, what is the most efficient method of letting each user know of the new data? Would it be simply to query the database and update the application with the new data on a timer? Or would creating a server application to sit in between the user and the database server to perform queries itself and notify each connected user of updated data?
See it all depends how important is it to notify the clients in real time about the changes in your database. If your clients have no issue with a delay of minute or two you can probably go for the timer approach. But if they really wish the data to be real time (delay of less than 1-2 sec), go for the other approach. Create a separate service which polls the database and notify the client application for any update. For this you can make use of socket listners.
Hope that helps !!
4 users? On a local LAN? Using simple, indexed queries? Just poll the DB from the clients. Kick off a thread at application start up and have it run a query every 2-5 seconds, then notify the user using whatever is appropriate for background threads updating GUIs in .NET.
This is straightforward, don't over think it. The "hardest" part is the asynchronous notification of the user (which depending on your GUI layout and required actions is probably not a big deal either, thus the quotes).

Scalability and availability

I am quite confused on which approach to take and what is best practice.
Lets say i have a C# application which does the following:
sends emails from a queue. Emails to send and all the content is stored in the DB.
Now, I know how to make my C# application almost scalable but I need to go somewhat further.
I want some form of responsibility of being able to distribute the tasks across say X servers. So it is not just 1 server doing all the processing but to share it amoungst the servers.
If one server goes down, then the load is shared between the other servers. I know NLB does this but im not looking for an NLB here.
Sure, you could add a column of some kind in the DB table to indicate which server should be assigned to process that record, and each of the applications on the servers would have an ID of some kind that matches the value in the DB and they would only pull their own records - but this I consider to be cheap, bad practice and unrealistic.
Having a DB table row lock as well, is not something I would do due to potential deadlocks and other possible issues.
I am also NOT indicating using threading "to the extreme" here but yes, there will be threading per item to process or batching them up per thread for x amount of threads.
How should I approach and what do you recommend on making a C# application which is scalable and has high availability? The aim is to have X servers, each with the same application and for each to be able to get records and process them but have the level of processing/items to process shared amoungst the servers so incase if one server or service fails, the other can take on that load until another server is put back.
Sorry for my lack of understanding or knowledge but have been thinking about this quite alot and had lack of sleep trying to think of a good robust solution.
I would be thinking of batching up the work, so each app only pulled back x number of records at a time, marking those retrieved records as taken with a bool field in the table. I'd amend the the SELECT statement to pull only records not marked as taken/done. Table locks would be ok in this instance for very short periods to ensure there is no overlap of apps processing the same records.
EDIT: It's not very elegant, but you could have a datestamp and a status for each entry (instead of a bool field as above). Then you could run a periodic Agent job which runs a sproc to reset the status of any records which have a status of In Progress but which have gone beyond a time threshold without being set to complete. They would be ready for reprocessing by another app later on.
This may not be enterprise-y enough for your tastes, but I'd bet my hide that there are plenty of apps out there in the enterprise which are just as un-sophisticated and work just fine. The best things work with the least complexity.

Database Scheduled manipulation

I've wrote a Restaurant Management application.
I have a Database based on SQL Server 2005 which has one table named OrdersItems. Every 5 minutes I want to read all rows of this table and based on a specific criteria Update some fields.
I don't want to do that in my main application and I prefer to have an Alternative engine to perform this.
Which method is the best method to perform such task ? Also note that this Table (OrdersItems) is under process every time because main application must be always running and get new Restaurant Orders.
You can create a SQL Server Agent job that does the update every five minutes.
If you are using SQL Server Express edition, you can't use SQL Server Agent because it's only included in the "bigger" versions of SQL Server.
In this case, you can create your jobs manually using batch files and Windows Task Scheduler.
I definitely agree with Christian and dougajmcdonald's points about using SQL Task/ Maintenance. However since you included c# in your tags an alternative is to create a Windows Service.
The benefits of this approach
Can run on a machine that doesn't have the SQL Server Agent installed (including express editions)
Can be run outside the context of a user login.
Has standard stop start pause continue mechanism that's well understood.
If the service itself fails will likely result in an event log
This answer contains a template for a windows service that periodically gets data and executes. You may simply want to change the DoStuff method to execute a stored procedure
Create a dialog timer and let it activate a stored procedure. This has the advantage of being fully contained inside the database (no external process), it does not require SQL Agent (runs on Express) and is completely crash resilient at the point it will survive database detach/attach and backup/restore operations (the scheduled job will run after recovery on the new restored database).
I would expect a SQL Task / Maintenance plan would be the best for this.
You can set them up for whatever interval you want, specifying a SQL statement, maintenance task etc you want to run.
You can also setup alerts etc if you want to know when it fails for example.
Deploy a cron job on a server with access to the database which is started every 5 minutes and processes your data, using transactions. I see one problem there: If the amount of data to be processed is large, it could quite work more than five minutes.

How to run periodic processes on SQL Server 2008

I've built a very simple chatroom-like ASP.NET application which displays current Online/Offline users:
I have a Table with a DateTime column used as a TimeStamp. Every time a user causes a Postback or similar Get event, I update the TimeStamp. I want to, on the server, create a periodic process of some sort that I can use to check how long a user has been inactive given that I know the last time they were active. Once they have been deemed inactive (after a few minutes lets say), I want to set the value of another column to mark them as "Offline".
Any suggestions would be appreciated.
You could create a Sql Server Agent Job that runs periodically.
http://msdn.microsoft.com/en-us/library/ms187910.aspx
http://msdn.microsoft.com/en-us/library/ms186273.aspx
While the answer given above for using SQL Server Agent is a very good solution, have you considered having a component (a WCF component maybe) on the server which is doing all the state management, instead of managing the state in the database directly?
We do something similar by adding a simple webservice to the Asp.Net application, and calling it at a configurable time interval from a windows service running on the same web-server. This allows us to use our business logic, while keeping it all in one place - the web application.

Categories