The service is defined as follows:
[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 );
}
The service implementation is as follows:
[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 = 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 ();
}
The server configuration is as follows:
<System. servicemodel>
<! -- Provided services -->
<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>
The client code is as follows:
Icomplexservice complex = new complexserviceclient ();
Try
{
Using (transactionscope scope = new transactionscope (transactionscospontion. 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 ();
}
Running result:
An exception occurs in the using (oracleconnection oraconnection = new oracleconnection (oraconnectionstring) code block in the executenonequery method of the server. The exception is as follows:
"Unable to load DLL" oramts. dll ": the specified module cannot be found. (The exception is from hresult: 0x8007007e ). "
Conclusion:
Database transactions can be implemented through program transaction deformation, but must be supported by corresponding database products. sqlserver products are supported by default. Oracle database products need to consider this risk and avoid it in a timely manner.