2008-11-26 6 views
5

J'essaie de configurer une transaction simple pour mes actions Linq-to-SQL par rapport à ma base de données Sql 2000. En utilisant TransactionScope il ressemble à ceci:Erreur TransactionScope contre Sql Server 2000 - Le gestionnaire de transactions partenaire a désactivé sa prise en charge pour les transactions réseau/distant

using (TransactionScope transaction = new TransactionScope()) 
{ 
    try 
     { 
     Store.DBDataContext dc = new Store.DBDataContext(); 
     Store.Product product = GetProduct("foo"); 
     dc.InsertOnSubmit(product); 
     dc.SubmitChanges(); 
     transaction.Complete(); 
    } 
    catch (Exception ex) 
    {     
     throw ex; 
    } 
} 

Cependant, je continue à obtenir l'erreur suivante:

Le gestionnaire de transactions partenaire a désactivé son soutien pour les transactions à distance/réseau. (Exception de HRESULT: 0x8004D025)

Mais, si je configure la transaction en utilisant une transaction traditionnelle, cela fonctionne correctement. Donc, cela fonctionne très bien:

Store.DBDataContext dc = new Store.DBDataContext(); 
try 
{ 
    dc.Connection.Open(); 
    dc.Transaction = dc.Connection.BeginTransaction(); 
    Store.Product product = GetProduct("foo"); 
    dc.InsertOnSubmit(product); 
    dc.SubmitChanges(); 
    dc.Transaction.Commit(); 
} 
catch (Exception ex) 
{ 
    dc.Transaction.Rollback(); 
    throw ex; 
} 
finally 
{ 
    dc.Connection.Close();  
    dc.Transaction = null; 
} 

Je me demande si le TransactionScope fait quelque chose de différent sous les couvertures que ma deuxième mise en œuvre. Si non, qu'est-ce que je perds en n'utilisant pas TransactionScope? De plus, toute indication sur ce qui cause l'erreur serait également bonne. J'ai confirmé que MSDTC s'exécute dans le serveur sql et sur mon ordinateur client.

Répondre

6

Jetez un coup d'oeil ici:

transactions rapides avec System.Transactions et Microsoft SQL Server 2000 http://blogs.msdn.com/florinlazar/archive/2005/09/29/475546.aspx

Et ici:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=230390&SiteID=1

First verify the "Distribute Transaction Coordinator" Service is running on both database server computer and client computers
1. Go to "Administrative Tools > Services"
2. Turn on the "Distribute Transaction Coordinator" Service if it is not running

If it is running and client application is not on the same computer as the database server, on the computer running database server
1. Go to "Administrative Tools > Component Services"
2. On the left navigation tree, go to "Component Services > Computers > My Computer" (you may need to double click and wait as some nodes need time to expand)
3. Right click on "My Computer", select "Properties"
4. Select "MSDTC" tab
5. Click "Security Configuration"
6. Make sure you check "Network DTC Access", "Allow Remote Client", "Allow Inbound/Outbound", "Enable TIP" (Some option may not be necessary, have a try to get your configuration)
7. The service will restart
8. BUT YOU MAY NEED TO REBOOT YOUR SERVER IF IT STILL DOESN'T WORK (This is the thing drove me crazy before)

On your client computer use the same above procedure to open the "Security Configuration" setting, make sure you check "Network DTC Access", "Allow Inbound/Outbound" option, restart service and computer if necessary.

On you SQL server service manager, click "Service" dropdown, select "Distribute Transaction Coordinator", it should be also running on your server computer.

+0

Le DatabaseTransactionAdapter référencé dans le poste Florin Lazar a fait l'affaire. Je posterai mon code d'implémentation en réponse. –

+0

Les instructions pour configurer le MSDTC ne semblent pas s'appliquer à Windows 7 Je n'ai qu'une seule option sous l'onglet MSDTC: utiliser le coordinateur local ou spécifier l'hôte distant à utiliser. – Myster

