Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JDBC Too Many Connection Error

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs down JDBC Too Many Connection Error

    Please help me regarding to this error guys...

    I'm using a JDBC class for a Java Servlet web application and in my web application, my categories will be loading from the Database and after some page exchanges, I'm getting this error.


    com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException:
    Data source rejected establishment of connection, message from server: "Too many connections"

    please help me...

    My JDBC class is


    package db;
     
    import java.sql.*;
     
    public class DB {
     
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/lkshops";
     
        Connection getConnection() throws Exception {
            Class.forName(driver);
            Connection con = DriverManager.getConnection(url, "lkshops", "123");
            return con;
        }
     
        public void putdata(String sql) {
            try {
                PreparedStatement state = getConnection().prepareStatement(sql);
                state.executeUpdate();
                state.close();
            } catch (Exception e) {
            }
        }
     
        public ResultSet getdata(String sql) throws Exception {
            Statement state = getConnection().createStatement();
            ResultSet rset = state.executeQuery(sql);
            return rset;
        }
    }

    And, My Category JSP page's code is,

    <%@page import="java.sql.ResultSet"%>
    <%@page import="db.DB"%>
    <%
        DB db = new db.DB();
     
        try {
     
            int x = 0;
            ResultSet rs = db.getdata("SELECT max(Cat_ID) as maxVal FROM categories");
            rs.next();
            x = rs.getInt("maxVal");
     
            for (int i = 1; i <= x; i++) {
     
                ResultSet rset = db.getdata("SELECT * FROM categories WHERE Cat_ID = '" + i + "' ");
                ResultSet rset2 = db.getdata("SELECT * FROM sub_categories WHERE Cat_ID = '" + i + "'");
                rset.next();
    %>
    <div id="smoothmenu1" class="ddsmoothmenu" style="width: 1000px;">
        <ul>
            <li><a href="#"><% out.print(rset.getString("Cat_Name"));%></a>
     
                <% if (rset2 != null) {
                %><ul> <% while (rset2.next()) {
                    %><li><a href="#"><% out.print(rset2.getString("Sub_Cat_Name")); %></a>
                    </li><% }%> </ul> <% } else {
                    %> <%}%>
            </li>
        </ul>
    </div>
    <% }
        } catch (Exception ex) {
            out.print(ex);
        }
    %>

    Please tell me if there has any of error and I want to correct and learn this method very clearly guys... thanks a lot...
    Last edited by sajithgsm; February 22nd, 2012 at 02:14 AM. Reason: Bold


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: JDBC Too Many Connection Error

    You've got some lame* exception handling in your code and you're not adequately closing the Java/MySQL resources. The MySQL Connector/J resources must be very carefully closed off after use, or you'll hit problems like yours or suffer memory leaks. Close down resources exactly as Mark Matthews does it in this post:

    MySQL :: Connection pooling with MySQL Connector/J

    It seems a very long-winded way of doing it, but anything less than this will eventually run into resource exhaustion problems.

    * here for example:
                PreparedStatement state = getConnection().prepareStatement(sql);
                state.executeUpdate();
                state.close();
    If either of the first two lines throw an Exception, your close() method won't be executed. Either write separate try blocks or (better) use a 'finally' block to clean up - with separate try blocks inside it if you're cleaning up more than one resource.

Similar Threads

  1. Error in URL connection
    By HardikDobaria in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2012, 07:50 AM
  2. [SOLVED] JDBC Connection from Single Computer
    By Max Avion in forum JDBC & Databases
    Replies: 4
    Last Post: November 17th, 2011, 12:41 PM
  3. jdbc connection problem
    By sny in forum JDBC & Databases
    Replies: 11
    Last Post: January 6th, 2011, 04:39 AM
  4. JDBC connection error
    By nrao in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 11th, 2010, 12:43 PM
  5. jdbc connection with phpmyadmin
    By anand_fevi in forum JDBC & Databases
    Replies: 1
    Last Post: May 13th, 2010, 02:26 AM

Tags for this Thread