Always On 可用性グループで使用するように SQL Server インスタンスを構成するには、SQL Server エージェントを有効にして、フェイルオーバー時にログをキャプチャし、クリーンアップ ジョブを実行する必要があります。これを行う前に、CDC エージェント ジョブのステップを変更して、現在のレプリカが実際にプライマリかどうかを確認する必要があります。これは sys.fn_hadr_is_primary_replica 関数を使用して行います。
次のコマンドを使用してインスタンスを設定します。
-- Check if the current replica is a primary for the corresponding database.USE[DATABASE_NAME];DECLARE@DatabaseNameSYSNAME=DB_NAME();IF(SELECTsys.fn_hadr_is_primary_replica(@DatabaseName))=1BEGIN-- If the replica isn't a primary, the code block that follows is skippedEXECUTEsys.sp_cdc_add_job@job_type='capture';EXECUTEsys.sp_cdc_add_job@job_type='cleanup';END
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[],[],null,["# Work with Always On availability groups\n\nThis page describes what Always-On availability groups in SQL Server are and how\nDatastream supports them for failover and data recovery scenarios.\n\nOverview of Always On availability groups\n-----------------------------------------\n\nIn SQL Server, Always On availability groups are a high-availability solution\nthat lets you prepare your databases for disaster recovery scenarios.\n\nAlways On availability groups maximize database availability for businesses.\nAvailability groups support a replicated environment for a selected set of\ndatabases, known as availability databases. Each group includes one set of\nprimary databases for read and write tasks, and up to eight sets of corresponding\nsecondary databases. The secondary databases can optionally allow read-only\naccess or backup operations.\n\nFor more information about Always On availability groups, see\n[What is an Always On availability group?](https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/overview-of-always-on-availability-groups-sql-server) in the SQL Server\ndocumentation.\n\nFor information about Always On Availability group prerequisites for a SQL\nServer instance, see the [SQL Server documentation](https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/prereqs-restrictions-recommendations-always-on-availability?view=sql-server-ver17&source=recommendations#ServerInstance).\n\nConfigure Datastream for use with Always On availability groups\n---------------------------------------------------------------\n\nDatastream supports *synchronous-commit* availability mode with the\nchange tables CDC method. Under this mode, the secondary database stays\nsynchronised with the corresponding primary until data synchronization stops.\nThe confirmation of a transaction is only sent to the client when the secondary\nreplica writes the incoming transaction log records to a disk.\n\nFor information about availability modes, see\n[Differences between availability modes for an Always On availability group](https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/overview-of-always-on-availability-groups-sql-server).\n\nTo configure your SQL Server instance for use with Always On availability groups,\nyou need to enable SQL Server Agent to capture logs when there's a failover, and\nthen run a clean up job. Before you can do this, you need to modify the CDC\nAgent job steps to check if the current replica is actually the primary. This is\ndone using the `sys.fn_hadr_is_primary_replica` function.\n\nUse the following commands to set up your instance: \n\n -- Check if the current replica is a primary for the corresponding database.\n USE [\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDATABASE_NAME\u003c/span\u003e\u003c/var\u003e];\n DECLARE @DatabaseName SYSNAME = DB_NAME();\n IF (SELECT sys.fn_hadr_is_primary_replica(@DatabaseName)) = 1\n BEGIN\n -- If the replica isn't a primary, the code block that follows is skipped\n EXECUTE sys.sp_cdc_add_job @job_type = 'capture';\n EXECUTE sys.sp_cdc_add_job @job_type = 'cleanup';\n END\n\nWhat's next\n-----------\n\n- Learn more about how Datastream works with [SQL Server sources](/datastream/docs/sources-sqlserver)."]]