Connection上获取MySQL错误堆栈跟踪

Connection上获取MySQL错误堆栈跟踪

本文介绍了在Oracle JDBC Connection上获取MySQL错误堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用连接字符串进行Oracle JDBC连接时,我遇到非常奇怪的错误.我将用户名指定为"sys"(应为sys,为sysdba),理想情况下,它应通过ojdbc6.jar来提供,但在我的情况下,它来自mysql-connector-java-8.0.11.jar.在我的项目中.请帮忙,因为我必须将两个jar都保留在我的项目中.

I am facing very odd error while making Oracle JDBC connection using a connection string. I am giving username as "sys"(which should be sys as sysdba) and ideally, it should come through ojdbc6.jar but in my case, it is coming from mysql-connector-java-8.0.11.jar which is also included in my project. Please help as I have to keep both the jars in my project.

当我删除mysql-connector-java-8.0.11.jar时,出现下面提到的预期的oracle错误.

When I remove the mysql-connector-java-8.0.11.jar, I get the expected oracle error mentioned below.

private static Connection createConnectionOracle() {
            Connection conn = null;

            try {
                 Class.forName("oracle.jdbc.driver.OracleDriver");
                 conn = DriverManager.getConnection("jdbc:oracle:thin:@ (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.0.151)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)))","sys","sys");
            } catch (Exception e) {
                e.printStackTrace();
            }
             return conn;
        }

实际结果:

java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the main URL sections.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:108)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:79)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:131)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:231)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.dataguise.discoverAgent.util.Test.createConnectionOracle(Test.java:399)
    at com.dataguise.discoverAgent.util.Test.main(Test.java:29)
Caused by: com.mysql.cj.exceptions.UnableToConnectException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the main URL sections.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:103)
    ... 5 more
Caused by: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the main URL sections.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
    at com.mysql.cj.conf.ConnectionUrlParser.parseConnectionString(ConnectionUrlParser.java:139)
    at com.mysql.cj.conf.ConnectionUrlParser.<init>(ConnectionUrlParser.java:129)
    at com.mysql.cj.conf.ConnectionUrlParser.parseConnectionString(ConnectionUrlParser.java:118)
    at com.mysql.cj.conf.ConnectionUrl.getConnectionUrlInstance(ConnectionUrl.java:179)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:201)
    ... 4 more

预期结果:

ava.sql.SQLException: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:445)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:389)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:382)
    at oracle.jdbc.driver.T4CTTIfun.processError(T4CTTIfun.java:600)
    at oracle.jdbc.driver.T4CTTIoauthenticate.processError(T4CTTIoauthenticate.java:445)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:450)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192)
    at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:380)
    at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:760)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:401)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.dataguise.discoverAgent.util.Test.createConnectionOracle(Test.java:399)
    at com.dataguise.discoverAgent.util.Test.main(Test.java:29)

推荐答案

此错误在MySQL Connector/J 8版本8.0.11或更早版本(以及从未发布的MySQL Connector/J 6版本)中发生.您需要升级到8.0.12或更高版本(当前最新版本是8.0.17,请参见 https ://dev.mysql.com/downloads/connector/j/).

This error occurs with MySQL Connector/J 8 version 8.0.11 or earlier (and the never released MySQL Connector/J 6 versions). You need to upgrade to 8.0.12 or higher (current latest version is 8.0.17, see https://dev.mysql.com/downloads/connector/j/).

通过连接器/J 8.0.12发行说明:

另请参见 https://bugs.mysql.com/bug.php?id = 87600

作为背景,通常,不支持特定URL的JDBC驱动程序(例如,基于jdbc:之后的子协议)应返回null,并且仅当URL用于以下情况时才引发异常.驱动程序,但是URL或创建连接存在问题.

As background, normally, JDBC drivers that don't support a specific URL (eg based on the sub-protocol after jdbc:), should return null, and an exception should only be thrown if a URL is for the driver, but there is a problem with the URL or with creating a connection.

但是,即使存在MySQL Connector/J驱动程序中的此错误,您仍然可以连接到Oracle数据库,因为DriverManager将尝试每个驱动程序,直到能够打开连接为止.如果没有驱动程序能够连接,它将抛出任何驱动程序抛出的 first 异常,或者-如果所有驱动程序都返回null-它将抛出找不到合适的驱动程序" .

However, even with this bug in the MySQL Connector/J driver, you would still be able to connect to an Oracle database, as DriverManager will try each driver until it is able to open a connection. If no drivers were able to connect, it will throw the first exception thrown by any driver, or - if all drivers returned null - it will throw a "No suitable driver found" exeption.

在这种情况下,好像MySQL是在Oracle驱动程序之前尝试过的驱动程序,并且您希望Oracle连接也会失败,所以它恰好会抛出MySQL异常,因为这是第一个异常.

In this case it looks like MySQL was the driver tried before the Oracle driver, and as you expect your Oracle connection to fail as well, it just happens to throw the MySQL exception as that was the first exception.

长话短说,更新您的MySQL Connector/J.

Long story short, update your MySQL Connector/J.

这篇关于在Oracle JDBC Connection上获取MySQL错误堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:36