Elton Stoneman

  Home  |   Contact  |   Syndication    |   Login
  82 Posts | 0 Stories | 116 Comments | 0 Trackbacks

News

Archives

Post Categories

[Source: http://geekswithblogs.net/EltonStoneman]

The typical approach for connecting to Oracle data sources using ODP.NET is to use a named source in the connection string and rely on the name being resolved through SQLNAMES.ORA and TNSNAMES.ORA:

app.config:

  <connectionStrings>

    <add name="XYZ" connectionString="Data Source=XYZ.WORLD;User ID=xyz;Password=xyz;"/>

  </connectionStrings>

TNSNAMES.ORA:

XYZ.WORLD =

  (DESCRIPTION =

    (ADDRESS_LIST =

      (LOAD_BALANCE = off)

      (FAILOVER = on)

      (ADDRESS_LIST =

        (LOAD_BALANCE = on)

        (FAILOVER = on)

        (ADDRESS = (PROTOCOL = TCP)(HOST = XYZ1-1.internal.xyz.org.uk)(PORT = 1522))

        (ADDRESS = (PROTOCOL = TCP)(HOST = XYZ1-2.internal.xyz.org.uk)(PORT = 1522))

      )

      (ADDRESS_LIST =

        (LOAD_BALANCE = on)

        (FAILOVER = on)

        (ADDRESS = (PROTOCOL = TCP)(HOST = XYZ2-1.internal.xyz.org.uk)(PORT = 1522))

        (ADDRESS = (PROTOCOL = TCP)(HOST = XYZ2-2.internal.xyz.org.uk)(PORT = 1522))

      )

    )

    (CONNECT_DATA =

      (SERVICE_NAME = XYZ)

      (FAILOVER_MODE =

        (TYPE = select)

        (METHOD = basic)

        (RETRIES = 16)

        (DELAY = 1)

      )

    )

  )

 

This is fine until you upgrade your Oracle client, or have multiple instances – if the wrong TNSNAMES gets picked up you’ll get error ORA-12514: TNS:could not resolve the connect identifier specified, and it can be fiddly to track down.

A neater way is to put the whole set of service details in the connection string, avoid the named identifier and circumvent TNSNAMES.ORA altogether:

<connectionStrings>

    <add name="XYZ" connectionString="Data Source=(DESCRIPTION =

    (ADDRESS_LIST =

      (LOAD_BALANCE = off)

      (FAILOVER = on)

      (ADDRESS_LIST =

        (LOAD_BALANCE = on)

        (FAILOVER = on)

        (ADDRESS = (PROTOCOL = TCP)(HOST = XYZ1-1.internal.xyz.org.uk)(PORT = 1522))

        (ADDRESS = (PROTOCOL = TCP)(HOST = XYZ1-2.internal.xyz.org.uk)(PORT = 1522))

      )

      (ADDRESS_LIST =

        (LOAD_BALANCE = on)

        (FAILOVER = on)

        (ADDRESS = (PROTOCOL = TCP)(HOST = XYZ2-1.internal.xyz.org.uk)(PORT = 1522))

        (ADDRESS = (PROTOCOL = TCP)(HOST = XYZ2-1.internal.xyz.org.uk)(PORT = 1522))

      )

    )

    (CONNECT_DATA =

      (SERVICE_NAME = UVASA)

      (FAILOVER_MODE =

        (TYPE = select)

        (METHOD = basic)

        (RETRIES = 16)

        (DELAY = 1)

      )

    )

  );User ID=xyz;Password=xyz;"/>

  </connectionStrings>

Service details contain the whole of the TNSNAMES entry following the identifier (i.e. from DESCRIPTION= onwards), and can span multiple lines in the config file.

Incidentally, if you’re not using ODP.NET in favour of the Microsoft’s framework Oracle provider, System.Data.OracleClient, consider migrating as it will be deprecated from .NET 4.0.

posted on Thursday, November 05, 2009 10:03 PM