Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 19th, 2003, 03:36 AM
Authorized User
 
Join Date: Jul 2003
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I ran through my books in order to clear this out. It seems that this is just an instantiation but the object is not created until is referenced..so I guess its not the same thing as declaring it to Session.Every user gets an instance but the Connection is not created until the user needs the Connection..
Maybe it would be a better idea to change the scope to Application, as you mentioned.
As far as your technique, I would like to see a coding example, so to get a grip to it :)

Cheers
Kostas Lagos

 
Old November 19th, 2003, 08:44 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This doesn't entirely make sense. An object instantiation is an instance of the object (the object has been created). The object tags provide that, and it seems pretty clear that those object tags indicate that the object is instanced in the scope of the session.

If you were to simply declare the object in the the onstart, then it wouldn't be instantiated until the first time it was created, but that instance would persist throughout the life of the session, and may very well live on after the session is gone.

Here's the general idea I mentioned (please excuse the code, I had to type it out of my head):

dbHelpers.inc

<%
Dim m_objConn 'This will be the global DB connection

Function CheckDBConn()
    If IsNothing(m_objConn) Then
        Set m_objConn = Server.CreateObject("ADODB.Connection")
        m_objConn.ConnectionString = "<myconnection string>"
        m_objConn.Open()
    End If
End Function

Function Execute(sSQL)
    CheckDBConn
    Dim objRS : Set objRS = Server.CreateObject("ADODB.RecordSet")
    Set objRS = m_objConn.Execute(sSQL)
    Set Execute = objRS
End Function
%>

You can expand on this idea. I think in the actual version I use, I set some recordset properties and maybe do some additional error handling, but this is the general concept. I actually created a VBScript class to wrap around the db connection object so that I only need to call 1 method on that class and it does everything internally like what I have here. If I can remember later, I'll see if I can dig it up.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 19th, 2003, 10:42 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Here's that class I was talking about

<%
Class DBInterface

    '************************************************* *****
    ' Internal Variables and their Public Properties
    '************************************************* *****

    Private mobjConn
    Private mstrConnectionString

    Public Property Let ConnectionString(vData)
        mstrConnectionString = vData
    End Property

    '************************************************* *****


    '************************************************* *****
    ' Public Functions
    '************************************************* *****

    Public Function GetRows(strSQL)

        Dim objRS
        Set objRS = Server.CreateObject("ADODB.Recordset")

        Call CheckState()

        objRS.CursorLocation = 3 'adUseClient
        objRS.CursorType = 0 'adOpenForwardOnly
        objRS.LockType = 1 'adLockReadOnly
        On Error Resume Next
        Set objRS = mobjConn.Execute(strSQL)
        If Err.number <> 0 Then
            Call ErrorHandler(strSQL)
        End If

        If Not (objRS.BOF And objRS.EOF) Then
            GetRows = objRS.GetRows()
        Else
            GetRows = Array()
        End If

    End Function

    Public Function GetRS(strSQL)

        Dim objRS
        Set objRS = Server.CreateObject("ADODB.Recordset")

        Call CheckState()

        objRS.CursorLocation = 3 'adUseClient
        objRS.CursorType = 0 'adOpenForwardOnly
        objRS.LockType = 1 'adLockReadOnly
        On Error Resume Next
        Set objRS = mobjConn.Execute(strSQL)
        If Err.number <> 0 Then
            Call ErrorHandler(strSQL)
        End If
        Set GetRS = objRS

    End Function

    Public Sub Execute(strSQL)
        On Error Resume Next
        Call CheckState()
        mobjConn.Execute strSQL
        If Err.number <> 0 Then
            Call ErrorHandler(strSQL)
        End If
    End Sub

    '************************************************* *****


    '************************************************* *****
    ' Private Functions
    '************************************************* *****
    Private Sub CheckState()
        If mobjConn.State = adStateClosed Then
            mobjConn.ConnectionString = mstrConnectionString
            mobjConn.Open
        End If
    End Sub

    Private Sub ErrorHandler(strSQL)
        Response.Write "<pre>" & vbcrlf
        Response.Write "DB Interface Object encounted an error while trying to execute the following SQL:" & vbcrlf
        Response.Write vbcrlf & vbtab & strSQL & vbcrlf & vbcrlf
        Response.Write "Error Number: " & Err.number & vbcrlf
        Response.Write "Error Description: " & Err.description & vbcrlf
        Response.Write "Error Source: " & Err.source & vbcrlf
        Response.Write "</pre>" & vbcrlf
        Err.Clear
    End Sub

    '************************************************* *****


    '************************************************* *****
    ' Class Constructor/Destructor Functions
    '************************************************* *****

    Private Sub Class_Initialize
        mintCursorType = adOpenStatic
        mstrConnectionString = ""

        Set mobjConn = Server.CreateObject("ADODB.Connection")

    End Sub

    Private Sub Class_Terminate

        If mobjConn.State = adStateOpen Then mobjConn.Close
        Set mobjConn = Nothing

    End Sub

    '************************************************* *****

End Class
%>
Notice at the end, the Class_Terminate. When the ASP finishes execution and goes out of scope, the instance of this class should also go out of scope, thus calling the termination method which closes and deallocates the connection object. I honestly can't recall if I confirmed that this actually does happen. You know ASP, thing you *think* will happen don't necessarily really happen.


Peter
------------------------------------------------------
Work smarter, not harder.





Similar Threads
Thread Thread Starter Forum Replies Last Post
session komalpriya .NET Framework 2.0 4 October 30th, 2007 09:16 AM
session lakshmi devi Classic ASP Basics 4 July 20th, 2006 04:33 AM
session and cookie problem (empty session file) msincan BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 February 27th, 2005 05:31 PM
session help -Dman100- Classic ASP Basics 1 November 29th, 2004 12:45 AM
About Session mani_he Beginning PHP 7 September 18th, 2004 03:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.