Domanda

I am creating a JDBC ODBC connection through JAVA programs. I have to make with this connection so many times . After some times the program throws the Too Many Client Task Exception . How can is resolve this problem . I am pasting a sample example of my requirement

class connectiondemo
{

public void connect()
{

 try {

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection con=DriverManager.getConnection("Jdbc:Odbc:dsn");
   Statement st= con.createStatement();

   }

  catch(Exception ex)
  {
   ex.printStackTrace();
  }
 }
}


calling programs

 class 
 {
  public static void main(String args[])

   {
    //supose i have to call connect methods thousands of times then what is the solution

    connectdemo demo= new connectdemo();
   dem0.connect();

   }
  }
È stato utile?

Soluzione

The key is saving connections.Why not use a static method to call connections as in:

public class Connector {

private static final String URL = "jdbc:mysql://localhost/";
private static final String LOGIN = "root";
private static final String PASSWORD = "azerty";
private static final String DBNAME = "videotheque";
private static Connector connector;
private static Connection connection;

private Connector() {
}

public synchronized static Connector getInstance() {
    if (connector == null) {
        connector = new Connector();
    }
    return connector;
}

public static Connection getConnection() {
    if (connection == null) {
        Connection c = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            c = DriverManager.getConnection(URL + DBNAME, LOGIN, PASSWORD);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return c;
    }
    return connection;
}

For connecting you can use it as:

Connector.getInstance().getConnection()

Altri suggerimenti

It seems you are opening too many db connections and not closing them. You need to make sure that you close the connections once you are done while executing the jdbc statements on that connection. However, if you forget, Java's garbage collector will close the connection when it cleans up stale objects.

Relying on garbage collection, especially in database programming, is very poor programming practice. You should make a habit of always closing the connection with the close() method associated with connection object.

Maybe adding a finally block should help you, something like this :

Connection con = null;
 try {

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("Jdbc:Odbc:dsn:);
   Statement st= con.createStatement();

   }

  catch(Exception ex)
  {
   ex.printStackTrace();
  } finally {
      try{
          con.close();
      } catch (Exception e) {
      }
  }
 }

How many connections do you actually require? The options are:

  • A single connection that is stored globally and used multiple times (connects only once).
  • A connection pool containing a number of connections that are connected when the pool starts up. This allows multiple threads to access the database (almost) concurrently.

I suspect the first option is probably the right one for your application. In which case, connect once and share the connection, otherwise your DBA might have something to say:

public static Connection getConnection() {
    if(connection == null) {
        //make and store connection globally
    }
    return connection;
}

public static void closeConnection() {
     if(connection != null) {
          //close connection
     }
}

public void clientMethod() {
    <SomeClass>.getConnection().prepareStatement("whatever");
}

Making a database connection is a costly exercise and will have a noticeable impact on the performance of your application, do it as few times as you can.

This is poor looking code for many reasons. I'd recommend that you not try to write this yourself, given your level of programming sophistication. Better to acquire and use a connection pool written by someone else, like the Apache DBCP library.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top