0

I am trying retrieve keys for a table on a MySQL database using DatabaseMetaData and trying to use InformationSchema to do it. MySQL Configuration Properties

import java.sql.*

class DataLoader {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"
    static final String DB_URL = "jdbc:mysql://localhost:3306/mydb?useInformationSchema=true"
    static final String USER = "user"
    static final String PASS = "password"
    static final String DB = "mydb"

    public static void main(String[] args) {
        Connection conn = null
        Statement stmt = null
        try {
            Class.forName(JDBC_DRIVER)
            conn = DriverManager.getConnection(DB_URL, USER, PASS)

            println "Database Product Name: " + conn.getMetaData().getDatabaseProductName()
            println "Database Product Version: " + conn.getMetaData().getDatabaseProductVersion()
            println "JDBC Driver: " + conn.getMetaData().getDriverName()
            println "Driver Version: " + conn.getMetaData().getDriverVersion()

            ResultSet importedKeysRS = conn.getMetaData().getImportedKeys(DB, null, "user")

        } catch (Exception e) {
            e.printStackTrace()
        } finally {
            try {
                if (stmt != null) {
                    stmt.close()
                    conn.close()
                }
            } catch (SQLException se) {
            }// do nothing
            try {
                if (conn != null)
                    conn.close()
            } catch (SQLException se) {
                se.printStackTrace()
            }
        }
    }
}

When I execute the above groovy code, the following is the exception thrown.

Database Product Name: MySQL
Database Product Version: 5.6.15-rel63.0-log
JDBC Driver: MySQL-AB JDBC Driver
Driver Version: mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'REFERENCED_TABLE_NAME' in where clause is ambiguous
    at com.mysql.jdbc.DatabaseMetaDataUsingInfoSchema.executeMetadataQuery(DatabaseMetaDataUsingInfoSchema.java:50)
    at com.mysql.jdbc.DatabaseMetaDataUsingInfoSchema.getImportedKeys(DatabaseMetaDataUsingInfoSchema.java:780)

When I remove useInformationSchema=true property from the JDBC URL, it works with no errors but takes too long to execute getImportedKeys()

How can I enable useInformationSchema?

If I cannot use property useInformationSchema, what can I do to make queries like getImportedKeys() and getExportedKeys() run faster?

MajorXbox
  • 503
  • 1
  • 4
  • 13
  • Are you looking to make your code run _faster_, or is the problem that your code does not run at all? – Tim Biegeleisen Jun 21 '18 at 04:27
  • @TimBiegeleisen, run faster is the goal. Unsure if `useInformationSchema` will help. – MajorXbox Jun 21 '18 at 04:28
  • First, update your MySQL Connector/J driver. Version 5.1.6 is ancient (March 2008!), the latest 5.1.x is 5.1.46 (March 2018!) and the latest version is 8.0.11 – Mark Rotteveel Jun 21 '18 at 08:45
  • @MarkRotteveel, switched to 8.0.11 and that fixed the error. But `useInformationSchema` did not improve DB call times. After removing `getExportedKeys()` (which were taking majority of the time), response times were quicker. – MajorXbox Jun 21 '18 at 14:01

0 Answers0