Drish Mali
Drish Mali

Reputation: 35

Too many connection in java jdbc

Error:- SEVERE: null com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections" code is shown below

   try {
        BufferedReader br=new BufferedReader(new FileReader(filename));
        String line;
        String tru="TRUNCATE `project`.`uploadtable`;";
        try
       {
             Statement stmt = Dutil.getConnection().createStatement();



     stmt.executeUpdate(tru);
        }
        catch(Exception e){}
        try {
            while((line=br.readLine())!=null){
                String values[]=line.split("\t");
                String[] splitStr = values[1].split(" ");


               try {String  sql="INSERT INTO `project`.`uploadtable` 
            (`empid`, `date`, `time`, `in_out_index`) VALUES 
            ('"+values[0]+"', '"+splitStr[0]+"', '"+splitStr[1]+"', 
             '"+values[3]+"');";
                    PreparedStatement 
                   pst=Dutil.getConnection().prepareStatement(sql);
                    pst.executeUpdate();
                } catch (SQLException ex) {
                    System.out.println("Error");

             Logger.getLogger(UploadFrame.class.getName()).log(Level.SEVERE, 
              null, ex);
                }
            } br.close();

             this.dispose();
           LogButtonFrame lbf=new LogButtonFrame();
          lbf.clockinouttable();
           JOptionPane.showMessageDialog(null,"Upload Complete");} catch 
           (IOException ex) {
            Logger.getLogger(UploadFrame.class.getName()).log(Level.SEVERE, 
          null, ex);
            }
             } catch (FileNotFoundException ex) {
           Logger.getLogger(UploadFrame.class.getName()).log(Level.SEVERE, 
            null, ex);
          }
          catch (Exception ex) {
         JOptionPane.showMessageDialog(null, "Error");
        }

Upvotes: 1

Views: 1185

Answers (3)

M.Navy
M.Navy

Reputation: 155

If your application requires more than one connection, you should use a connection pool like the Apache DB connection pool where you can configure number of connections. Here is the link for DBCP documentation https://commons.apache.org/proper/commons-dbcp/

If you are sure your application only needs one connection, make your connection object as a Singleton object in DBUtil class. This will definitely solve the issue and no need to close the connection. Your entire application will make use of single connection object to connect to DB.

Upvotes: 0

Mark Rotteveel
Mark Rotteveel

Reputation: 109240

The problem is:

PreparedStatement pst = Dutil.getConnection().prepareStatement(sql);

You haven't shown the code of Dutil, but assuming it creates a new connection on each call, this means the connection is not getting closed, leaving it to luck, driver implementation specifics, and the garbage collector to get it closed.

Instead you should use try-with-resources:

try (Connection connection = Dutil.getConnection();
     PreparedStatement pst = connection.prepareStatement(sql)) {
    // Use pst
}

After the end of this block, both pst and connection will be properly closed, even if exceptions occur, etc.

Upvotes: 2

DaveH
DaveH

Reputation: 7335

You seem to be creating connections and never closing them, although its hard to tell from the images you've posed.

Upvotes: 0

Related Questions