1

I would like go know the jdbc connection exception and dbbool connection exception and How can I avoid those.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kannan Thangadurai
  • 1,117
  • 2
  • 17
  • 36
  • Can you be more specific? What exactly is your problem? What have you tried? – Korem Jun 20 '14 at 17:23
  • If I have 10 JDBC Connections and those are active. when I try to create new connection, I should catch the proper exception and display proper error message instead of showing error – Kannan Thangadurai Jun 20 '14 at 17:56
  • use a static connection then you use one connection .it's like you use one water pipe for get water from a well.if you open connection and close them it's like you use one pipe but put a tube get water and pullout it.again input pipe then get water then remove it and so on.and if you open connection without closing you fill your well with pipes.best way is to use a static connection – Madhawa Priyashantha Jul 30 '14 at 14:27

3 Answers3

2

If you are using then Apache Commons Pool library, the GenereicObjectPool provides two methods:

getMaxActive() returns the amount of allowed Connections

getNumActive() returns the number of actually used Connections

If you check getMaxActive() > getNumActive() you can achive your requirement without catching Exceptions.

Robert
  • 230
  • 2
  • 9
1

To display error message properly you have to put try and catch in the right place and at right time, To summarize you can use java.sql.SQLException as it can occur can both in the driver and the database.

The basic and common scheme of any JDBC operations are:

1). Register your Driver

2). Open a connection.

3). Execute query

4). Extracting datas

5). Closing, Close everything: the ResultSet, the Statement, and the Connection.

This is how you correctly catch the error or exception causes :

try{
  //1).Register JDBC driver
  Class.forName("com.mysql.jdbc.Driver");

  // 2). Open a connection
  conn = DriverManager.getConnection(DB_URL,USER,PASS);

  //3). Execute a query
  System.out.println("Creating statement...");
  Statement stmt = conn.createStatement();
  String sql;
  sql = "SELECT id, first, last, age FROM Employees";
  ResultSet rs = stmt.executeQuery(sql);

  //4). Extracting datas
  while(rs.next()){
    .................
.............
  }
  //5).close everything
   rs.close();
   stmt.close();
   conn.close();
     } catch(SQLException se){
          //Handle errors for JDBC
           se.printStackTrace();
    }  catch(Exception e){
         //Handle errors for Class.forName / JDBC driver
          e.printStackTrace();
   }finally{
    //finally block used to close resources
    try{
        if(conn!=null)
             conn.close();
          } catch(SQLException se){
                se.printStackTrace();
              }//end finally try
    }//end try

And about JDBC connection pool , you can check out links provided below:

How to establish a connection pool in JDBC?

Community
  • 1
  • 1
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19
1

It really depends on the pool library you are using.

If you are using Apache Commons Pool, then set BaseGenericObjectPool.setBlockWhenExhausted(false).

Then calls to GenericObjectPool.borrowObject(long borrowMaxWaitMillis) will throw a NoSuchElementException.

piknyc
  • 61
  • 4