             }, dbPointer)
     }
     
+    private func uninstallTransactionObserverHooks() {
+        sqlite3_update_hook(sqliteConnection, nil, nil)
+        sqlite3_commit_hook(sqliteConnection, nil, nil)
+        sqlite3_rollback_hook(sqliteConnection, nil, nil)
+    }
+    
     private enum StatementCompletion {
         // Statement has ended with a commit (implicit or explicit).
         case TransactionCommit

        }
    }
    
    private func setupTransactionHooks() {
        let dbPointer = unsafeBitCast(self, UnsafeMutablePointer<Void>.self)
        
        sqlite3_update_hook(sqliteConnection, { (dbPointer, updateKind, databaseName, tableName, rowID) in
            let db = unsafeBitCast(dbPointer, Database.self)
            
            // Notify change event
            if !db.transactionObservers.isEmpty {
                db.didChangeWithEvent(DatabaseEvent(
                    kind: DatabaseEvent.Kind(rawValue: updateKind)!,
                    databaseName: String.fromCString(databaseName)!,
                    tableName: String.fromCString(tableName)!,
                    rowID: rowID))
            }
            }, dbPointer)
        
        
        sqlite3_commit_hook(sqliteConnection, { dbPointer in
            let db = unsafeBitCast(dbPointer, Database.self)
            
            do {
                try db.willCommit()
                db.statementCompletion = .TransactionCommit

    public func addTransactionObserver(transactionObserver: TransactionObserverType) {
        preconditionValidQueue()
        transactionObservers.append(transactionObserver)
        if transactionObservers.count == 1 {
            installTransactionObserverHooks()
        }
    }
    
    public func removeTransactionObserver(transactionObserver: TransactionObserverType) {
        preconditionValidQueue()
        if let index = transactionObservers.indexOf({ observer in observer === transactionObserver}) {
            transactionObservers.removeAtIndex(index)
        }
        if transactionObservers.isEmpty {
            uninstallTransactionObserverHooks()
        }
    }
    
