so, there is no "easy" way to invoke transactions from BLL level? i keep googling but nothing interesting...
let's say we have the following:
Class UserBLL
<div mce_keep="true">AddUser(userlogin,username,password...) returns newly inserted auto ID (SCOPE_IDENTITY)</div>
Class UserRegistrationBLL
<div mce_keep="true">AddUserRegistrationInfo(UserId,blabla...) return true or false</div>
any way to have a third method:
<div mce_keep="true">Register(userlogin,username,password,blabla,type) { If type = 1 call AddUser and AddUserRegistration within same transaction scope, if type = 2 call AddUserOnly, if ...}</div>
the only option left for me is to wrap the Biz logic inside Stored procedures and use SQL server transactions instead of ado.net ones... i just dont think that sp is the right place to do biz stuff.
Why can't you use the System.Transactions namespace, it gives you the ability to handle distributed transactions. You can see it being used in the Pet Shop.net sample enterprise application
http://msdn2.microsoft.com/en-us/library/aa479070.aspx.
can you provide an example of that please? i looked into that namespace, i think that some dll has to be registred, but it's a bit confusing, can you please provide an exmple of a BLL class and two or 3 methods..
You have to pass the transcation object as paramaters to your methods and then the method checks if the transaction is null then it will be invoked without transaction or if not null then use it
is there any way to make MSDTC work using a remote server (in the same local network) to work. our production configuration is web serv->DB serv, the asp.net software and SQL instance are not sitting in the same machine. any one had already worked on similar
situation?
i would like to know also if there is any drawback on using MSDTC, i coulndt avoid it when we choosed to implement transactions in the BLL level.
well, after changing the DB server connection to a remote server (this is our test and production config as well), we faced some issues getting MSDTC to work. now it's fine but the issue is that the previous code is not working anymore.
we tried to add a system.transactions.transaction parameter to each and every method, we check in the main method if transaction.current is not null, and then we call the sub-methods using that transaction object (transaction.current) but we keep getting
the same error below:
can you please tell me how to use that transaction parameter in the sub-methods? you said "invoke using this transaction", what does it mean? our sub methods code is like the following:
Public Function AddNewPremiumClientReturnIdentity(ByVal ClientId
As Integer,
ByVal PremiumId
As Integer,
ByVal AccountActivate
As Integer, _
ByVal AccountStartDate
As Nullable(Of DateTime),
ByVal AccountEndDate
As Nullable(Of DateTime), _
ByVal TotalJobsPosted
As Integer,
ByVal NoOfJobs
As Integer,
ByVal TotalResumes
As Integer,
ByVal NoOfResumes
As Integer, _
ByVal SelectionProcess
As Integer,
ByVal SaleMode
As Nullable(Of
Integer), ByVal SaleUserId
As Nullable(Of
Integer), _
ByVal ApproverId
As Nullable(Of
Integer), ByVal ActivatedBy
As Nullable(Of
Integer), _
ByVal SaleRegionId
As Nullable(Of
Integer), ByVal AccountHeadActiveStatus
As Nullable(Of
Boolean), _
ByVal AccountType
As Nullable(Of
Boolean), ByVal tr
As System.Transactions.Transaction)
As Integer
Return
CType(Adapter_PremiumClient.AddNewWHIRE_Premium_Client_Return_Identity(ClientId, PremiumId, AccountActivate, _
Nothing), _
IIF(AccountType.HasValue, AccountType, Nothing)),
Integer)
End Function
Private Function IIF(ByVal condition
As Boolean,
ByVal trueObj
As Object,
ByVal falseObj
As Object)
As Object
If condition
Then
Return trueObj
Else
Return falseObj
End If
End Function
The transaction has already been implicitly or explicitly committed or aborted (Exception from HRESULT: 0x8004D00E)
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details:
System.Runtime.InteropServices.COMException: The transaction has already been implicitly or explicitly committed or aborted (Exception
from HRESULT: 0x8004D00E)
is there any way to make MSDTC work using a remote server (in the same local network) to work. our production configuration is web serv->DB serv, the asp.net software and SQL instance are not sitting in the same machine. any one had already worked on similar
situation?
i would like to know also if there is any drawback on using MSDTC, i coulndt avoid it when we choosed to implement transactions in the BLL level.
If you are using Transaction scope, its handled by Resource Manager and the transactions are automatically managed by the infrastructure. And the msdn actually recommends implementing implicit transactions using transaction scope.
The transaction Manager decides whether to use LTM (lightweight transaction manager) or Microsoft Distributed Tranasction Coordinator (MSDTC).
LTM transactions are much lighter and faster. But you need to meet certain criteria before this can be true. If not, you'll still need MSDTC for your transactions.
The easy part is, that your transactions are automatically escalated to the
MSDTC, without you knowing. It's all happening in the background. This is called
promotable enlistment. But the bad thing is when it happens, and you're not expecting it, your transaction will be much slower.
So when will your transactions be promoted to an MSDTC coordinated transaction? When the transaction spreads multiple app domains, two durable RMs show up in the same transactions, or a durable RM is used that does not support single phase notifications.
In your code above you should also include the connection to the server as well in your code so that its enlisted in the transaction and connection is closed as when the scope is completed.
The drawbacks of MSDTC are that is heavyweight and slow. However enlisting a single connection with SQL Server 2005 does not cause a transaction to be promoted. However, whenever you open a second connection to a SQL Server 2005 database causing
the database to enlist, the System.Transactions infrastructure detects that it is the second durable resource in the transaction, and escalates it to an MSDTC transaction. But this is going
to be soon fixed http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=626675&SiteID=1
but from your code it doesnt look like it will be promoted to MSDTC, however you should not stop MSDTC as its required for a proper functioning in case it needs to promote.
Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
Thank you for your reply. i'm used to use normal transactions like commandobject.transaction = tr, ... is that what you call light transactions? anyway. thanks for your reply, i didnt get everything but im sure it will be useful in the future.. anyway, it
doesnt answer my question, is there any way to use transactions from the BLL side.
my main concern was to avoid wrapping business logic in the stored procedures, so is there any way of embedding multiple methods calls within one single transaction, either ligh or heavy.
an example with the same code structure that i posted before would be HIGHLY appreciated. two sub methods call wrapped in one single method, handeling the transaction stuff.
viva-emptiness
Member
40 Points
147 Posts
Transactions in the BLL level
Nov 18, 2007 10:23 AM|LINK
so, there is no "easy" way to invoke transactions from BLL level? i keep googling but nothing interesting...
let's say we have the following:
Class UserBLL
Class UserRegistrationBLL
any way to have a third method:
the only option left for me is to wrap the Biz logic inside Stored procedures and use SQL server transactions instead of ado.net ones... i just dont think that sp is the right place to do biz stuff.
will appreciate any quick feedback
scott@elband...
Star
11336 Points
1864 Posts
Re: Transactions in the BLL level
Nov 18, 2007 12:19 PM|LINK
Why can't you use the System.Transactions namespace, it gives you the ability to handle distributed transactions. You can see it being used in the Pet Shop.net sample enterprise application http://msdn2.microsoft.com/en-us/library/aa479070.aspx.
You can read more about it here:
System.Transactions Namespace
http://msdn2.microsoft.com/en-gb/library/system.transactions.aspx
System.Transactions and ADO.NET 2.0
http://www.code-magazine.com/Article.aspx?quickid=0605031
viva-emptiness
Member
40 Points
147 Posts
Re: Transactions in the BLL level
Nov 18, 2007 12:59 PM|LINK
can you provide an example of that please? i looked into that namespace, i think that some dll has to be registred, but it's a bit confusing, can you please provide an exmple of a BLL class and two or 3 methods..
thank you,
Tamer Fathy
Participant
1174 Points
225 Posts
Re: Transactions in the BLL level
Nov 18, 2007 01:01 PM|LINK
You have to pass the transcation object as paramaters to your methods and then the method checks if the transaction is null then it will be invoked without transaction or if not null then use it
as follow
Public MyMethod(param1, param2.. transacton_obj)
{
if(transacton_obj!=null)
invoke using this transaction
else
invoke without using this transaction
}
}
architecture N-Tier configuration
MCAD .NET
http://tamer-fathy.blogspot.com/
viva-emptiness
Member
40 Points
147 Posts
Re: Transactions in the BLL level
Nov 18, 2007 02:05 PM|LINK
well, i got it working but i dont know if something is wrong in my code:
Public
Function InsertUsingTransaction() As Boolean Dim result As Boolean Try Dim ts As New TransactionScope(TransactionScopeOption.Required) Using (ts) UserId = Me.AddNewuser(117, 69, 0, Now, Now, 0, 100, 0, 0, 1, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing) UserRegistrationId = Me.userRegistration(UserId, 1, Now, 1500, 0, 1500, False, Nothing, Nothing, _ Nothing, Nothing, Nothing, Nothing, Nothing)Me.AddNewUserDetails(UserRegistrationId, 1, Now, 0, 1500, Nothing, Nothing, Nothing)ts.Complete()
End Usingresult =
True Catch ex As Exceptionresult =
False End Try Return result End Functionscott@elband...
Star
11336 Points
1864 Posts
Re: Transactions in the BLL level
Nov 19, 2007 08:40 AM|LINK
Your code looks fine - whats your error?
Have a watch of this webcast it may be of some use:
MSDN Architecture Webcast: Using System.Transactions in .NET 2.0 (Level 200)
http://msevents.microsoft.com/cui/WebCastEventDetails.aspx?culture=en-US&EventID=1032295238&CountryCode=US
viva-emptiness
Member
40 Points
147 Posts
Re: Transactions in the BLL level
Nov 27, 2007 06:24 AM|LINK
is there any way to make MSDTC work using a remote server (in the same local network) to work. our production configuration is web serv->DB serv, the asp.net software and SQL instance are not sitting in the same machine. any one had already worked on similar situation?
i would like to know also if there is any drawback on using MSDTC, i coulndt avoid it when we choosed to implement transactions in the BLL level.
thank you,
viva-emptiness
Member
40 Points
147 Posts
Re: Transactions in the BLL level
Nov 27, 2007 09:02 AM|LINK
well, after changing the DB server connection to a remote server (this is our test and production config as well), we faced some issues getting MSDTC to work. now it's fine but the issue is that the previous code is not working anymore.
we tried to add a system.transactions.transaction parameter to each and every method, we check in the main method if transaction.current is not null, and then we call the sub-methods using that transaction object (transaction.current) but we keep getting the same error below:
can you please tell me how to use that transaction parameter in the sub-methods? you said "invoke using this transaction", what does it mean? our sub methods code is like the following:
Public Function AddNewPremiumClientReturnIdentity(ByVal ClientId As Integer, ByVal PremiumId As Integer, ByVal AccountActivate As Integer, _ ByVal AccountStartDate As Nullable(Of DateTime), ByVal AccountEndDate As Nullable(Of DateTime), _ ByVal TotalJobsPosted As Integer, ByVal NoOfJobs As Integer, ByVal TotalResumes As Integer, ByVal NoOfResumes As Integer, _ ByVal SelectionProcess As Integer, ByVal SaleMode As Nullable(Of Integer), ByVal SaleUserId As Nullable(Of Integer), _ ByVal ApproverId As Nullable(Of Integer), ByVal ActivatedBy As Nullable(Of Integer), _ ByVal SaleRegionId As Nullable(Of Integer), ByVal AccountHeadActiveStatus As Nullable(Of Boolean), _ ByVal AccountType As Nullable(Of Boolean), ByVal tr As System.Transactions.Transaction) As Integer Return CType(Adapter_PremiumClient.AddNewWHIRE_Premium_Client_Return_Identity(ClientId, PremiumId, AccountActivate, _IIF(AccountStartDate.HasValue, AccountStartDate,
Nothing), _ IIF(AccountEndDate.HasValue, AccountEndDate, Nothing), _TotalJobsPosted, _
NoOfJobs, _
TotalResumes, _
NoOfResumes, _
SelectionProcess, _
IIF(SaleMode.HasValue, SaleMode, Nothing), _IIF(SaleUserId.HasValue, SaleUserId,
Nothing), _ IIF(ApproverId.HasValue, ApproverId, Nothing), _IIF(ActivatedBy.HasValue, ActivatedBy,
Nothing), _ IIF(SaleRegionId.HasValue, SaleRegionId, Nothing), _IIF(AccountHeadActiveStatus.HasValue, AccountHeadActiveStatus,
Nothing), _ IIF(AccountType.HasValue, AccountType, Nothing)), Integer) End Function Private Function IIF(ByVal condition As Boolean, ByVal trueObj As Object, ByVal falseObj As Object) As Object If condition Then Return trueObj Else Return falseObj End If End FunctionThe transaction has already been implicitly or explicitly committed or aborted (Exception from HRESULT: 0x8004D00E)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.Runtime.InteropServices.COMException: The transaction has already been implicitly or explicitly committed or aborted (Exception from HRESULT: 0x8004D00E)
naturehermit
Star
14610 Points
3046 Posts
Re: Transactions in the BLL level
Nov 27, 2007 09:22 AM|LINK
If you are using Transaction scope, its handled by Resource Manager and the transactions are automatically managed by the infrastructure. And the msdn actually recommends implementing implicit transactions using transaction scope.
The transaction Manager decides whether to use LTM (lightweight transaction manager) or Microsoft Distributed Tranasction Coordinator (MSDTC).
LTM transactions are much lighter and faster. But you need to meet certain criteria before this can be true. If not, you'll still need MSDTC for your transactions.
The easy part is, that your transactions are automatically escalated to the MSDTC, without you knowing. It's all happening in the background. This is called promotable enlistment. But the bad thing is when it happens, and you're not expecting it, your transaction will be much slower.
So when will your transactions be promoted to an MSDTC coordinated transaction? When the transaction spreads multiple app domains, two durable RMs show up in the same transactions, or a durable RM is used that does not support single phase notifications.
In your code above you should also include the connection to the server as well in your code so that its enlisted in the transaction and connection is closed as when the scope is completed.
The drawbacks of MSDTC are that is heavyweight and slow. However enlisting a single connection with SQL Server 2005 does not cause a transaction to be promoted. However, whenever you open a second connection to a SQL Server 2005 database causing the database to enlist, the System.Transactions infrastructure detects that it is the second durable resource in the transaction, and escalates it to an MSDTC transaction. But this is going to be soon fixed http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=626675&SiteID=1but from your code it doesnt look like it will be promoted to MSDTC, however you should not stop MSDTC as its required for a proper functioning in case it needs to promote.
viva-emptiness
Member
40 Points
147 Posts
Re: Transactions in the BLL level
Nov 27, 2007 09:47 AM|LINK
Thank you for your reply. i'm used to use normal transactions like commandobject.transaction = tr, ... is that what you call light transactions? anyway. thanks for your reply, i didnt get everything but im sure it will be useful in the future.. anyway, it doesnt answer my question, is there any way to use transactions from the BLL side.
my main concern was to avoid wrapping business logic in the stored procedures, so is there any way of embedding multiple methods calls within one single transaction, either ligh or heavy.
an example with the same code structure that i posted before would be HIGHLY appreciated. two sub methods call wrapped in one single method, handeling the transaction stuff.
thank you again