Results 1 to 3 of 3

Thread: Best way to open connection in COM+ environment?

  1. #1

    Thread Starter
    Addicted Member ShIzO's Avatar
    Join Date
    Apr 1999
    Location
    Bartlett, IL
    Posts
    189

    Best way to open connection in COM+ environment?

    hi, here is the scenario:
    we have Data.dll that has few data classes and a module w/ global
    functions like the one to update database, lets call is: "UpdateDB"
    (it takes SQL statement as parameter).

    im looking for the best possible way to do this now, so is it better
    to:

    1) have that global function (UpdateDB) open and close DB connection
    within itself

    2) open connection at the begining of one of the data functions and
    close it at the end, pass connection object to that global function
    (UpdateDB)

    thanks,
    wojo.
    www.HardFind.com -buy/sell/trade your used hardware.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    I prefer #2. Why? Well, at some point you are going to need to perform two (or more) updates in one of your data functions. If one of these updates fail you might need to rollback the ones that were successfull. Using #2 you now have the ability to make use of database Transactions.

    There is a third option that you might want to consider as well. Its a combination of options 1 and 2. Your global update procedure can accept an Optional Connection object as a parameter. The procedure would then check to see if the Connection was valid. If not, open the connection (option 1) then proceed to do the update (option 2).

    Hope this gives you some ideas.
    Bruce

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    We have a setup like this. This way a connection is opened in the procedure itself and you're not relying on the fact that there is an open connection for a start, secondly, the active connection object is only held in memory for the short time it is needed - not throughout the life of the application.

    You can declare all your connection string etc as constants in a common module for each procedure to call which would cut down some coding for you...

    VB Code:
    1. Function xyz(b as variable) as variable
    2.     open connection
    3.     do stuff to database
    4.     close connection
    5. End Function
    6.  
    7. Function zyx(a as variable) as variable
    8.     open connection
    9.     do stuff to database
    10.     close connection
    11. End Function

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

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



Click Here to Expand Forum to Full Width