tags:

views:

15

answers:

0

Hi

I just faced the following exception :

Exception Occured while Connecting : com.mysql.jdbc.CommunicationsException: The driver was unable to create a connection due to an inability to establish the client portion of a socket.

This is usually caused by a limit on the number of sockets imposed by the operating system. This limit is usually configurable.

This made me go back to my underlying classes to make sure i'm closing the connections properly.

I have a Functions class >

public class Functions {
    private String DB_SERVER;
    private String DB_NAME;
    private String DB_USERNAME;
    private String DB_PASSWORD;
    public  Connection con;

    public void connect()    {
        String[] dbParms = Parameters.load();

        DB_SERVER = dbParms[0];
        DB_NAME = dbParms[1];
        DB_USERNAME = dbParms[2];
        DB_PASSWORD = dbParms[3];

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
        catch(Exception e)  {
            new util.ShowError( new javax.swing.JFrame(),
                                "Database Connect",
                                "Connection Error",
                                "Unable to set class for mySQL Driver",
                                e );
            return;
        }

        try {
            con = DriverManager.getConnection("jdbc:mysql://"
                    + DB_SERVER + "/"
                    + DB_NAME, DB_USERNAME, DB_PASSWORD);
        }
        catch(SQLException se)  {
            new util.ShowError( new javax.swing.JFrame(),
                                "Database Connect",
                                "Connection Error",
                                "Unable to connect to Database",
                                se );       
        }
    }

    public void disconnect()  {
        try {
            if(con != null)
                con.close();
        }
        catch(SQLException se)  {
            new util.ShowError( new javax.swing.JFrame(),
                                "Database Disconnect",
                                "Connection Error",
                                "Unable to disconnect frpm Database",
                                se );       
        }
    }
}

Now, i have multiple classes (table based) which use the Functions class for operations.

Let's take one of these - the Global class >

public class Global {
    public static Object getValueOf(String table, long ID, String column)    {
        Object value = null;        
        Functions dbf = new Functions();
        dbf.connect();

        String query = "SELECT " + column + " FROM " + table + " WHERE ID = ?";

        try {
            PreparedStatement ps = dbf.con.prepareStatement(query);
            ps.setLong(1, ID);
            ResultSet rs = ps.executeQuery();
            rs.first();

            value = rs.getObject(column);
        }
        catch(SQLException se)   {
            new util.ShowError( new javax.swing.JFrame(),
                                "DB Error",
                                "Retrieval Error",
                                "Unable to get value of " + table + "[" + column + "] at ID = " + ID,
                                se );
        }
        finally {
            dbf.disconnect();
            return value;
        }
    }

    public static void delete(long ID, String table)    {
        Functions dbf = new Functions();
        dbf.connect();

        String query = "DELETE FROM " + table + " WHERE ID = ?";
        try {
            PreparedStatement ps = dbf.con.prepareStatement(query);
            ps.setLong(1, ID);
            ps.executeUpdate();
        }
        catch(SQLException se)   {
            new util.ShowError( new javax.swing.JFrame(),
                                "DB Error",
                                "Deletion Error",
                                "Unable to delete from " + table + " at ID = " + ID,
                                se );
        }
        finally {
            dbf.disconnect();
        }
    }

    public static long getCount(String table)    {
        long count = -1;
        Functions dbf = new Functions();
        dbf.connect();

        String query = "SELECT COUNT(ID) FROM " + table;
        try {
            PreparedStatement ps = dbf.con.prepareStatement(query);
            ResultSet rs = ps.executeQuery();

            if(rs != null && rs.first())
                count = rs.getLong(1);
            else
                count = 0;
        }
        catch(SQLException se)   {
            new util.ShowError( new javax.swing.JFrame(),
                                "DB Error",
                                "Retrieval Error",
                                "Unable to get count of table " + table,
                                se );
        }
        finally {
            dbf.disconnect();
            return count;
        }
    }
}

Now i may be making complex calls such as :

basicTotalField.setText("" + objects.billtemp.getBasicTotal(TID));

Where the function getBasicTotal() may get as complicated as >

public static BigDecimal getBasicTotal(long billtemp)    {
    BigDecimal btotal = BigDecimal.ZERO;

    Functions dbf = new Functions();
    dbf.connect();

    String query = "SELECT * FROM billitemstemp" +
            " LEFT JOIN item on billitemstemp.Item = item.ID" +
            " WHERE billitemstemp.BillTemp = ?";

    try {
        PreparedStatement ps = dbf.con.prepareStatement(query);
        ps.setLong(1, billtemp);

        ResultSet rs = ps.executeQuery();

        if(rs != null)  {
            while(rs.next())    {
                btotal = btotal.add(objects.item.getAmount(
                        rs.getBigDecimal("billitemstemp.Price"),
                        rs.getBigDecimal("billitemstemp.Discount"),
                        rs.getInt("billitemstemp.Quantity")
                        ));
            }
        }
    }
    catch(SQLException se)   {
        new util.ShowError( new javax.swing.JFrame(),
                            "DB Error",
                            "Retrieval Error",
                            "getBasicTotal()",
                            se );
    }
    finally {
        btotal = btotal.setScale(2, BigDecimal.ROUND_HALF_UP);
        dbf.disconnect();
        return btotal;
    }
}

Am i walking into big trouble here?

Where should i start the corrections / refactoring?