Συμβουλή συστήματοςΑυτό το άρθρο ισχύει για διαφορετικό λειτουργικό σύστημα από αυτό που χρησιμοποιείτε. Το περιεχόμενο του άρθρου που ενδέχεται να μην σας αφορά έχει απενεργοποιηθεί.
Αυτό το άρθρο βήμα προς βήμα περιγράφει τον τρόπο υλοποίησης αμφίδρομες συναλλαγές αναπαραγωγής. Αυτό το άρθρο περιγράφει επίσης τα θέματα που εμπλέκονται στην υλοποίηση αμφίδρομες συναλλαγές αναπαραγωγής.
Bidirectional transactional replication, also known as two-way
transactional replication, permits a server to be both a publisher and a
subscriber to the same data. Because the servers that participate in the
replication will replicate any changes to the other servers, any changes are
not be propagated back to the originating server.
For example, if you
have two servers (Server A and Server B), the servers are said to be in
bidirectional transactional replication if both of the following conditions are
true:
The changes that are made to Table T1 at Server A are
replicated to Table T1 at Server B.
The changes that are made to Table T1 at Server B are
replicated to Table T1 at Server A.
Therefore, if a change originates from Server A, the change is
replicated to Server B, but Server B does not propagate the same change back to
Server A. Replication uses a loopback detection mechanism that the distributor
uses to determine whether to send the changes back to originating
server.
Plan the Topology for Bidirectional Transactional Replication
For bidirectional transactional replication, one of the servers
can act as central subscriber, and all the
other servers subscribe to the central subscriber. Therefore, any changes that
originate at a server are replicated to the central subscriber, and then the
central subscriber, in turn, replicates the changes to all the other servers
that participate in the replication. However, with the help of the loopback
detection mechanism, the distributor stops the change from being propagated to
the originating server.
For example, if three servers (Server A,
Server B, and Server C) participate in bidirectional transactional replication
and Server A is the central subscriber, the publishers and subscribers are
maintained in the following ways:
Server A publishes to Server B and Server C.
Server A subscribes from Server B and Server C.
Server B publishes to and subscribes from only Server
A.
Server C publishes to and subscribes from only Server
A.
Therefore, any change that originates at Server B is replicated
to Server A and Server C.
Conflicts in Bidirectional Transactional Replication
When you make changes on a server that is participating in
replication, the changes are replicated to all other participating servers.
During this replication, conflicts may occur and replication may fail. The
following list describes the possible conflicts and the ways that you can avoid
these conflicts:
If you insert a record that has a key into a table on one
of the servers and another record that has the same key already exists on the
other servers that participate in the replication, the replication does not
propagate the changes to the other servers.
Suggested
ActionTo avoid this problem, make sure that you use different keys on
each server that participates in the replication. To do so, allocate a
predetermined range of keys to each server that participates in the
replication. You can also use a composite key on each server.
When you update a record that has been deleted on another
server, the UPDATE statement affects zero rows on the server where the record
has been deleted, and the replication fails with an error.
Suggested ActionTo avoid this problem, perform one of the following steps:
Κατάργηση του@@ROWCOUNTcheck after the actual UPDATE statement in the update custom
stored procedure.
-ή-
Χρήση του-Skiperrorsparameter for the distribution agent to skip this error. For more
information about skipping errors in transactional replication, visit the
following Microsoft Web site:
Look for a record before the UPDATE statement in the
update stored procedure. If no record exists, bypass the UPDATE statement, and
the record is deleted on all the subscribers.
When you update a column in a record that is updated at the
same time on another server, the data may be different on the two
servers.
Suggested ActionTo avoid this problem, determine if the data is being updated at
the same time on other servers, and then take any necessary action. To do so,
modify the update custom stored procedure and use XCALL syntax to call the
update stored procedure. The XCALL syntax provides the values for all the
columns before the update procedure is called and provides the updated values
in the column. You can compare the current value of the column against the
value before the update stored procedure is called. If you see different
values, the column is being updated at the same time by different servers. You
can customize the stored procedure to select which value persists. For more
information about how to use XCALL, visit the following Microsoft Web
site:
ΣΗΜΕΙΩΣΗYou can specify the XCALL syntax to call the corresponding update
stored procedure or delete stored procedure by usingsp_addarticleduring publication. You can also specify the XCALL syntax by
using SQL Server Enterprise Manager. Για να το κάνετε αυτό, ακολουθήστε τα εξής βήματα:
In SQL Server Enterprise Manager, locate the
publication that you want.
Right-click the publication, and then clickΙδιότητες (Properties).
Κάντε κλικ στην καρτέλαArticlestab, locate the
article, and then click the article properties button (...) next to the
article.
ΣτοReplace UPDATE commands with this stored
procedure callπλαίσιο κειμένου, πληκτρολογήστεXCALL, και στη συνέχεια κάντε κλικ στο κουμπίOk.
ΣτοPublication Propertiesπαράθυρο διαλόγου πλαίσιο, κάντε κλικ στο κουμπίOk.
When you update different columns in a record, simultaneous
updates of different columns of a record may sometimes lead to
conflicts.
Suggested ActionTo avoid this problem, determine if the different columns in the
same record are updated at the same time, and then take any necessary action.
To do so, modify the update custom stored procedure, and then use the XCALL
syntax to call the update stored procedure. Because the XCALL syntax provides
the values before the update stored procedure is called, you can add one of the
following options to the update stored procedure before the actual update is
performed:
Compare the current values of all the columns against
their values before the update stored procedure is called.
Add a column to represent the row version and to
compare its current value with its value before the update stored procedure is
called.
Different values indicate that different columns are being
updated at the same time. You can then decide whether to update the
column.
When you delete a row that is being updated by another
server at the same time, the replication may fail.
Suggested ActionTo determine if a row is updated and deleted at the same time,
use the XCALL syntax in the delete stored procedure. Compare every column of
the row that is being deleted against the values before the delete stored
procedure is called. Different values indicate that these updates are being
performed at the same time. You can either delete or retain the updated
row.
ΣΗΜΕΙΩΣΗEven if you do not delete the record on the subscriber, the
record no longer exists on the server that originated the DELETE
statement.
When you delete a row that is being deleted at the same
time on another server that is participating in the replication, the
replication fails because the DELETE statement does not affect any rows on some
of the subscribers.
Suggested
ActionTo avoid this problem, perform one of the following steps:
Κατάργηση του@@ROWCOUNTcheck after the actual DELETE statement in the update custom
stored procedure.
-ή-
Χρήση του-Skiperrorsη παράμετρος με τον αντιπρόσωπο της διανομής για να παραβλέψετε αυτό το σφάλμα. Για περισσότερες πληροφορίες σχετικά με τα σφάλματα skipping στην αναπαραγωγή συναλλαγών, επισκεφθείτε την ακόλουθη τοποθεσία της Microsoft στο Web:
ΣΗΜΕΙΩΣΗΚάθε ανάπτυξης μπορεί να απαιτεί μια διαφορετική προσέγγιση για να επιλύσετε αυτές τις διενέξεις, ανάλογα με τις απαιτήσεις της εργασίας. Αυτές οι διενέξεις είναι ευκολότερο για την επίλυση όταν εμπλέκονται μόνο δύο διακομιστές. Όταν εμπλέκονται περισσότερα από δύο διακομιστές, ενδέχεται να μπορείτε να χρησιμοποιήσετε αποθηκευμένες διαδικασίες για να προσδιορίσετε το διακομιστή που προέρχονται οι αλλαγές. Ενημερωμένη έκδοση που είναι αποθηκευμένη διαδικασία που χρησιμοποιείται για να ενημερώσετε τις εγγραφές στο διακομιστή C δεν γνωρίζει αν η αλλαγή δημιουργήθηκε στο διακομιστή A ή το διακομιστή β. Αναπαραγωγής συγχώνευσης, σε αντίθεση με τις συναλλαγές αναπαραγωγής δεν έχει σχεδιαστεί για την επίλυση των διενέξεων. Ανάπτυξη συναλλαγών αναπαραγωγή μόνο σε σενάρια όπου αποφεύγονται διενέξεις αντί να επιλυθεί.
Για να υλοποιήσετε τις αμφίδρομες συναλλαγές αναπαραγωγής, πρέπει να ισχύει όλες οι ακόλουθες συνθήκες:
Τα δεδομένα είναι συγχρονισμένα μεταξύ των διακομιστών αναπαραγωγής.
Βρίσκονται αποθηκευμένες διαδικασίες που χρησιμοποιούνται από αναπαραγωγή σε όλες τις βάσεις δεδομένων που συμμετέχουν.
Η συνδρομή έχει οριστεί με τη χρήση του@ loopback_detection = 'true'Παράμετρος.
ΣΗΜΕΙΩΣΗΗ επιλογήΟρισμός @ loopback_detection = 'true'Δεν είναι διαθέσιμη αυτήν τη στιγμή στο περιβάλλον εργασίας χρήστη. Επομένως, πρέπει να εγγραφείτε, χρησιμοποιώντας τοsp_addsubscriptionαποθηκευμένη διαδικασία.
Εάν εκτελείτε τη δημιουργία αντιγράφων ασφαλείας και επαναφοράς, να θυμάστε ότι διαφορετικές τοποθεσίες απαιτούν διαφορετικούς περιορισμούς πρωτεύοντος κλειδιού, για να βεβαιωθείτε ότι δεν δημιουργούνται διπλές πρωτεύοντα κλειδιά. Θυμηθείτε επίσης να δημιουργήσετε όλους τους περιορισμούς με τον όρο NOT FOR ΑΝΑΠΑΡΑΓΩΓΗΣ.
Μπορείτε να χρησιμοποιήσετε το παράδειγμα που ακολουθεί, για να ρυθμίσετε τις αμφίδρομες συναλλαγές αναπαραγωγής σε έναν υπολογιστή που εκτελεί τον SQL Server 2000.
ΣΗΜΕΙΩΣΗΟι προτάσεις Transact-SQL παρατίθενται για κάθε ένα από τα παρακάτω βήματα. Εκτελέστε τις προτάσεις Transact-SQL για την εκτέλεση της εργασίας που αναφέρεται στο προηγούμενο βήμα.
Δημιουργία ενός διανομέα, οι εκδότες και συνδρομητές σε έναν υπολογιστή που εκτελεί τον SQL Server. Για να το κάνετε αυτό, ακολουθήστε τα εξής βήματα:
Δημιουργήστε δύο βάσεις δεδομένων:
use master
go
create database test1
go
create database test2
go
Δημιουργήστε τους δύο πίνακες που έχουν έναΤαυτότητα (Identity)ο ορισμός στήλης με την επιλογή NOT FOR ΑΝΑΠΑΡΑΓΩΓΗΣ:
use test1
go
create table two_way_test1
(
pkcol INTEGER PRIMARY KEY NOT NULL,
intcol INTEGER IDENTITY(1,1) NOT FOR REPLICATION,
charcol CHAR(100),
timestampcol TIMESTAMP
)
use test2
go
create table two_way_test2
(
pkcol INTEGER PRIMARY KEY NOT NULL,
intcol INTEGER IDENTITY(1000000000,1) NOT FOR REPLICATION,
charcol CHAR(100),
timestampcol TIMESTAMP
)
go
Εκχωρήστε ένα προκαθορισμένο εύρος τιμών στη στήλη πρωτεύοντος κλειδιού, έτσι ώστε οι τιμές σε διαφορετικούς διακομιστές δεν βρίσκονται στην ίδια περιοχή. Για παράδειγμα, μπορείτε να επιβάλετε 1-1000, με την περιοχή κλειδιού για τοtwo_way_test1πίνακας με τοTest1βάση δεδομένων και στη συνέχεια επιβολή 1001-2000 με την περιοχή κλειδιού γιαtwo_way_test2πίνακας με τοTest2Database. Για να γίνει αυτό, χρησιμοποιήστε τον ακόλουθο κώδικα:
-- Constraint to enforce a range of values between 1 and 1000 in database test1
use test1
go
alter table
two_way_test1
with nocheck
add constraint
checkprimcol check NOT FOR REPLICATION
(
pkcol BETWEEN 1 and 1000
)
go
use test2
go
-- Constraint to enforce a range of values between 1001 and 2000 in the database test2
alter table
two_way_test2
with nocheck
add constraint
checkprimcol check NOT FOR REPLICATION
(
pkcol BETWEEN 1001 and 2000
)
go
Ενεργοποίηση του διακομιστή με το διανομέα και στη συνέχεια να δημιουργήσετε μια βάση δεδομένων της διανομής:
use master
go
sp_adddistributor @distributor = '<distributor name>'
go
use master
go
sp_adddistributiondb @database='distribution'
go
Ενεργοποίηση όλων των υπολογιστών εκτελεί τον SQL Server που συμμετέχουν στην αναπαραγωγή του ως εκδότες:
use master
go
exec sp_adddistpublisher
@publisher = '<Your Server Name>',
@distribution_db ='distribution',
@security_mode = 0,
@login = 'sa',
@password = 'sa',
@working_directory ='<Location of Directory>'
Ενεργοποίηση όλων των εντοπισμένων βάσεων δεδομένων για την αναπαραγωγή:
use master
go
exec sp_replicationdboption N'test1', N'publish', true
go
exec sp_replicationdboption N'test2', N'publish', true
go
Δημιουργία προσαρμοσμένης αποθηκευμένες διαδικασίες για INSERT, UPDATE, και DELETE λειτουργίες σε όλες τις βάσεις δεδομένων για να εφαρμόσετε τις αλλαγές που έγιναν κατά τη διάρκεια της αναπαραγωγής.
ΣΗΜΕΙΩΣΗΣυνήθως, όταν εισαγάγετε μια τιμή σε μια στήλη ΙDENTITY, την επιλογή IDENTITY_INSERT για τον πίνακα πρέπει να είναι ON. Αυτή η εργασία επιτυγχάνεται με την επιλογή NOT FOR ΑΝΑΠΑΡΑΓΩΓΗΣ για εισερχόμενη αναπαραγωγή παράγοντες.
Δεν μπορείτε να ενημερώσετε τις τιμές στη στήλη IDENTITY. Επομένως, όταν ενημερώνετε τις τιμές κατά τη διάρκεια της αναπαραγωγής, πρέπει να καταργήσετε τις παλιές τιμές και να εισαγάγετε τις νέες τιμές. Για να δημιουργήσετε το προσαρμοσμένο αποθηκευμένες διαδικασίες, ακολουθήστε τα εξής βήματα:
Δημιουργία προσαρμοσμένης αποθηκευμένων διαδικασιών σε τοTest1Database:
use test1
go
-- INSERT Stored Procedure
create procedure sp_ins_two_way_test1
@pkcol int,
@intcol int,
@charcol char(100),
@timestampcol timestamp,
@rowidcol uniqueidentifier
as
insert into two_way_test1
(
pkcol,
intcol,
charcol
)
values
(
@pkcol,
@intcol,
@charcol
)
go
--UPDATE Stored Procedure
create procedure sp_upd_two_way_test1
@pkcol int,
@intcol int,
@charcol char(100),
@timestampcol timestamp,
@rowidcol uniqueidentifier,
@old_pkcol int
as
declare @x int
declare @y int
declare @z char(100)
select
@x=pkcol,
@y=intcol,
@z=charcol
from
two_way_test1
where
pkcol = @pkcol
delete
two_way_test1
where
pkcol=@pkcol
insert into two_way_test1
(
pkcol,
intcol,
charcol
)
values
(
case isnull(@pkcol,0) when 0 then @x else @pkcol end,
case isnull(@intcol,0) when 0 then @y else @intcol end,
case isnull(@charcol,'N') when 'N' then @z else @charcol end
)
go
-- DELETE Stored Procedure
create procedure sp_del_two_way_test1
@old_pkcol int
as
delete
two_way_test1
where
pkcol = @old_pkcol
go
Δημιουργία προσαρμοσμένης αποθηκευμένων διαδικασιών σε τοTest2Database:
use test2
go
-- INSERT Stored Procedure
create procedure sp_ins_two_way_test2
@pkcol int,
@intcol int,
@charcol char(100),
@timestampcol timestamp,
@rowidcol uniqueidentifier
as
insert into two_way_test2
(
pkcol,
intcol,
charcol
)
values
(
@pkcol,
@intcol,
@charcol
)
go
--UPDATE Stored Procedure
create procedure sp_upd_two_way_test2
@pkcol int,
@intcol int,
@charcol char(100),
@timestampcol timestamp,
@rowidcol uniqueidentifier,
@old_pkcol int
as
declare @x int
declare @y int
declare @z char(100)
select
@x=pkcol,
@y=intcol,
@z=charcol
from
two_way_test2
where
pkcol = @pkcol
delete
two_way_test2
where
pkcol=@pkcol
insert into two_way_test2
(
pkcol,
intcol,
charcol
)
values
(
case isnull(@pkcol,0) when 0 then @x else @pkcol end,
case isnull(@intcol,0) when 0 then @y else @intcol end,
case isnull(@charcol,'N') when 'N' then @z else @charcol end
)
go
-- DELETE Stored Procedure
create procedure sp_del_two_way_test2
@old_pkcol int
as
delete
two_way_test2
where
pkcol = @old_pkcol
go
Δημιουργήστε μια δημοσίευση συναλλαγών και προσθέστε στη δημοσίευση και στις δύο άρθρα τουTest1και τοTest2βάσεις δεδομένων:
Ενεργοποίηση ως συνδρομητές όλους τους διακομιστές που συμμετέχουν στην αναπαραγωγή:
exec sp_addsubscriber
@subscriber = '<Your Server Name>',
@login = '<login name>',
@password = '<password>'
go
Προσδιορισμός μία από τις βάσεις δεδομένων ως κεντρική συνδρομητή.
Create transactional subscriptions in all the databases that participate in the
replication so that all the databases subscribe to the central subscriber and
the central subscriber subscribes to all the other databases.
For
example, in this scenario, theTest1database is the central subscriber. Create transactional
subscriptions in thetest2database that subscribe to the publication atTest1and in theTest1database that subscribe to the publication attest2.
ΣΗΜΕΙΩΣΗCreate all the subscriptions with the LOOPBACK_DETECTION option
enabled.
To do so, use the following code:
--Adding the transactional subscription in test1.
use test1
go
exec sp_addsubscription
@publication = N'two_way_pub_test1',
@article = N'all',
@subscriber = '<Your Server Name>',
@destination_db = N'test2',
@sync_type = N'none',
@status = N'active',
@update_mode = N'sync tran',
@loopback_detection = 'true'
go
-- Adding the transactional subscription in test2.
use test2
go
exec sp_addsubscription
@publication = N'two_way_pub_test2',
@article = N'all',
@subscriber = '<Your Server Name>',
@destination_db = N'test1',
@sync_type = N'none',
@status = N'active',
@update_mode = N'sync tran',
@loopback_detection = 'true'
go
ΣΗΜΕΙΩΣΗYou can also create custom stored procedures for all publications
by using thesp_scriptpublicationcustomprocsσύστημα αποθηκευμένης διαδικασίας. Για περισσότερες πληροφορίες σχετικά με τοsp_scriptpublicationcustomprocssystem stored procedure, see the
"sp_scriptpublicationcustomprocs" topic in SQL Server 2000 Updated Books
Online.
ΣΗΜΕΙΩΣΗSQL Query Analyzer returns only 256 characters per column. You
can change this option to the maximum allowed value.
Για περισσότερες πληροφορίες, κάντε κλικ στους αριθμούς των άρθρων παρακάτω για να προβάλετε τα άρθρα της Γνωσιακής Βάσης της Microsoft (Knowledge Base):
320499
(http://support.microsoft.com/kb/320499/EN-US/
)
How To Manually Synchronize Replication Subscriptions by Using Backup or Restore
327817
(http://support.microsoft.com/kb/327817/
)
INF: Use the "-SkipErrors" Parameter in Distribution Agent Cautiously
For additional information about implementing bidirectional transactional
replication in SQL Server 7.0, click the following article numbers to view the articles in the Microsoft Knowledge Base:
300164
(http://support.microsoft.com/kb/300164/EN-US/
)
INF: How to Set Up an Identity Column on both the Publisher and the
Subscriber with Transactional Replication
ΣΗΜΑΝΤΙΚΟ: Αυτό το άρθρο είναι προϊόν λογισμικού μηχανικής μετάφρασης της Microsoft και όχι ανθρώπινης μετάφρασης. Η Microsoft σάς προσφέρει άρθρα που είναι προϊόντα ανθρώπινης αλλά και μηχανικής μετάφρασης έτσι ώστε να έχετε πρόσβαση σε όλα τα άρθρα της Γνωσιακής Βάσης μας στη δική σας γλώσσα. Ωστόσο, ένα άρθρο που έχει προκύψει από μηχανική μετάφραση δεν είναι πάντα άριστης ποιότητας. Ενδέχεται να περιέχει λεξιλογικά, συντακτικά ή γραμματικά λάθη, όπως ακριβώς τα λάθη που θα έκανε ένας μη φυσικός ομιλητής επιχειρώντας να μιλήσει τη γλώσσα σας. Η Microsoft δεν φέρει καμία ευθύνη για τυχόν ανακρίβειες, σφάλματα ή ζημίες που προκύψουν λόγω τυχόν παρερμηνειών στη μετάφραση του περιεχομένου ή χρήσης του από τους πελάτες της. Επίσης, η Microsoft πραγματοποιεί συχνά ενημερώσεις στο λογισμικό μηχανικής μετάφρασης.
Η αγγλική έκδοση αυτού του άρθρου είναι η ακόλουθη:820675
(http://support.microsoft.com/kb/820675/en-us/
)
Πόση προσπάθεια καταβάλλατε για να χρησιμοποιήσετε αυτό το άρθρο;
Πολύ λίγη
Λίγη
Μέτρια
Μεγάλη
Πολύ μεγάλη
Πείτε μας για ποιον λόγο και με ποιον τρόπο θα μπορούσαμε να βελτιώσουμε αυτές τις πληροφορίες
Σας ευχαριστούμε! Τα σχόλιά σας θα μας βοηθήσουν να βελτιώσουμε το περιεχόμενο υποστήριξης. Για περισσότερες επιλογές βοήθειας, επισκεφτείτε την αρχική σελίδα της Βοήθειας και υποστήριξης.