WCF & Transactions

N

No Name

Hello,

I have following scenario(s):

1) WPF client should connect to windows service hosting
WCF which runs operations using BLL and DAL dlls.

2) WPF client with same DLLs without WCF service.

---------------------------------------------------------

I have BLL object which should be the root of / or participate in
transaction (operation can include one or more DAL
components). Transaction scope inside BLL method uses
ambient transaction created by WCF (if client transaction
is not present) or by WPF client.

I want to use same SQL connection object with all DAL
components in order to avoid distributed transaction.

My question is:

Is it possible/correct to hook "SqlConnection.Close()"
code to TransactionCompleted for current event in order
to close database connection correctly and in right moment?

If not, when is the right moment to close shared connection?

Thank you,

A.
 
A

Alvin Bruney [ASP.NET MVP]

No, because the transaction environment does a lot more than simply
monitoring for close to clean up? What about exceptions, cancellations,
time-outs? Anyway, this already provided for you in the WCF spec -
atomictransactions, no need to re-invent the wheel.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
 
S

suse2k5

I am not trying to reinvent the wheel. I already use WCF transactions
and transaction scope mechanisms inside BLL object and WCF code.

Question was:

How can I be sure that connection which
is used by more then one DAL object is closed at the right moment
(after
current transaction commit or rollback). Is it possible using event
handler connected
to Transaction Completed Event?

My code is not written with (using SqlConnection....) { ... } for each
DAL object
because I pass sql connection reference between DAL objects in order
to avoid distributed transaction. So, I need to close that connection
at
some point in time... Correct?

Greetings,

No, because the transaction environment does a lot more than simply
monitoring for close to clean up? What about exceptions, cancellations,
time-outs? Anyway, this already provided for you in theWCFspec -
atomictransactions, no need to re-invent the wheel.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively onwww.lulu.com/owc$19.99
-------------------------------------------------------


I have following scenario(s):
1) WPF client should connect to windows service hosting
WCFwhich runs operations using BLL and DAL dlls.
2) WPF client with same DLLs withoutWCFservice.

I have BLL object which should be the root of / or participate in
transaction (operation can include one or more DAL
components). Transaction scope inside BLL method uses
ambient transaction created byWCF(if client transaction
is not present) or by WPF client.
I want to use same SQL connection object with all DAL
components in order to avoid distributed transaction.
My question is:
Is it possible/correct to hook "SqlConnection.Close()"
code to TransactionCompleted for current event in order
to close database connection correctly and in right moment?
If not, when is the right moment to close shared connection?
Thank you,
 
A

Alvin Bruney [ASP.NET MVP]

Sorry for misunderstanding you. I don't understand what you are doing. You
seem to be sharing one connection object with clients as opposed to object
pooling a connection object among clients. There's a huge difference here if
I've understood you correctly. I don't know what the behavior would be at
this point.

To your design point, you shouldn't be passing opened connection around
because you place the closing responsibility on the caller - caller doesn't
have to honor it which implies a resource leak. The better pattern is to
grab the data from the connection, close the connection, and return the data
in a custom business object inside your DAL. There's no possibility of a
resource leak in that pattern.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------


I am not trying to reinvent the wheel. I already use WCF transactions
and transaction scope mechanisms inside BLL object and WCF code.

Question was:

How can I be sure that connection which
is used by more then one DAL object is closed at the right moment
(after
current transaction commit or rollback). Is it possible using event
handler connected
to Transaction Completed Event?

My code is not written with (using SqlConnection....) { ... } for each
DAL object
because I pass sql connection reference between DAL objects in order
to avoid distributed transaction. So, I need to close that connection
at
some point in time... Correct?

Greetings,

No, because the transaction environment does a lot more than simply
monitoring for close to clean up? What about exceptions, cancellations,
time-outs? Anyway, this already provided for you in theWCFspec -
atomictransactions, no need to re-invent the wheel.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively onwww.lulu.com/owc$19.99
-------------------------------------------------------


I have following scenario(s):
1) WPF client should connect to windows service hosting
WCFwhich runs operations using BLL and DAL dlls.
2) WPF client with same DLLs withoutWCFservice.

I have BLL object which should be the root of / or participate in
transaction (operation can include one or more DAL
components). Transaction scope inside BLL method uses
ambient transaction created byWCF(if client transaction
is not present) or by WPF client.
I want to use same SQL connection object with all DAL
components in order to avoid distributed transaction.
My question is:
Is it possible/correct to hook "SqlConnection.Close()"
code to TransactionCompleted for current event in order
to close database connection correctly and in right moment?
If not, when is the right moment to close shared connection?
Thank you,
 
N

No Name

Hello,

I have one BLL object X with method Y. Inside method Y (*) I call
DAL object(s).

Operation Y can include more then one DAL object (dalObj1.Write,
dalObj2.Write) so I create some kind of "token" with sql connection
reference and pass it to DAL object(s) involved in operation Y (**).

At some point in time (ambient) transaction will finish (with success or
failure) and emit TransactionCompleted event.

I am asking if I can grab that token reference with sqlconnection
reference in TransactionCompleted eventhandler and close connection
without side effects and problems?


(*) call(s) to DAL object(s) is/are inside transaction scope which is
configured to use ambient transaction

(**) I know that it is possible to write this code with "(using
SqlConnection) { write db }" inside each DAL object write method and
don't bother with connection closing problem but this will result in
distributed transaction (because I open N connections to the same SQL
server). Am I correct?

I hope explanation is better this time :)
 
A

Alvin Bruney [ASP.NET MVP]

You can do it this way, bear in mind that, according to the docs, your way
of hooking up to transactioncompleted events cause performance issues.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top