+1

J'ai trouvé des instructions pour trouver ces paramètres sur Windows 7 et 2008! Regardez ici: http://msdn.microsoft.com/en-us/library/aa561924(BTS.20).aspx – skb

2

La mise en œuvre de DatabaseTransactionAdapter dans florin Lazar Post que Keith Sirmons m'a indiqué semble faire l'affaire. Voici mon code qu'il appelle:

Store.DBDataContext dc = new Store.DBDataContext(); 
using (TransactionScope transaction = new TransactionScope()) 
{ 
    try 
    { 
     var dbAdapter = new DatabaseTransactionAdapter(dc.Connection); 
     dc.Connection.Open(); 
     dbAdapter.Begin(); 
     dc.Transaction = (SqlTransaction)dbAdapter.Transaction; 
     Store.Product product = GetProduct("foo"); 
     dc.InsertOnSubmit(product); 
     dc.SubmitChanges(); 
     transaction.Complete(); 
    } 
    catch (Exception ex) 
    {     
     throw ex; 
    } 
} 

La seule chose qui me fait mal à l'aise est que je ne suis pas explicitement la fermeture de la connexion, même si ce n'est pas déclaré dans une déclaration « à l'aide ».

Mais selon Florin Lazar, c'est exprès.

And you also must not close the connection, because the connection should stay open until the transaction is completed, which happens after the “using” statement ends. The adapter will take ownership of the connection lifetime and close it when it is done with it.

0

plus avis que: - Guide de configuration du serveur Activer accès réseau COM + (Windows Server 2003) Démarrer ==> Panneau de configuration ==> Ajouter ou supprimer des programmes ==> Ajout/Suppression de composants Windows, sélectionnez Application Server, puis cliquez sur Détails. Cliquez sur Activer l'accès COM + réseau, puis sur OK. Cliquez sur Suivant, puis sur Terminer.
- Si entre 2 serveurs ont un pare-feu, ouvrez le pare-feu pour les deux entrées/sorties sur ce port: Cliquez sur Démarrer ==> Tableaux de bord ==> Outils d'administration ==> Services de composants. Développez Service de composants, développez Ordinateurs, cliquez avec le bouton droit sur Poste de travail et choisissez Propriétés. Dans la fenêtre Propriétés du Poste de travail, cliquez sur l'onglet Protocole par défaut, cliquez sur TCP/IP orienté connexion et choisissez les propriétés. Dans la nouvelle fenêtre, cliquez sur Ajouter et tapez la nouvelle plage de ports DTC. Cliquez sur OK pour appliquer ces changements. Server doit être reprise pour les nouvelles modifications prennent effet

1

Les étapes pour activer ce sur Windows 2008 ou plus tard sont:

First verify the "Distribute Transaction Coordinator" Service is running on both database server computer and client computers

  1. Go to "Administrative Tools > Services"
  2. Turn on the "Distribute Transaction Coordinator" Service if it is not running

If it is running and client application is not on the same computer as the database server, on the computer running database server

  1. Go to "Administrative Tools > Component Services"
  2. On the left navigation tree, go to "Component Services > Computers > My Computer > Distributed Transaction Coordinator" (you may need to double click and wait as some nodes need time to expand)
  3. Right click on "Local DTC", select "Properties"
  4. Select "Security" tab
  5. Make sure you check "Network DTC Access", "Allow Remote Client", "Allow Inbound/Outbound"
  6. The service will restart
  7. BUT YOU MAY NEED TO REBOOT YOUR SERVER IF IT STILL DOESN'T WORK (This is the thing drove me crazy before)

On your client computer use the same above procedure to open the "Security Configuration" setting, make sure you check "Network DTC Access", "Allow Inbound/Outbound" option, restart service and computer if necessary.

On your SQL server service manager, click "Service" dropdown, select "Distribute Transaction Coordinator", it should be also running on your server computer.

Questions connexes