How do I override openaccess connection string during runtime?

4 posts, 0 answers
  1. James
    James avatar
    2 posts
    Member since:
    May 2011

    Posted 11 May 2011 Link to this post

    G'Day,

    We are rolling out an upgraded version of our CMS using Telerik OpenAccess.

    In part of this package is a setup routine, which accepts a new database connection string. Setup connects to the new database, creates all the database objects (using scripting) then connects using OpenAccess to populate the tables.

    We are changing the Telerik connection string programmatically. Problem is, Telerik appears to be caching the old connection string (ie. the one in web.config at the time website is loaded). Web.Config snippit below:

    <connectionStrings>
      <add name="MainConnection" connectionString="Packet Size=4096;Data Source='(local)'; Initial Catalog='EITCMS_TEST6';User Id='eitcms';Password='eitcms';Persist Security Info=False;" />
    </connectionStrings>
    <openaccess xmlns="http://www.telerik.com/OpenAccess">
      <references>
        <reference assemblyname="EITCMS.DataAccess" configrequired="True" />
      </references>
    </openaccess>

    Could you please advise how to override the database that Telerik is using. I have attempted to look at connectionpools of the backend configuration for the scope provider but there appears to be no way to override the connection.

    Note that this line fails to use the new connection string:

    theEITCMSObjectScopeProvider._myDatabase = Telerik.OpenAccess.Database.Get("MainConnection")

    Appears to be caching the original connection string.

    What am I doing wrong? Has anybody attempted to do this?

    Thank You,
    James
  2. Serge
    Admin
    Serge avatar
    375 posts

    Posted 14 May 2011 Link to this post

    Hi James,

     What actually happens is that once a Database.Get call  is executedwith a certain connection string name it is cached. This means that for each connection string name there is a single database object. This is done because the retrieval (initialization) of a database object is actually a operation that is not that cheap. Whenever a Database.Get is executed the model that is retrieved from your mapping is further calculated based on your backend and this is timeconsuming.

    However if you want to make sure that it is recreated and not cached you can just retrieve it, dispose of it and then try to retrieve it again, this will make sure that the new connection string will be used. 

    If that is not your case please do let us know so that we can further investigate and resolve this issue. 

    I am looking forward to hearing from you again.

    Best wishes,
    Serge
    the Telerik team
    Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
  3. DevCraft banner
  4. James
    James avatar
    2 posts
    Member since:
    May 2011

    Posted 17 May 2011 Link to this post

    Thanks for your reply. Sorry for taking so long to get back to you...

    I tried this code in the objectscopeprovider:

    Public Shared Function Database() As Database
        theEITCMSObjectScopeProvider = Nothing
        If theEITCMSObjectScopeProvider Is Nothing Then
            theEITCMSObjectScopeProvider = New EITCMSObjectScopeProvider()
        End If
        theEITCMSObjectScopeProvider._myDatabase = Nothing
        If theEITCMSObjectScopeProvider._myDatabase Is Nothing Then
            theEITCMSObjectScopeProvider._myDatabase = Telerik.OpenAccess.Database.Get("MainConnection")
        End If
        Return theEITCMSObjectScopeProvider._myDatabase
    End Function

    However, the connection string being used from web.config is still the old one.

    What am I doing wrong? Looking forward to your reply...

    Thanks,
    James
  5. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 20 May 2011 Link to this post

    Hi James,

    Actually you need to force the database object to be disposed by calling its Dispose method. Please try this code:
    theEITCMSObjectScopeProvider._myDatabase.Dispose()
    instead of:
    theEITCMSObjectScopeProvider._myDatabase = Nothing

    Hope that helps.

    Greetings,
    Alexander
    the Telerik team
    Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
Back to Top