ic3 ic3 - 4 months ago 28
Java Question

DatabaseMetaData.getColumns returning an empty ResultSet for synonyms

The method

getColumns()
on the metadata is returning an empty ResultSet for Synonyms (for tables and views it's returning the list of columns properly).

This happens on Oracle 11g Express and using the latest Oracle JDBC driver (11.2.3).

Does this happen for other SQL servers too?

Any help/ideas to solve this issue are welcome.

Answer

By default the Oracle drivers does not return information on synonyms in getColumns(). This is documented in the Oracle 11g JDBC Developer's Guide under Performance Extensions:

Considerations for getColumns

By default, the getColumns method does not retrieve information about the columns if a synonym is specified. To enable the retrieval of information if a synonym is specified, you must call the setIncludeSynonyms method on the connection as follows:

( (oracle.jdbc.driver.OracleConnection)conn ).setIncludeSynonyms(true)

This will cause all subsequent getColumns method calls on the connection to include synonyms. This is similar to setRemarksReporting. Alternatively, you can set the includeSynonyms connection property. This is similar to the remarksReporting connection property.

However, bear in mind that if includeSynonyms is true, then the name of the object returned in the table_name column will be the synonym name, if a synonym exists. This is true even if you pass the table name to getColumns.

Note that this last item is really important to keep in mind!