Strange Problem with MTS!

Results 1 to 2 of 2

Thread: Strange Problem with MTS!

  1. #1
    Join Date
    Dec 1969

    Default Strange Problem with MTS!

    I have written a Component using VB. I am accessing a database and stores the values on private members of the component. When the component is not registered in the MTS, the ASP page returns the string values when I access them, using the Function/Property of the component. But once, the component is registered in MTS, it stops returning those string values. <BR><BR>For example, I am passing the connection string to the component, and CountryCode is a private member of the component. The method GetCountryCode(UserId) retrives the country code of the passed user id and stores it in the private variable and returns a code stating whether the method call is successful or not. <BR><BR>When the component is not registered on the MTS, I am able to retrieve the countrycode using another member function "GetCountry". But once the component is registered in MTS, it stops working.<BR><BR>Here is the code of the component<BR>----------------------------------<BR>Dim strCountryCode As String<BR>Dim strConnection As String<BR>Dim strError As String<BR>Dim strStatus As String<BR><BR>Dim AdCon As ADODB.Connection<BR>Dim AdRs As ADODB.Recordset<BR>Dim oContext As ObjectContext<BR><BR><BR><BR><BR>Public Function CountryName() As String<BR> CountryName = strCountryCode<BR>End Function<BR><BR><BR><BR><BR>Public Function Error() As String<BR> Error = strError<BR>End Function<BR><BR>Public Sub GetConnection(ByVal ConnectionString As String)<BR> strConnection = ConnectionString<BR>End Sub<BR><BR><BR>Public Function GetCountry(ByVal UserId As String) As Integer<BR> Dim RetVal As Integer<BR> <BR> On Error GoTo ErrHandle<BR> <BR> Set oContext = GetObjectContext<BR> <BR> If Not oContext Is Nothing Then<BR> Set AdCon = oContext.CreateInstance("Adodb.Connection")<BR> Set AdRs = oContext.CreateInstance("Adodb.Recordset")<BR> strStatus = "ObjectContext Used"<BR> Else<BR> Set AdCon = CreateObject("Adodb.Connection")<BR> Set AdRs = CreateObject("Adodb.Recordset")<BR> strStatus = "ObjectContext Not Used"<BR> End If<BR> <BR> AdCon.Open strConnection<BR> AdRs.Open "select Country from Wisden_Users where userid=&#039;" & UserId & "&#039;", AdCon, adOpenForwardOnly, adLockReadOnly<BR> <BR> strCountryCode = AdRs.Fields(0)<BR> RetVal = 1<BR> AdRs.Close<BR> Set AdRs = Nothing<BR> <BR> AdCon.Close<BR> Set AdCon = Nothing<BR> <BR> If Not oContext Is Nothing Then<BR> oContext.SetComplete<BR> Set oContext = Nothing<BR> End If<BR> <BR> RetVal = 1<BR> strError = "none"<BR> <BR> GetCountry = RetVal<BR> Exit Function<BR> <BR>ErrHandle:<BR> RetVal = -1<BR> Set AdCon = Nothing<BR> Set AdRs = Nothing<BR> <BR> If Not oContext Is Nothing Then<BR> oContext.SetAbort<BR> Set oContext = Nothing<BR> End If<BR> strError = Err.Description<BR> GetCountry = RetVal<BR>End Function<BR><BR><BR>Public Function Status() As String<BR> Status = strStatus<BR>End Function<BR>-------------------------------------<BR><BR><BR>ASP PAGE<BR>=========<BR> Set MTS = Server.CreateObject("CountryTest.DBHandle")<BR> MTS.GetConnection ProviderString<BR> valueRet = MTS.GetCountry("vinod")<BR> If valueRet = 1 Then<BR> Response.Write "Successful execution"<BR> Response.Write "<BR>Status : " & MTS.Status<BR> Response.Write "<BR>Country : " & MTS.CountryName<BR> Response.Write "<BR>Error : " & MTS.Error<BR> Else<BR> Response.Write "UnSuccessful execution"<BR> Response.Write "<BR>Status : " & MTS.Status<BR> Response.Write "<BR>Country : " & MTS.CountryName<BR> Response.Write "<BR>Error : " & MTS.Error<BR> End If<BR> <BR> Set MTS = Nothing<BR>=====================================<B R><BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Strange Problem with MTS!

    Is the component marked as requiring a transaction? You see, MTS will not allow you to maintain state in an object across transaction boundaries. If it did, then you&#039;d be breaking the isolation of the transaction. <BR><BR>So as soon as you call ObjectContext.SetComplete, MTS deactivates the object to ensure transactional isolation. The next time you call a property or method on the object, MTS activates a NEW instance of the object, which doesn&#039;t know anything about what happened in the previous transaction.<BR><BR>From looking at this piece of code, there is no reason to use a transaction in this context - all you are doing is reading from the database. A design pattern that I&#039;ve found really helpful is to seperate methods that update the database (and hence require a transaction) into a different object from methods that just read the database. You don&#039;t store any state in either - the state lives in a very thin "manager" object that calls methods on the other 2 objects, passing in the correct state every time.<BR><BR>Dunc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts