Oracle's dblink

When a user wants to access data in another database table across the local Oracle database, a dblink of the remote database must be created in the local database. Through the dblink local database, the data in the remote database table can be accessed like a local database. The following describes how to create a dblink in a local database.

There are generally two ways to create a dblink, but the user must have the permission to create a dblink before creating a dblink. To know about dblink permissions, log in to the local database as the sys user:

  select * from user_sys_privs t
  where t.privilege like upper('%link%');
  1 SYS CREATE DATABASE LINK NO
  2 SYS DROP PUBLIC DATABASE LINK NO
  3 SYS CREATE PUBLIC DATABASE LINK NO

  It can be seen that dblink has three permissions in the database: CREATE DATABASE LINK (the created dblink can only be used by the creator, other users cannot), CREATE PUBLIC DATABASE LINK (public means that the created dblink can be used by all users) , DROP PUBLIC DATABASE LINK.

  Under the sys user, grant CREATE PUBLIC DATABASE LINK and DROP PUBLIC DATABASE LINK permissions to your user

  grant CREATE PUBLIC DATABASE LINK,DROP PUBLIC DATABASE LINK to scott;

  Then log in to the local database as the scott user

  1. The first way to create a dblink is to configure the database to be accessed remotely in the local database tnsnames.ora file.

  create public database link

  to_bylw connect to scott identified by tiger using 'bylw';

  Where to_bylw is the name of the dblink you created, bylw is the instance name of the remote database, and scott/tiger is the user/password to log in to the remote database. Then access the scott.tb_test table in the remote database 'bylw' through dblink in the local database, the sql statement is as follows

  select * from scott.tb_test@to_bylw;

  2. The second way to create a dblink is to not configure the remote database to be accessed in the local database tnsnames.ora file

Create dblink
CREATE DATABASE LINK database link name
CONNECT TO user name
IDENTIFIED BY password
USING 'database connection string';

  create database link to_test
  connect to scott identified by tiger
  using '(DESCRIPTION =
  (ADDRESS_LIST =
  (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.5)(PORT = 1521))
  )
  (CONNECT_DATA =
  (SERVER = DEDICATED)
  (SERVICE_NAME = bylw)
  )
  )';

  3. The second is to put the information in the first configuration in the tnsnames.ora file directly after the statement to create the dblink. In the first case, the information in the tnsnames.ora file is as follows:
The easy mistake is
to establish a connection in your own (client's) tnsname.ora, but not in
the tnsname.ora of A1's (server) database A1. .

  bylw =
  (DESCRIPTION =
  (ADDRESS_LIST =
  (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.5)(PORT = 1521))
  )
  (CONNECT_DATA =
  (SERVER = DEDICATED)
  (SERVICE_NAME = bylw)
  )
  )
4. Simplify using synonyms :
CREATE SYNONYM S_MY_TABLE FOR TABLENAME@database link name;

5. Solution for locks when
db_link is queried Every time you use db_link to query, release the connection and call the close function in the dbms_session package.
Example: dbms_session.close_database_link(CONN_MY_LINK);
or use When dblinking, even the select text must be committed or rolledback,
otherwise it will hinder other processes for a long time.
View the lock table process and unlock
(1) Method 1:
select sess.sid,
sess.serial#,
lo.oracle_username,
lo.os_user_name,
ao.object_name,
lo.locked_mode
from v$locked_object lo,
dba_objects ao,
v$session sess
where ao.object_id = lo.object_id and lo.session_id = sess.sid;
(2) Method 2:
select * from v$session t1, v$locked_object t2
where t1 .sid = t2.SESSION_ID;
(3) Process unlock
If there is a record, it means there is a lock, record the SID and serial#, replace the recorded ID with the following SID, serial, you can release LOCK
alter system kill session 'SID, serial ';

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325296533&siteId=291194637