服务定义如下:
[ServiceContract(SessionMode = SessionMode.Required)]
public interface IComplexService
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Mandatory)]
void ExecuteNoneQuery1(string sql);
[OperationContract]
[TransactionFlow(TransactionFlowOption.Mandatory)]
void ExecuteNoneQuery2(string sql);
[OperationContract]
[TransactionFlow(TransactionFlowOption.Mandatory)]
void ExecuteNoneQuery3(string sql);
}
服务实现如下:
[ServiceBehavior(TransactionTimeout = "00:00:45",TransactionIsolationLevel = System.Transactions.IsolationLevel.Serializable)]
public class ComplexServiceImplement : IComplexService
{
private static void ExecuteNoneQuery(string sql)
{
string sqlConnectionString = "Password=sa;User ID=sa;Initial Catalog=ipedf;Data Source=.";
string oraConnectionString = "User ID=sample;Password=sample;Data Source=insapp";
using (OracleConnection oraConnection = new OracleConnection(oraConnectionString))
{
try
{
oraConnection.Open();
OracleCommand cmd = oraConnection.CreateCommand();
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
string Debug = e.Message;
}
finally
{
oraConnection.Close();
}
}
using (SqlConnection sqlConnection = new SqlConnection(sqlConnectionString))
{
try
{
sqlConnection.Open();
SqlCommand cmd = sqlConnection.CreateCommand();
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
string Debug = e.Message;
}
finally
{
sqlConnection.Close();
}
}
}
[OperationBehavior(
TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void ExecuteNoneQuery1(string sql)
{
ExecuteNoneQuery(sql);
//OperationContext.Current.SetTransactionComplete();
}
[OperationBehavior(
TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void ExecuteNoneQuery2(string sql)
{
ExecuteNoneQuery(sql);
//OperationContext.Current.SetTransactionComplete();
}
[OperationBehavior(
TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void ExecuteNoneQuery3(string sql)
{
ExecuteNoneQuery(sql);
//OperationContext.Current.SetTransactionComplete();
}
服务端配置如下:
<system.serviceModel>
<!--提供的服务-->
<services>
<service name="Wf.Complex.Service.ComplexServiceImplement" behaviorConfiguration="myServiceBehavior">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="transactionalWsatHttpBinding" contract="Wf.Complex.Service.IComplexService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="myServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="transactionalWsatHttpBinding" transactionFlow="true"/>
</wsHttpBinding>
</bindings>
</system.serviceModel>
客户端代码如下:
IComplexService complex = new ComplexServiceClient();
try
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
string sql = "delete from student";
complex.ExecuteNoneQuery1(sql);
sql = "insert into student(id,name) values(1,'yeyang_1')";
complex.ExecuteNoneQuery2(sql);
sql = "insert into student(id,name) values(2,'yeyang_2')";
complex.ExecuteNoneQuery3(sql);
scope.Complete();
}
Console.WriteLine("succeed");
}
catch (Exception expt)
{
Console.WriteLine(expt.Message);
}
finally
{
(complex as ComplexServiceClient).Close();
}
运行结果:
服务端的ExecuteNoneQuery方法中using (OracleConnection oraConnection = new OracleConnection(oraConnectionString))代码块发生异常,异常内容如下:
"无法加载 DLL“oramts.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。"
结论:
对于数据库事务,可以通过程序事务变形的方式来实现,但必须获取对应数据库产品的支持,sqlserver产品默认支持,oracle数据库产品需要考虑本风险,及时规避.