Database chain and synonyms

Source: Internet
Author: User

You need to pay extra attention when creating synonyms that contain database links. Otherwise, problems may occur.

If the synonym points to the object of the current user, there will be no problem:

SQL> CONN YANGTK/YANGTK @ YTK is connected.
SQL> CREATE DATABASE LINK TEST2 CONNECT TO TEST IDENTIFIED BY TEST USING 'test2 ';
The database link has been created.
SQL> SELECT COUNT (*) FROM TEST @ TEST2;
COUNT (*)
----------
5
SQL> CREATE SYNONYM TEST FOR TEST @ TEST2;
You have created a synonym.
SQL> SELECT COUNT (*) FROM TEST;
COUNT (*)
----------
5

If another user creates a synonym pointing to TEST @ TEST2, the following error occurs:

SQL> CREATE USER ABC IDENTIFIED BY ABC;
The user has been created.
SQL> GRANT DBA TO ABC;
Authorization successful.
SQL> CONN ABC/ABC @ YTK is connected.
SQL> CREATE SYNONYM TEST FOR YANGTK. TEST;
You have created a synonym.
SQL> SELECT COUNT (*) FROM TEST;
Select count (*) FROM TEST
* Error in row 1st:
ORA-02019: Description of remote database connection not found

The most fundamental cause of this problem is the database chain. The special format of the database chain determines that it cannot be accessed through SCHEMA. DBLINK_NAME. Therefore, the user can only access the database chain or PUBLIC database chain of the current user.

For synonyms, it is just an alias. In actual execution, it will still be replaced with the original name, so the actual execution is: select count (*) from test @ TEST2.

The current user does not include the database chain, so the above error occurs. At this time, the same database chain needs to be created under the current user:

SQL> CREATE DATABASE LINK TEST2 CONNECT TO TEST IDENTIFIED BY TEST USING 'test2 ';
The database link has been created.
SQL> SELECT COUNT (*) FROM TEST;
COUNT (*)
----------
5

This problem does not occur if the synonym points to the global database chain.

Note that if the current user already has a database chain pointing to another database with the same name, or the user creates a database chain with the same name, it will cause an error.

SQL> CREATE DATABASE LINK TEST2 CONNECT TO TEST IDENTIFIED BY TEST USING 'testdata ';
The database link has been created.
SQL> SELECT COUNT (*) FROM TEST;
COUNT (*)
----------
1
SQL> SELECT COUNT (*) FROM YANGTK. TEST;
COUNT (*)
----------
1
SQL> CONN YANGTK/YANGTK @ YTK is connected.
SQL> SELECT COUNT (*) FROM TEST;
COUNT (*)
----------
5

On the surface, two users access the same object, but the two users access different databases in time. Be careful here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.