[HDBC connection thread affinity taken care of. prb@mult.ifario.us**20080703064803] { hunk ./src/Blog/BackEnd/HitTracker.hs 26 - , connection :: DHS3.Connection - , update_stmt :: DH.Statement - , initialize_stmt :: DH.Statement hunk ./src/Blog/BackEnd/HitTracker.hs 28 +data ConnectionState = ConnectionState { connection :: DHS3.Connection + , initialize_stmt :: DH.Statement + , update_stmt :: DH.Statement } + hunk ./src/Blog/BackEnd/HitTracker.hs 42 - ; L.infoM log_handle $ "Connected to SQLite3 database " ++ C.sqlite_hits_db_filename ++ "." + ; L.infoM log_handle $ "Connected to SQLite3 database " ++ C.sqlite_hits_db_filename ++ " inx boot on " + ++ (show defaultTid) ++ "." hunk ./src/Blog/BackEnd/HitTracker.hs 51 + ; DH.disconnect conn hunk ./src/Blog/BackEnd/HitTracker.hs 53 - ; u_st <- DH.prepare conn update_count_sql - ; i_st <- DH.prepare conn initialize_count_sql hunk ./src/Blog/BackEnd/HitTracker.hs 54 - ; let db_w = DatabaseWorker q_c conn u_st i_st defaultTid - ; db_tid <- CC.forkIO $ db_loop db_w + ; let db_w = DatabaseWorker q_c defaultTid + ; db_tid <- CC.forkIO $ db_loop db_w Nothing hunk ./src/Blog/BackEnd/HitTracker.hs 119 -db_loop :: DatabaseWorker -> IO () -db_loop dw = do { req <- CCC.readChan $ queue dw - ; do { case req of - SetCount item val -> - do { L.debugM log_handle $ "Post permatitle " ++ (permatitle item) ++ " is already tracked; updating database with new count of " ++ (show val) ++ "." - ; r <- DH.execute (update_stmt dw) [ DH.iToSql val, DH.toSql (permatitle item) ] - ; CM.unless (r == 1) $ L.warningM log_handle $ "Whoa." - } - Initialize item -> - do { L.debugM log_handle $ "Post id " ++ (permatitle item) ++ " is not yet tallied; inserting new record into database." - ; r <- DH.execute (initialize_stmt dw) [ DH.toSql (permatitle item) ] - ; CM.unless (r == 1) $ L.warningM log_handle $ "Whoa!" - } - ; L.debugM log_handle $ "Committing." - ; DH.commit (connection dw) } - `DH.catchSql` (log_sql_error "db_loop") - ; db_loop dw - } +db_loop :: DatabaseWorker -> Maybe ConnectionState -> IO () +db_loop dw Nothing = + do { myTid <- CC.myThreadId + ; conn <- DHS3.connectSqlite3 C.sqlite_hits_db_filename + ; L.infoM log_handle $ "Connected to SQLite3 database " ++ C.sqlite_hits_db_filename ++ " in db_loop on " + ++ (show myTid) ++ "." + ; u_st <- DH.prepare conn update_count_sql + ; i_st <- DH.prepare conn initialize_count_sql + ; db_loop dw $ Just $ ConnectionState conn i_st u_st } +db_loop dw c@(Just cs) = + do { req <- CCC.readChan $ queue dw + ; do { case req of + SetCount item val -> + do { L.debugM log_handle $ "Post permatitle " ++ (permatitle item) + ++ " is already tracked; updating database with new count of " + ++ (show val) ++ "." + ; r <- DH.execute (update_stmt cs) [ DH.iToSql val, DH.toSql (permatitle item) ] + ; CM.unless (r == 1) $ L.warningM log_handle $ "Whoa." + } + Initialize item -> + do { L.debugM log_handle $ "Post id " ++ (permatitle item) ++ " is not yet tallied; inserting new record into database." + ; r <- DH.execute (initialize_stmt cs) [ DH.toSql (permatitle item) ] + ; CM.unless (r == 1) $ L.warningM log_handle $ "Whoa!" + } + ; L.debugM log_handle $ "Committing." + ; DH.commit (connection cs) } + `DH.catchSql` (log_sql_error "db_loop") + ; db_loop dw c + } }