oracle 在 JDBC 连接中使用 TNS 名称和服务名称之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15819433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between using a TNS name and a service name in a JDBC connection
提问by Luis Garcia
I have a java based server (Tomcat) that connects to an Oracle database using a JDBC connection. There are multiple ways to connect to the database: SID, TNS name, Service name.
我有一个基于 Java 的服务器 (Tomcat),它使用 JDBC 连接连接到 Oracle 数据库。连接数据库有多种方式:SID、TNS名称、服务名称。
I would like to understand what is the difference between each of these connections and what would be the recommended connection (SID, TNS, or service) if connecting to a clustered database. Here is the TNS name we have for the database:
我想了解每个连接之间的区别是什么,如果连接到集群数据库,推荐的连接(SID、TNS 或服务)是什么。这是我们为数据库拥有的 TNS 名称:
MY_NICE_TNS_NAME.MY_COMPANY.COM =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = myhostname)(PORT = 1521))
(LOAD_BALANCE = YES)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = MY_NICE_SERVICE_NAME.MY_COMPANY.COM)
(FAILOVER_MODE =
(TYPE = SELECT)(METHOD = BASIC)(RETRIES = 180)(DELAY = 5)
)
)
)
Thanks!
谢谢!
采纳答案by TheEwook
Oracle SID is the unique name that uniquely identifies your instance/database where as Service name is the TNS alias that you give when you remotely connect to your database and this Service name is recorded in Tnsnames.ora file on your clients and it can be the same as SID and you can also give it any other name you want.
Oracle SID 是唯一标识您的实例/数据库的唯一名称,其中服务名称是您远程连接到数据库时提供的 TNS 别名,该服务名称记录在客户端的 Tnsnames.ora 文件中,它可以是与 SID 相同,您也可以为其指定任何其他名称。
SERVICE_NAME is the new feature from oracle 8i onwards in which database can register itself with listener. If database is registered with listener in this way then you can use SERVICE_NAME parameter in tnsnames.ora otherwise - use SID in tnsnames.ora.
SERVICE_NAME 是 oracle 8i 以后的新特性,数据库可以向监听器注册自己。如果数据库以这种方式注册到侦听器,那么您可以在 tnsnames.ora 中使用 SERVICE_NAME 参数,否则 - 在 tnsnames.ora 中使用 SID。
Also if you have OPS (RAC) you will have different SERVICE_NAME for each instance.
此外,如果您有 OPS (RAC),则每个实例都有不同的 SERVICE_NAME。
SERVICE_NAMES specifies one or more names for the database service to which this instance connects. You can specify multiple services names in order to distinguish among different uses of the same database. For example:
SERVICE_NAMES 为该实例连接的数据库服务指定一个或多个名称。您可以指定多个服务名称以区分同一数据库的不同用途。例如:
SERVICE_NAMES = sales.acme.com, widgetsales.acme.com
SERVICE_NAMES = sales.acme.com、widgetsales.acme.com
You can also use service names to identify a single service that is available from two different databases through the use of replication.
您还可以使用服务名称来标识通过使用复制可从两个不同数据库中获得的单个服务。
In an Oracle Parallel Server environment, you must set this parameter for every instance.
在 Oracle Parallel Server 环境中,您必须为每个实例设置此参数。
The TNS is the sql*net configuration file that defines datbases address for establishing connection to them.
TNS 是 sql*net 配置文件,它定义了用于建立与它们的连接的数据库地址。
回答by Premraj
SERVICE_NAME
is a aliasto a database instance (or many instances). The main purpose of this is if you are running a cluster. Using this we can connect specific database within a cluster. And other way, using SID
(System IDentifier) we can connect to a database instance, which is a uniquename for an Oracle database instance.
SERVICE_NAME
是数据库实例(或多个实例)的别名。这样做的主要目的是如果您正在运行集群。使用它我们可以连接集群内的特定数据库。和其他的方式,使用SID
(小号ystem IDentifier),我们可以连接到一个数据库实例,这是一个独特的Oracle数据库实例的名称。
In short, SID = the unique name of your DB, SERVICE_NAME = the alias used when connecting.
简而言之,SID = 数据库的唯一名称,SERVICE_NAME = 连接时使用的别名。
There are several ways to provide database information like Directly Specified, tnsnames.ora (i.e. TNS name), LDAP Directory, Network Information Services.
有多种提供数据库信息的方法,如直接指定、tnsnames.ora(即 TNS 名称)、LDAP 目录、网络信息服务。
A TNS (Transparent Network Substrate) name is the name of the entry in tnsnames.ora
file which is kept in $ORACLE_HOME/network/admin
This file contains the information which is used by the system to connect to oracle database. Using this a client can fetch server associated information transparently. It contains the following information
甲TNS(Ťransparent Ñetwork小号ubstrate)名称在条目的名称tnsnames.ora
,其被保持在文件$ORACLE_HOME/network/admin
该文件包含用于由系统连接到oracle数据库中的信息。使用它,客户端可以透明地获取服务器相关信息。它包含以下信息
PROTOCOL
HOST IP ADDRESS
PORTNO
SID or SERVICE_NAME
E.g
例如
mydb =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.35.15.1)(PORT = 1521))
(CONNECT_DATA = (SID = mydb))
JDBC drivers connect with a connection string using TNS as follows
JDBC 驱动程序使用 TNS 连接一个连接字符串,如下所示
System.setProperty("oracle.net.tns_admin", PATH_TO_TNSNAMES.ORA);
Class.forName ("oracle.jdbc.OracleDriver");
dbUrl = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST="+IPHOST+")(PORT="+PORT+"))(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME="+DBNAME+")))"
conn = DriverManager.getConnection(dbUrl, USERNAME, PASSWORD);