Java MySQL-命名管道连接在关闭时引发警告 [英] Java MySQL - Named Pipe connection throws warning on close

查看:133
本文介绍了Java MySQL-命名管道连接在关闭时引发警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用命名管道使用Connector/J连接到Java中的MySQL.我的连接和查询成功,但是当我尝试关闭连接时收到警告.我想知道我应该怎么做才能解决导致警告的问题,而不是删除connection.close()(或删除try-with-resources连接块,而不添加connection.close()).

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"只是一个带有方法"createConnection()"的自定义类.班级/方法没有什么特别的;它只是创建并返回一个连接.

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

方法完成后(或者从技术上讲,在Java 7 try-with-resources连接块完成之后),我收到以下错误/警告:

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 **

如果删除try-with-resources块(并且不添加connection.close()),则警告将永远不会出现.另外,如果我切换到TCP/IP连接,则永远不会出现该错误.但是,这些解决方案都不适合我的情况.我想确保正确关闭连接,并且将使用命名管道连接.

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.

有什么想法吗?

-(编辑)- 另外:Connector/J中的日志会引发错误.我的错误捕获与错误的捕获和打印方式无关.我试图研究如何禁用WARN问题,并使其仅打印严重问题(在Connector/J的Log内),但未成功.此外,我想解决WARN问题,而不是忽略/隐藏它.

-- (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.

-(编辑2)- 我监视了MySQL数据库,但连接没有关闭.如果我使用TCP/IP连接字符串而不是命名管道连接字符串(并且不进行任何其他更改),则关闭连接就可以了.

-- (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.

-(编辑3)- 忽略我的原始代码...只需尝试下面的代码,它就会发出警告.对我来说似乎是个虫子.

-- (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
        }
    }
}

推荐答案

由于它似乎是一个错误,我已经向MySQL错误论坛提交了正式的错误报告.

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.

更新:

我的错误报告已由MySQL编码人员分析.他们认为这是JAVA错误.

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

错误报告

这篇关于Java MySQL-命名管道连接在关闭时引发警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