Program transaction and Database Transaction conversion in WCF

Source: Internet
Author: User
Tags oracleconnection

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.