Java MySQL - Named Pipe connection throws warning

2019-07-19 12:01发布

问题:

I am connecting to MySQL in Java with Connector/J using Named Pipes. My connection and query are successful, but I am receiving a warning when I try to close the connection. I would like to know what I should do to fix whatever is causing the warning other than removing connection.close() (or removing the try-with-resources connection block and not adding a connection.close()).

Here is my code for the query:

public static List<List> QueryDB(int RowValue) {
    List<Long> ValueList = new ArrayList();

        try {
            try (Connection Connection_T = MySQLConnectionResources.createConnection()) {
                try (Statement Statement_T = Connection_T.createStatement()) {
                    String String_T = "SELECT AColumn FROM ATable WHERE BColumn = " + RowValue + ";";
                    try (ResultSet ResultSet_T = Statement_T.executeQuery(String_T)) {
                        while (ResultSet_T.next()) {
                            ValueList.add(ResultSet_T.getLong("AColumn"));
                        }
                    }
                }
            }
        } catch(SQLException SQLException_P) {
            System.err.println("ERROR: Failed to query the database.");
            ValueList.clear();
        }

    List<List> Result_M = new ArrayList();
    Result_M.add(ValueList);
    return Result_M;
}

"MySQLConnectionResources" is just a custom class with method "createConnection()". There is nothing special to the class/method; it just creates and returns a connection.

After the method is completed (or technically, after the Java 7 try-with-resources connection block is completed) I receive the following error/warning:

Thu Sep 22 00:31:54 EDT 2011 WARN: Caught while disconnecting...

EXCEPTION STACK TRACE:



** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: Socket is not connected

STACKTRACE:

java.net.SocketException: Socket is not connected
    at java.net.Socket.shutdownInput(Socket.java:1456)
    at com.mysql.jdbc.MysqlIO.quit(MysqlIO.java:1687)
    at com.mysql.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:4368)
    at com.mysql.jdbc.ConnectionImpl.close(ConnectionImpl.java:1557)
    at ... my class/method here (my class.java:#)


** END NESTED EXCEPTION **

If I remove the try-with-resources block (and don't add a connection.close()), the warning never appears. Also, if I switch to a TCP/IP connection the error never appears. BUT, neither of these solutions are satisfactory for my case. I want to ensure that the connection is properly closed and I will be using named pipe connections.

Any ideas?

-- (edit) -- Also: The error is being thrown by the Log within Connector/J. My error catching has nothing to do with how the error is being caught and printed. I've tried to research how to disable the WARN problems and have it print out just SEVERE problems (within Connector/J's Log), but I was unsuccessful. Furthermore, I would like to fix the WARN problem rather than ignore/conceal it.

-- (edit 2) -- I monitored the MySQL database and the connections aren't being closed. If I use a TCP/IP connection string rather than my named pipe connection string (and don't change anything else) the connection is closed just fine.

-- (edit 3) -- Ignoring my original code... just try the code below and it throws the warning. Seems like a bug to me.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public final class main {
    public static void main(String[] args) {
        Connection conn = null;
        try {
            conn =

DriverManager.getConnection("jdbc:mysql:///database?socketFactory=com.mysql.jdbc.NamedPipeSocketFactory&namedPipePath=\\\\.\\Pipe\\mysql.sock",
"root", "password");

            conn.close();


        } catch (SQLException ex) {
            // handle any errors
        }
    }
}

回答1:

As it appears to be a bug, I have submitted an official bug report to the MySQL bugs forum.

I will post back here whenever I receive an update.

UPDATE:

My bug report was analyzed by MySQL coders. They believe it is a JAVA bug.

Bug Report