Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Best method for keeping website visitor count in database

Best method for keeping website visitor count in database

Scheduled Pinned Locked Moved Database
csharpannouncementhtmlcssdatabase
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    N a v a n e e t h
    wrote on last edited by
    #1

    One of our project is a content management system something like wikipedia. So I need to calculate the visitors count getting for each article. Each article in this will have a unique id. So I am looking for a best method to keep the visitor count in database. I have a master table where article details are kept. I tried adding a column named "PageView" which will be updated each time the page is requested. This worked well when website gets very less traffic. But shows incorrect results when it's running on heavy traffic. I guess it's a row level locking issue. Is there any other better approach which can be used to keep the accurate visitor count ? The work around which is in my mind is, putting a lock to the row when I select "PageView" for updating. Update the count and release the lock. If this is right, will the other select requests to the same row wait until the lock get released ?

    All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

    H 1 Reply Last reply
    0
    • N N a v a n e e t h

      One of our project is a content management system something like wikipedia. So I need to calculate the visitors count getting for each article. Each article in this will have a unique id. So I am looking for a best method to keep the visitor count in database. I have a master table where article details are kept. I tried adding a column named "PageView" which will be updated each time the page is requested. This worked well when website gets very less traffic. But shows incorrect results when it's running on heavy traffic. I guess it's a row level locking issue. Is there any other better approach which can be used to keep the accurate visitor count ? The work around which is in my mind is, putting a lock to the row when I select "PageView" for updating. Update the count and release the lock. If this is right, will the other select requests to the same row wait until the lock get released ?

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

      H Offline
      H Offline
      Hesham Amin
      wrote on last edited by
      #2

      N a v a n e e t h wrote:

      putting a lock to the row when I select "PageView" for updating. Update the count and release the lock

      Why do you select the row and lock it? why don't you just execute a simple update ? also note that in most cases, sql server will handle locks in the best way. can you post the code you use?


      Hesham A. Amin My blog

      N 1 Reply Last reply
      0
      • H Hesham Amin

        N a v a n e e t h wrote:

        putting a lock to the row when I select "PageView" for updating. Update the count and release the lock

        Why do you select the row and lock it? why don't you just execute a simple update ? also note that in most cases, sql server will handle locks in the best way. can you post the code you use?


        Hesham A. Amin My blog

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        hspc wrote:

        Why do you select the row and lock it?

        As you know it's a multi threading issue. Database server is multi threaded. So if two requests came at same time, the count may appear incorrect. That's what I thought of using locking.

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

        S H 2 Replies Last reply
        0
        • N N a v a n e e t h

          hspc wrote:

          Why do you select the row and lock it?

          As you know it's a multi threading issue. Database server is multi threaded. So if two requests came at same time, the count may appear incorrect. That's what I thought of using locking.

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

          S Offline
          S Offline
          Sandeep Kumar
          wrote on last edited by
          #4

          Hi,

          N a v a n e e t h wrote:

          I thought of using locking.

          Thats a good idea and it will work fine.......I used same concept in one of my projects.

          Regards, Sandeep Kumar.V

          1 Reply Last reply
          0
          • N N a v a n e e t h

            hspc wrote:

            Why do you select the row and lock it?

            As you know it's a multi threading issue. Database server is multi threaded. So if two requests came at same time, the count may appear incorrect. That's what I thought of using locking.

            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

            H Offline
            H Offline
            Hesham Amin
            wrote on last edited by
            #5

            I really believe that you don't need to do so. this will be safe: Update tblPages Set ViewCount = ViwCount + 1 Where PageID = @PageID this will achieve what you need and will not require any effort on your side to manage locking.

            Hesham A. Amin My blog

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups