Bug #8789 MYSQL J throws SocketException when application is existing
Submitted: 24 Feb 2005 17:53 Modified: 3 Mar 2005 22:22
Reporter: NOT_FOUND NOT_FOUND Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.0.14 OS:Windows (WINDOWS)
Assigned to: CPU Architecture:Any

[24 Feb 2005 17:53] NOT_FOUND NOT_FOUND
Description:
Hi 

I have an application that reads information from a mysql database. I have a static connection object in the class which is then used every where.

I have set DriverManager loggin ON using
DriverManager.setLogWriter(pw);

When I read the log at then end of the run of the application, I get following message
#######################################################
trying driver[className=com.mysql.jdbc.Driver,com.mysql.jdbc.Driver@12a1e44]
SQLException: SQLState(08S01) vendor code(0)
java.sql.SQLException: Communication link failure: java.net.SocketException, underlying cause: Socket closed

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: Socket closed

STACKTRACE:

java.net.SocketException: Socket closed
	at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:99)
	at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
	at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:66)
	at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:124)
	at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:1758)
	at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:1721)
	at com.mysql.jdbc.MysqlIO.quit(MysqlIO.java:1040)
	at com.mysql.jdbc.Connection.realClose(Connection.java:2001)
	at com.mysql.jdbc.Connection.cleanup(Connection.java:2575)
	at com.mysql.jdbc.Connection.finalize(Connection.java:1210)
	at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
	at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:83)
	at java.lang.ref.Finalizer.access$100(Finalizer.java:14)
	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160)

** END NESTED EXCEPTION **

	at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:1779)
	at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:1721)
	at com.mysql.jdbc.MysqlIO.quit(MysqlIO.java:1040)
	at com.mysql.jdbc.Connection.realClose(Connection.java:2001)
	at com.mysql.jdbc.Connection.cleanup(Connection.java:2575)
	at com.mysql.jdbc.Connection.finalize(Connection.java:1210)
	at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
	at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:83)
	at java.lang.ref.Finalizer.access$100(Finalizer.java:14)
	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160)
getConnection returning driver[className=com.mysql.jdbc.Driver,com.mysql.jdbc.Driver@12a1e44]

#######################################################

Are there programming tips? or is this a bug?

How to repeat:
Get connection function is

private static Connection connection = null;

public Connection getConnection()
            throws DatabaseException {
        //Connection connection = null;
        String dBServerUrl = null;
        try {
            Class.forName(driver);
            dBServerUrl = url;
            
            PrintWriter pw = new PrintWriter(new FileOutputStream(csvLocation + "/JDBCDriver.log", true));
			DriverManager.setLogWriter(pw);

            //dBServerUrl = "jdbc:mysql://localhost:" + (new Integer(port)).toString() + "/" + database;
            if ( null == connection)
            {
            	connection = DriverManager.getConnection(dBServerUrl, userName, password);
            	connection.setAutoCommit(false);
            }
            
        } catch (ClassNotFoundException e) {
			log("ClassNotFoundException while getting connection " + e, false);
			e.printStackTrace();
			throw new DatabaseException(e.getMessage());
		} catch (SQLException e) {
			log("SQLException while getting connection " + e, false);
			e.printStackTrace();
			throw new DatabaseException(e.getMessage());
		}
		catch (Exception e) {
			log("Exception while getting connection " + e, false);
			e.printStackTrace();
			throw new DatabaseException(e.getMessage());
        }
        return connection;
    }
[3 Mar 2005 22:22] Mark Matthews
You're not closing the connection explicitly, instead the finalizer called when the Connection becomes unreferenced and GC'd is. This is not recommended. You should call .close() on connections when you are done using them (this is in fact said in the JDBC spec and API docs as well).